Repository navigation
Expand file tree
/
Copy pathLog.cpp
More file actions
306 lines (282 loc) · 11.3 KB
/
Copy pathLog.cpp
File metadata and controls
306 lines (282 loc) · 11.3 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
//-----------------------------------------------------------------------------
//
// Log.cpp
//
// Cross-platform message and error logging
//
// Copyright (c) 2010 Mal Lansell <mal@lansell.org>
// 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 <stdarg.h>
#include "Defs.h"
#include "platform/Mutex.h"
#include "platform/Log.h"
#ifdef WIN32
#include "platform/windows/LogImpl.h" // Platform-specific implementation of a log
#elif defined WINRT
#include "platform/winRT/LogImpl.h" // Platform-specific implementation of a log
#else
#include "platform/unix/LogImpl.h" // Platform-specific implementation of a log
#endif
using namespace OpenZWave;
char const *OpenZWave::LogLevelString[] =
{ "Invalid", /**< Invalid Log Level Status - Used to Indicate error from Importing bad Options.xml */
"None", /**< LogLevel_None Disable all logging */
"Always", /**< LogLevel_Always These messages should always be shown */
"Fatal", /**< LogLevel_Fatal A likely fatal issue in the library */
"Error", /**< LogLevel_Error A serious issue with the library or the network */
"Warning", /**< LogLevel_Warning A minor issue from which the library should be able to recover */
"Alert", /**< LogLevel_Alert Something unexpected by the library about which the controlling application should be aware */
"Info", /**< LogLevel_Info Everything's working fine...these messages provide streamlined feedback on each message */
"Detail", /**< LogLevel_Detail Detailed information on the progress of each message */
"Debug", /**< LogLevel_Debug Very detailed information on progress that will create a huge log file quickly
But this level (as others) can be queued and sent to the log only on an error or warning */
"StreamDetail", /**< LogLevel_StreamDetail Will include low-level byte transfers from controller to buffer to application and back */
"Internal" /**< LogLevel_Internal Used only within the log class (uses existing timestamp, etc.) */
};
Log* Log::s_instance = NULL;
std::vector<i_LogImpl*> Log::m_pImpls;
static bool s_dologging;
//-----------------------------------------------------------------------------
// <Log::Create>
// Static creation of the singleton
//-----------------------------------------------------------------------------
Log* Log::Create(string const& _filename, bool const _bAppend, bool const _bConsoleOutput, LogLevel const _saveLevel, LogLevel const _queueLevel, LogLevel const _dumpTrigger)
{
if ( NULL == s_instance)
{
s_instance = new Log(_filename, _bAppend, _bConsoleOutput, _saveLevel, _queueLevel, _dumpTrigger);
s_dologging = true; // default logging to true so no change to what people experience now
}
else
{
Log::Destroy();
s_instance = new Log(_filename, _bAppend, _bConsoleOutput, _saveLevel, _queueLevel, _dumpTrigger);
s_dologging = true; // default logging to true so no change to what people experience now
}
return s_instance;
}
//-----------------------------------------------------------------------------
// <Log::Create>
// Static creation of the singleton
//-----------------------------------------------------------------------------
/* It isn't clear this is ever called or used. If no one complains, consider
deleting this code in April 2012.
Log* Log::Create
(
i_LogImpl *LogClass
)
{
if (NULL == s_instance )
{
s_instance = new Log( "" );
s_dologging = true;
}
SetLoggingClass( LogClass );
return s_instance;
}
*/
//-----------------------------------------------------------------------------
// <Log::Destroy>
// Static method to destroy the logging singleton.
//-----------------------------------------------------------------------------
void Log::Destroy()
{
delete s_instance;
s_instance = NULL;
}
//-----------------------------------------------------------------------------
// <Log::SetLoggingClass>
// Set log class
//-----------------------------------------------------------------------------
bool Log::SetLoggingClass(i_LogImpl *LogClass, bool Append)
{
if (!Append) {
for (std::vector<i_LogImpl*>::iterator it = s_instance->m_pImpls.begin(); it != s_instance->m_pImpls.end();) {
i_LogImpl *lc = *it;
delete lc;
it = s_instance->m_pImpls.erase(it);
}
}
s_instance->m_pImpls.push_back(LogClass);
return true;
}
//-----------------------------------------------------------------------------
// <Log::SetLoggingState>
// Set flag to actually write to log or skip it (legacy version)
// If logging is enabled, the default log detail settings will be used
// Write to file/screen LogLevel_Detail
// Save in queue for errors LogLevel_Debug
// Trigger for dumping queue LogLevel_Warning
// Console output? Yes
// Append to an existing log? No (overwrite)
//-----------------------------------------------------------------------------
void Log::SetLoggingState(bool _dologging)
{
bool prevLogging = s_dologging;
s_dologging = _dologging;
if (!prevLogging && s_dologging)
Log::Write(LogLevel_Always, "Logging started\n\n");
}
//-----------------------------------------------------------------------------
// <Log::SetLoggingState>
// Set flag to actually write to log or skip it
//-----------------------------------------------------------------------------
void Log::SetLoggingState(LogLevel _saveLevel, LogLevel _queueLevel, LogLevel _dumpTrigger)
{
// parameter checking:
// _queueLevel cannot be less than or equal to _saveLevel (where lower ordinals are more severe conditions)
// _dumpTrigger cannot be greater than or equal to _queueLevel
if (_queueLevel <= _saveLevel)
Log::Write(LogLevel_Warning, "Only lower priority messages may be queued for error-driven display.");
if (_dumpTrigger >= _queueLevel)
Log::Write(LogLevel_Warning, "The trigger for dumping queued messages must be a higher-priority message than the level that is queued.");
bool prevLogging = s_dologging;
// s_dologging is true if any messages are to be saved in file or queue
if ((_saveLevel > LogLevel_Always) || (_queueLevel > LogLevel_Always))
{
s_dologging = true;
}
else
{
s_dologging = false;
}
if (s_instance && s_dologging && (s_instance->m_pImpls.size() > 0))
{
s_instance->m_logMutex->Lock();
for (std::vector<i_LogImpl*>::iterator it = s_instance->m_pImpls.begin(); it != s_instance->m_pImpls.end(); it++)
(*it)->SetLoggingState(_saveLevel, _queueLevel, _dumpTrigger);
s_instance->m_logMutex->Unlock();
}
if (!prevLogging && s_dologging)
Log::Write(LogLevel_Always, "Logging started\n\n");
}
//-----------------------------------------------------------------------------
// <Log::GetLoggingState>
// Return a flag to indicate whether logging is enabled
//-----------------------------------------------------------------------------
bool Log::GetLoggingState()
{
return s_dologging;
}
//-----------------------------------------------------------------------------
// <Log::Write>
// Write to the log
//-----------------------------------------------------------------------------
void Log::Write(LogLevel _level, char const* _format, ...)
{
if (s_instance && s_dologging && (s_instance->m_pImpls.size() > 0))
{
s_instance->m_logMutex->Lock(); // double locks if recursive
va_list args;
va_start(args, _format);
for (std::vector<i_LogImpl*>::iterator it = s_instance->m_pImpls.begin(); it != s_instance->m_pImpls.end(); it++)
(*it)->Write(_level, 0, _format, args);
va_end(args);
s_instance->m_logMutex->Unlock();
}
}
//-----------------------------------------------------------------------------
// <Log::Write>
// Write to the log
//-----------------------------------------------------------------------------
void Log::Write(LogLevel _level, uint8 const _nodeId, char const* _format, ...)
{
if (s_instance && s_dologging && (s_instance->m_pImpls.size() > 0))
{
if (_level != LogLevel_Internal)
s_instance->m_logMutex->Lock();
va_list args;
va_start(args, _format);
for (std::vector<i_LogImpl*>::iterator it = s_instance->m_pImpls.begin(); it != s_instance->m_pImpls.end(); it++)
(*it)->Write(_level, _nodeId, _format, args);
va_end(args);
if (_level != LogLevel_Internal)
s_instance->m_logMutex->Unlock();
}
}
//-----------------------------------------------------------------------------
// <Log::QueueDump>
// Send queued messages to the log (and empty the queue)
//-----------------------------------------------------------------------------
void Log::QueueDump()
{
if (s_instance && s_dologging && (s_instance->m_pImpls.size() > 0))
{
s_instance->m_logMutex->Lock();
for (std::vector<i_LogImpl*>::iterator it = s_instance->m_pImpls.begin(); it !=s_instance->m_pImpls.end(); it++)
(*it)->QueueDump();
s_instance->m_logMutex->Unlock();
}
}
//-----------------------------------------------------------------------------
// <Log::QueueClear>
// Empty the queued message queue
//-----------------------------------------------------------------------------
void Log::QueueClear()
{
if (s_instance && s_dologging && (s_instance->m_pImpls.size() > 0))
{
s_instance->m_logMutex->Lock();
for (std::vector<i_LogImpl*>::iterator it = s_instance->m_pImpls.begin(); it !=s_instance->m_pImpls.end(); it++)
(*it)->QueueClear();
s_instance->m_logMutex->Unlock();
}
}
//-----------------------------------------------------------------------------
// <Log::SetLogFileName>
// Change the name of the log file (will start writing a new file)
//-----------------------------------------------------------------------------
void Log::SetLogFileName(const string &_filename)
{
if (s_instance && s_dologging && (s_instance->m_pImpls.size() > 0))
{
s_instance->m_logMutex->Lock();
for (std::vector<i_LogImpl*>::iterator it = s_instance->m_pImpls.begin(); it !=s_instance->m_pImpls.end(); it++)
(*it)->SetLogFileName(_filename);
s_instance->m_logMutex->Unlock();
}
}
//-----------------------------------------------------------------------------
// <Log::Log>
// Constructor
//-----------------------------------------------------------------------------
Log::Log(string const& _filename, bool const _bAppend, bool const _bConsoleOutput, LogLevel const _saveLevel, LogLevel const _queueLevel, LogLevel const _dumpTrigger) :
m_logMutex(new Internal::Platform::Mutex())
{
if (m_pImpls.size() == 0)
{
m_pImpls.push_back(new Internal::Platform::LogImpl(_filename, _bAppend, _bConsoleOutput, _saveLevel, _queueLevel, _dumpTrigger));
}
}
//-----------------------------------------------------------------------------
// <Log::~Log>
// Destructor
//-----------------------------------------------------------------------------
Log::~Log()
{
m_logMutex->Release();
for (std::vector<i_LogImpl*>::iterator it = s_instance->m_pImpls.begin(); it != s_instance->m_pImpls.end();) {
i_LogImpl *lc = *it;
delete lc;
it = s_instance->m_pImpls.erase(it);
}
}