-
Notifications
You must be signed in to change notification settings - Fork 7
/
Logger.cpp
130 lines (111 loc) · 2.85 KB
/
Logger.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
126
127
128
129
130
/*************************************************************************
*
* This file is part of the Remoterelay Arduino sketch.
* Copyleft 2017 Nicolas Agius <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ***********************************************************************/
#include "Logger.h"
Logger::Logger()
{
// Init ring log
for (int i=0; i<RINGLOG_SIZE; i++)
ringlog[i][0]='\0';
enableDebug = false;
enableSerial = false;
}
void Logger::setDebug(bool d)
{
enableDebug = d;
}
void Logger::setSerial(bool d)
{
enableSerial = d;
}
void Logger::debug(const char *fmt, ...)
{
if(enableDebug)
{
va_list ap;
va_start(ap, fmt);
log(fmt, ap);
va_end(ap);
}
}
void Logger::info(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log(fmt, ap);
va_end(ap);
}
void Logger::log(const char *fmt, va_list ap)
{
char buffer[BUF_LEN];
// Generate log message (does not support float)
vsnprintf(buffer, BUF_LEN, fmt, ap);
// Add timestamp header
uint32_t uptime = millis();
snprintf(ringlog[index], BUF_LEN, "[%d.%03d] %s", uptime / 1000, uptime % 1000, buffer);
if(enableSerial)
Serial.println(ringlog[index]);
// Loop over at the begining of the ring
if (++index >= RINGLOG_SIZE)
index=0;
}
String Logger::getLog()
{
// Get uptime
char uptime[9];
int sec = millis() / 1000;
int min = sec / 60;
int hour = min / 60;
snprintf(uptime, sizeof(uptime), "%02d:%02d:%02d", hour, min % 60, sec % 60);
// Generate header
String msg = " ==== DEBUG LOG ====";
msg += "\r\nChip ID: ";
msg += ESP.getChipId();
msg += "\r\nFree Heap: ";
msg += ESP.getFreeHeap();
msg += "\r\nFlash Size: ";
msg += ESP.getFlashChipSize();
msg += "\r\nUptime: ";
msg += uptime;
msg += "\r\nPrinting last ";
msg += RINGLOG_SIZE;
msg += " lines of the log:\r\n";
// Get most recent half of the ring
for(int i=index; i<RINGLOG_SIZE; i++)
{
if(strlen(ringlog[i]) > 0)
{
msg += ringlog[i];
msg += "\r\n";
}
}
// Get older half of the ring
for(int i=0; i<index; i++)
{
if(strlen(ringlog[i]) > 0)
{
msg += ringlog[i];
msg += "\r\n";
}
}
msg += " ==== END LOG ====\r\n";
if(enableSerial)
Serial.print(msg);
return msg;
}