Repository navigation
Expand file tree
/
Copy pathLogImpl.cpp
More file actions
343 lines (317 loc) · 10.7 KB
/
Copy pathLogImpl.cpp
File metadata and controls
343 lines (317 loc) · 10.7 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//-----------------------------------------------------------------------------
//
// LogImpl.cpp
//
// Unix implementation of message and error logging
//
// Copyright (c) 2010, Greg Satz <satz@iranger.com>
// All rights reserved.
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include <string>
#include <cstring>
#include <pthread.h>
#include <iostream>
#include "Defs.h"
#include "LogImpl.h"
namespace OpenZWave
{
namespace Internal
{
namespace Platform
{
//-----------------------------------------------------------------------------
// <LogImpl::LogImpl>
// Constructor
//-----------------------------------------------------------------------------
LogImpl::LogImpl(string const& _filename, bool const _bAppendLog, bool const _bConsoleOutput, LogLevel const _saveLevel, LogLevel const _queueLevel, LogLevel const _dumpTrigger) :
m_filename(_filename), // name of log file
m_bConsoleOutput(_bConsoleOutput), // true to provide a copy of output to console
m_bAppendLog(_bAppendLog), // true to append (and not overwrite) any existing log
m_saveLevel(_saveLevel), // level of messages to log to file
m_queueLevel(_queueLevel), // level of messages to log to queue
m_dumpTrigger(_dumpTrigger), // dump queued messages when this level is seen
pFile( NULL)
{
if (!m_filename.empty())
{
if (!m_bAppendLog)
{
this->pFile = fopen(m_filename.c_str(), "w");
}
else
{
this->pFile = fopen(m_filename.c_str(), "a");
}
if (this->pFile == NULL)
{
std::cerr << "Could Not Open OZW Log File." << std::endl;
}
else
{
setlinebuf(this->pFile);
}
}
setlinebuf(stdout); // To prevent buffering and lock contention issues
}
//-----------------------------------------------------------------------------
// <LogImpl::~LogImpl>
// Destructor
//-----------------------------------------------------------------------------
LogImpl::~LogImpl()
{
if (this->pFile)
fclose(this->pFile);
}
unsigned int LogImpl::toEscapeCode(LogLevel _level)
{
unsigned int code = 39;
switch (_level)
{
case LogLevel_Internal:
case LogLevel_StreamDetail:
code = 97;
break; // 97=bright white
case LogLevel_Debug:
code = 36;
break; // 36=cyan
case LogLevel_Detail:
code = 94;
break; // 94=bright blue
case LogLevel_Info:
code = 39;
break; // 39=white
case LogLevel_Alert:
code = 93;
break; // 93=bright yellow
case LogLevel_Warning:
code = 33;
break; // 33=yellow
case LogLevel_Error:
code = 31;
break; // 31=red
case LogLevel_Fatal:
code = 95;
break; // 95=bright magenta
case LogLevel_Always:
code = 32;
break; // 32=green
case LogLevel_Invalid:
case LogLevel_None:
code = 39;
break; // 39=white (reset to default)
}
return code;
}
//-----------------------------------------------------------------------------
// <LogImpl::Write>
// Write to the log
//-----------------------------------------------------------------------------
void LogImpl::Write(LogLevel _logLevel, uint8 const _nodeId, char const* _format, va_list _args)
{
// create a timestamp string
string timeStr = GetTimeStampString();
string nodeStr = GetNodeString(_nodeId);
string loglevelStr = GetLogLevelString(_logLevel);
// handle this message
if ((_logLevel <= m_queueLevel) || (_logLevel == LogLevel_Internal)) // we're going to do something with this message...
{
char lineBuf[1024] =
{ 0 };
//int lineLen = 0;
if (_format != NULL && _format[0] != '\0')
{
va_list saveargs;
va_copy(saveargs, _args);
vsnprintf(lineBuf, sizeof(lineBuf), _format, _args);
va_end(saveargs);
}
// should this message be saved to file (and possibly written to console?)
if ((_logLevel <= m_saveLevel) || (_logLevel == LogLevel_Internal))
{
std::string outBuf;
if (this->pFile != NULL || m_bConsoleOutput)
{
if (_logLevel != LogLevel_Internal) // don't add a second timestamp to display of queued messages
{
outBuf.append(timeStr);
outBuf.append(loglevelStr);
outBuf.append(nodeStr);
outBuf.append(lineBuf);
outBuf.append("\n");
}
// print message to file (and possibly screen)
if (this->pFile != NULL)
{
fputs(outBuf.c_str(), pFile);
}
if (m_bConsoleOutput)
{
fprintf(stdout, "\x1B[%02um", toEscapeCode(_logLevel));
fputs(outBuf.c_str(), stdout);
fprintf(stdout, "\x1b[39m");
/* always return to normal */
fprintf(stdout, "\x1B[%02um", toEscapeCode(LogLevel_Info));
}
}
}
if (_logLevel != LogLevel_Internal)
{
char queueBuf[1024];
string threadStr = GetThreadId();
snprintf(queueBuf, sizeof(queueBuf), "%s%s%s", timeStr.c_str(), threadStr.c_str(), lineBuf);
Queue(queueBuf);
}
}
// now check to see if the _dumpTrigger has been hit
if ((_logLevel <= m_dumpTrigger) && (_logLevel != LogLevel_Internal) && (_logLevel != LogLevel_Always))
QueueDump();
}
//-----------------------------------------------------------------------------
// <LogImpl::Queue>
// Write to the log queue
//-----------------------------------------------------------------------------
void LogImpl::Queue(char const* _buffer)
{
string bufStr = _buffer;
m_logQueue.push_back(bufStr);
// rudimentary queue size management
if (m_logQueue.size() > 500)
{
m_logQueue.pop_front();
}
}
//-----------------------------------------------------------------------------
// <LogImpl::QueueDump>
// Dump the LogQueue to output device
//-----------------------------------------------------------------------------
void LogImpl::QueueDump()
{
Log::Write(LogLevel_Always, "");
Log::Write(LogLevel_Always, "Dumping queued log messages");
Log::Write(LogLevel_Always, "");
list<string>::iterator it = m_logQueue.begin();
while (it != m_logQueue.end())
{
string strTemp = *it;
Log::Write(LogLevel_Internal, strTemp.c_str());
it++;
}
m_logQueue.clear();
Log::Write(LogLevel_Always, "");
Log::Write(LogLevel_Always, "End of queued log message dump");
Log::Write(LogLevel_Always, "");
}
//-----------------------------------------------------------------------------
// <LogImpl::Clear>
// Clear the LogQueue
//-----------------------------------------------------------------------------
void LogImpl::QueueClear()
{
m_logQueue.clear();
}
//-----------------------------------------------------------------------------
// <LogImpl::SetLoggingState>
// Sets the various log state variables
//-----------------------------------------------------------------------------
void LogImpl::SetLoggingState(LogLevel _saveLevel, LogLevel _queueLevel, LogLevel _dumpTrigger)
{
m_saveLevel = _saveLevel;
m_queueLevel = _queueLevel;
m_dumpTrigger = _dumpTrigger;
}
//-----------------------------------------------------------------------------
// <LogImpl::GetTimeStampString>
// Generate a string with formatted current time
//-----------------------------------------------------------------------------
std::string LogImpl::GetTimeStampString()
{
// Get a timestamp
struct timeval tv;
gettimeofday(&tv, NULL);
// use threadsafe verion of localtime. Reported by nihilus, 2019-04
// https://www.gnu.org/software/libc/manual/html_node/Broken_002ddown-Time.html#Broken_002ddown-Time
struct tm *tm, xtm;
memset(&xtm, 0, sizeof(xtm));
tm = localtime_r(&tv.tv_sec, &xtm);
// create a time stamp string for the log message
char buf[100];
snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d.%03d ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int) tv.tv_usec / 1000);
string str = buf;
return str;
}
//-----------------------------------------------------------------------------
// <LogImpl::GetNodeString>
// Generate a string with formatted node id
//-----------------------------------------------------------------------------
std::string LogImpl::GetNodeString(uint8 const _nodeId)
{
if (_nodeId == 0)
{
return "";
}
else if (_nodeId == 255) // should make distinction between broadcast and controller better for SwitchAll broadcast
{
return "contrlr, ";
}
else
{
char buf[20];
snprintf(buf, sizeof(buf), "Node%03d, ", _nodeId);
return buf;
}
}
//-----------------------------------------------------------------------------
// <LogImpl::GetThreadId>
// Generate a string with formatted thread id
//-----------------------------------------------------------------------------
std::string LogImpl::GetThreadId()
{
char buf[20];
snprintf(buf, sizeof(buf), "%08lx ", (long unsigned int) pthread_self());
string str = buf;
return str;
}
//-----------------------------------------------------------------------------
// <LogImpl::SetLogFileName>
// Provide a new log file name (applicable to future writes)
//-----------------------------------------------------------------------------
void LogImpl::SetLogFileName(const string &_filename)
{
m_filename = _filename;
}
//-----------------------------------------------------------------------------
// <LogImpl::GetLogLevelString>
// Provide a new log file name (applicable to future writes)
//-----------------------------------------------------------------------------
std::string LogImpl::GetLogLevelString(LogLevel _level)
{
if ((_level >= LogLevel_None) && (_level <= LogLevel_Internal))
{
char buf[20];
snprintf(buf, sizeof(buf), "%s, ", LogLevelString[_level]);
return buf;
}
else
return "Unknown, ";
}
} // namespace Platform
} // namespace Internal
} // namespace OpenZWave