-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtlogger.cpp
More file actions
387 lines (324 loc) · 9.7 KB
/
tlogger.cpp
File metadata and controls
387 lines (324 loc) · 9.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (C) 2024, 2025 by Andreas Theofilu <andreas@theosys.at>
*
* 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, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <filesystem>
#include <cstring>
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include <time.h>
#include "tlogger.h"
#include <QJsonObject>
#include <QJsonDocument>
using std::string;
using std::min;
using std::cout;
using std::cerr;
using std::endl;
using std::ofstream;
using std::streambuf;
using std::stringstream;
using std::atomic;
using std::lock_guard;
using std::mutex;
namespace fs = std::filesystem;
LOG_LEVEL_t TLogger::mLogLevel{LVL_WARN};
LOG_LEVEL_t TLogger::mCurLevel{LVL_ERROR};
bool TLogger::mSyslog{true};
atomic<int> TLogger::mIndent{0};
std::string TLogger::mLogFile;
std::ostream *TLogger::mCOut{&std::cout};
std::ostream *TLogger::mCErr{&std::cerr};
std::ofstream *TLogger::mOut{nullptr};
std::streambuf *TLogger::mCOutOrig{nullptr};
std::streambuf *TLogger::mCErrOrig{nullptr};
std::streambuf *TLogger::mOutSyslog{nullptr};
std::string IOLogger::mLogFile;
string _threadIDtoStr(threadID_t tid)
{
std::stringstream s;
s << std::hex << std::setw(8) << std::setfill('0') << tid;
return s.str();
}
threadID_t _getThreadID()
{
#ifdef __APPLE__
return pthread_mach_thread_np(pthread_self());
#else
return std::this_thread::get_id();
#endif
}
class TSyslog : public streambuf
{
public:
enum { bufsize = 0x2000 };
TSyslog() { this->setp(buffer, buffer + bufsize - 1); }
private:
int overflow(int c)
{
if (c == traits_type::eof())
{
*this->pptr() = traits_type::to_char_type(c);
this->sbumpc();
}
return this->sync()? traits_type::eof(): traits_type::not_eof(c);
}
int sync()
{
int rc = 0;
if (this->pbase() != this->pptr())
{
char writebuf[bufsize+1];
memcpy(writebuf, this->pbase(), this->pptr() - this->pbase());
writebuf[this->pptr() - this->pbase()] = '\0';
int eType;
switch(TLogger::getCurLevel())
{
case LVL_INFO: eType = LOG_INFO; break;
case LVL_WARN: eType = LOG_WARNING; break;
case LVL_ERROR: eType = LOG_ERR; break;
case LVL_TRACE: eType = LOG_NOTICE; break;
case LVL_DEBUG: eType = LOG_DEBUG; break;
case LVL_NOTICE: eType = LOG_NOTICE; break;
case LVL_FATAL: eType = LOG_CRIT; break;
}
syslog(LOG_DAEMON | eType, "%s", writebuf);
rc = 1;
this->setp(buffer, buffer + bufsize - 1);
}
return rc;
}
char buffer[bufsize];
};
TLogger::TLogger()
{
mOutSyslog = new TSyslog;
mCOutOrig = cout.rdbuf(mOutSyslog);
mCErrOrig = cerr.rdbuf(mOutSyslog);
}
void TLogger::log(int level, const char *str, ...)
{
char buffer[8192];
va_list valist;
va_start(valist, str);
vsnprintf(buffer, sizeof(buffer), str, valist);
va_end(valist);
if (mSyslog)
{
int l = level;
if (l <= 0)
l = LOG_NOTICE;
syslog(LOG_DAEMON | l, "%s", buffer);
}
else
*mCOut << getTime() << ": " << getLevelStr(sysLevelToLogLevel(level)) << buffer << endl;
}
std::ostream *TLogger::getCurrent(LOG_LEVEL_t level)
{
mCurLevel = level;
*mCOut << getTime() << ": " << getLevelStr(level);
return mCOut;
}
std::ostream *TLogger::getCurrentErr(LOG_LEVEL_t level)
{
mCurLevel = level;
*mCErr << getTime() << ": " << getLevelStr(level);
return mCErr;
}
void TLogger::setLogfile(const string& f)
{
try
{
if (mOut)
{
cout.rdbuf(mCOutOrig);
cerr.rdbuf(mCErrOrig);
if (mOut->is_open())
mOut->close();
delete mOut;
mOut = nullptr;
}
mOut = new ofstream;
mOut->open(f, std::ios::binary | std::ios::trunc);
mCOutOrig = cout.rdbuf(mOut->rdbuf());
mCErrOrig = cerr.rdbuf(mOut->rdbuf());
}
catch (std::exception& e)
{
syslog(LOG_DAEMON | LOG_ERR, "Couldn't redirect logging to file %s!", f.c_str());
if (mOut && mOut->is_open())
mOut->close();
return;
}
mSyslog = false;
}
string TLogger::getLevelStr(LOG_LEVEL_t level)
{
switch(level)
{
case LVL_INFO: return "INFO: ";
case LVL_ERROR: return "ERROR: ";
case LVL_FATAL: return "FATAL: ";
case LVL_DEBUG: return "DEBUG: ";
case LVL_WARN: return "WARNING: ";
case LVL_NOTICE: return "NOTICE: ";
case LVL_TRACE: return "TRACE: ";
}
return string(); // should never be reached!
}
LOG_LEVEL_t TLogger::sysLevelToLogLevel(int lvl)
{
switch(lvl)
{
case LOG_INFO: return LVL_INFO;
case LOG_EMERG: return LVL_FATAL;
case LOG_ALERT: return LVL_FATAL;
case LOG_CRIT: return LVL_FATAL;
case LOG_ERR: return LVL_ERROR;
case LOG_WARNING: return LVL_WARN;
case LOG_NOTICE: return LVL_TRACE;
case LOG_DEBUG: return LVL_DEBUG;
}
return LVL_TRACE;
}
string TLogger::getTime()
{
time_t rawtime;
struct tm *timeinfo;
char buffer[80];
rawtime = time(nullptr);
timeinfo = localtime(&rawtime);
if (!timeinfo)
{
cerr << "ERROR: Couldn't get the local time!" << endl;
return string();
}
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
string str(buffer);
return str;
}
void TLogger::setLogLevel(LOG_LEVEL_t lvl)
{
if (lvl > LVL_TRACE)
{
mLogLevel = LVL_TRACE;
return;
}
if (lvl < LVL_FATAL)
{
mLogLevel = LVL_FATAL;
return;
}
mLogLevel = lvl;
}
void TLogger::incIndent()
{
mIndent++;
}
void TLogger::decIndent()
{
if (mIndent > 0)
mIndent--;
}
string TLogger::getIndent()
{
if (mIndent == 0)
return string();
stringstream s;
s << std::setw(mIndent * 2) << std::left << std::setfill(' ') << " ";
return s.str();
}
/*
* class TTracer
*
* This class is used in the start of a method or function to print out a text.
* Using this class means to create an instance of it. When the method or function
* ends, the class is also destroyed. On destroy the initial string is printed
* again.
* When printing the string, a static string is printed before the message,
* indicating the start and the end of the method. Embedded in a macro, the
* file name and line number in the file is printed also. Additionally it handles
* indentation to make it easier to read.
* This is meant for debugging purposes.
*/
TTracer::TTracer(const std::string& msg, int line, const char *file, threadID_t tid)
: mThreadID(tid)
{
if (TLogger::getLogLevel() < LVL_TRACE)
return;
lock_guard<mutex> guard(mMutexLogger); // Block the method to avoid mixing of threads
string indent = TLogger::getIndent();
mFile = file;
size_t pos = mFile.find_last_of("/");
if (pos != string::npos)
mFile = mFile.substr(pos + 1);
*TLogger::getCurrent(LVL_TRACE) << std::setw(5) << std::right << line << ", " << std::setw(20) << std::left << mFile << ", " << _threadIDtoStr(mThreadID) << " " << indent << "{entry " << msg << std::endl;
TLogger::incIndent();
mHeadMsg = msg;
mLine = line;
}
TTracer::~TTracer()
{
if (TLogger::getLogLevel() < LVL_TRACE)
return;
lock_guard<mutex> guard(mMutexLogger); // Block the method to avoid mixing of threads
TLogger::decIndent();
string indent = TLogger::getIndent();
*TLogger::getCurrent(LVL_TRACE) << " , " << std::setw(20) << std::left << mFile << ", " << _threadIDtoStr(mThreadID) << " " << indent << "}exit " << mHeadMsg << std::endl;
mHeadMsg.clear();
}
/*
* class IOLogger
*/
void IOLogger::logMsg(const string& msg, const string& str)
{
if (mLogFile.empty())
return;
string t = TLogger::getTime();
ofstream of;
string seperator = "-----------------------------------------------------------\n";
try
{
of.open(mLogFile, std::ios::out | std::ios::app);
of.write(t.c_str(), t.length());
of.write(": ", 2);
if (!str.empty())
{
of.write(str.c_str(), str.length());
of.write("\n", 1);
}
of.write(msg.c_str(), msg.length());
of.write("\n", 1);
of.write(seperator.c_str(), seperator.length());
of.close();
}
catch (std::exception &e)
{
cerr << "Error on file " << mLogFile << ": " << e.what() << endl;
if (of.is_open())
of.close();
}
}
void IOLogger::logMsg(const QJsonObject& obj, const string& msg)
{
if (mLogFile.empty())
return;
QJsonDocument jdoc;
jdoc.setObject(obj);
QByteArray json = jdoc.toJson(QJsonDocument::Compact);
logMsg(json.toStdString(), msg);
}