-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.cpp
125 lines (102 loc) · 2.15 KB
/
log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "espp/log.h"
#include "driver/uart.h"
#include "esp_libc.h"
namespace espp {
namespace {
const UBaseType_t _buffer_size = 32;
char _buffer[_buffer_size] = {
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' '
};
char* _last_buffer_char = _buffer + _buffer_size - 1;
const char ZERO_STR[] = "0 ";
const char HEX_PREFIX[] = "0x";
inline
void _OutChar(int ch)
{
ets_putc(ch);
}
inline
void _OutBuffer(const char *buffer, std::size_t size)
{
for(const auto until_ptr = buffer + size; buffer != until_ptr; ++buffer) {
_OutChar(*buffer);
}
}
inline
void _OutUInt(unsigned int src)
{
if(src == 0) {
_OutBuffer(ZERO_STR, 2);
return;
}
char* current_character = _last_buffer_char;
while(src != 0) {
current_character -= 1;
*current_character = static_cast<char>('0' + src % 10);
src /= 10;
}
_OutBuffer(current_character, _last_buffer_char - current_character + 1);
}
inline
void _OutInt(int src)
{
if(src < 0) {
_OutChar('-');
src = -src;
}
_OutUInt(src);
}
}
void Log::_OutChar(int ch)
{
::espp::_OutChar(ch);
}
const Log& Log::operator<<(char obj) const
{
_OutChar(obj);
_OutChar(' ');
return *this;
}
const Log& Log::operator<<(int obj) const
{
_OutInt(obj);
return *this;
}
const Log& Log::operator<<(unsigned int obj) const
{
_OutUInt(obj);
return *this;
}
const Log& Log::operator<<(const void* obj) const
{
_OutBuffer(HEX_PREFIX, 2);
const auto value = reinterpret_cast<uintptr_t>(obj);
_OutUInt(value);
return *this;
}
namespace {
const auto TRUE = "true";
const auto FALSE = "false";
}
const espp::Log& Log::operator<<(const Buffer& buffer) const
{
_OutBuffer(buffer.charData(), buffer.length());
_OutChar(' ');
return *this;
}
const Log& Log::operator<<(const char* obj) const
{
for(;*obj != 0; ++obj) {
_OutChar(*obj);
}
_OutChar(' ');
return *this;
}
const espp::Log& Log::operator<<(bool is) const
{
return *this << (is ? TRUE: FALSE);
}
}