-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsimpleQtLogger.cpp
1107 lines (951 loc) · 33.6 KB
/
simpleQtLogger.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Simple Logger for Qt
Mario Ban, 2015-05
https://github.com/Mokolea/SimpleQtLogger
GNU Lesser General Public License v2.1
Copyright (C) 2023 Mario Ban
*/
#include "simpleQtLogger.h"
#include <QCoreApplication>
#include <QTimer>
#include <QDateTime>
#include <QFileInfo>
#include <QtDebug>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
#include <QElapsedTimer>
#endif
namespace simpleqtlogger
{
/* Log-sinks */
bool ENABLE_LOG_SINK_FILE = true;
bool ENABLE_LOG_SINK_CONSOLE = false;
bool ENABLE_LOG_SINK_QDEBUG = false;
bool ENABLE_LOG_SINK_SIGNAL = false;
bool ENABLE_LOG_SINK_SYSLOG = false;
#ifdef Q_OS_LINUX
QString NAME_LOG_SINK_SYSLOG = "";
int FACILITY_LOG_SINK_SYSLOG = LOG_USER;
#endif
/* Log-level */
EnableLogLevels::EnableLogLevels()
: logLevel_FATAL(true)
, logLevel_ERROR(true)
, logLevel_WARNING(true)
, logLevel_NOTE(true)
, logLevel_INFO(true)
, logLevel_DEBUG(false)
, logLevel_FUNCTION(false)
, logLevel_INTERNAL(true)
{}
bool EnableLogLevels::enabled(LogLevel logLevel) const
{
if (logLevel == LogLevel_FATAL) {
return logLevel_FATAL;
}
if (logLevel == LogLevel_ERROR) {
return logLevel_ERROR;
}
if (logLevel == LogLevel_WARNING) {
return logLevel_WARNING;
}
if (logLevel == LogLevel_NOTE) {
return logLevel_NOTE;
}
if (logLevel == LogLevel_INFO) {
return logLevel_INFO;
}
if (logLevel == LogLevel_DEBUG) {
return logLevel_DEBUG;
}
if (logLevel == LogLevel_FUNCTION) {
return logLevel_FUNCTION;
}
if (logLevel == LogLevel_INTERNAL) {
return logLevel_INTERNAL;
}
return false;
}
EnableLogLevels ENABLE_LOG_LEVELS;
/* Log-function stack-trace */
bool ENABLE_FUNCTION_STACK_TRACE = true;
/* Sink console color */
bool ENABLE_CONSOLE_COLOR = true;
/* Sink console trimmed messages */
bool ENABLE_CONSOLE_TRIMMED = true;
/* Console */
bool ENABLE_CONSOLE_LOG_FILE_STATE = true;
// -------------------------------------------------------------------------------------------------
Sink::Sink(QObject* parent)
: QObject(parent)
, _logFormat(DEFAULT_LOG_FORMAT)
, _logFormatInt(DEFAULT_LOG_FORMAT_INTERNAL)
{
// qDebug("Sink::Sink");
}
Sink::~Sink()
{
// qDebug("Sink::~Sink");
}
void Sink::setLogFormat(const QString& logFormat, const QString& logFormatInt)
{
// qDebug("Sink::setLogFormat");
_logFormat = logFormat;
_logFormatInt = logFormatInt;
}
void Sink::setLogLevels(const EnableLogLevels& enableLogLevels)
{
// qDebug("Sink::setLogLevels");
_enableLogLevels = enableLogLevels;
}
EnableLogLevels Sink::getLogLevels() const
{
// qDebug("Sink::getLogLevels");
return _enableLogLevels;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
bool Sink::addLogFilter(const QRegularExpression& re)
{
// qDebug("Sink::addLogFilter");
if (!re.isValid()) {
return false;
}
_reList.append(re);
return true;
}
#endif
QString Sink::getLogFormat() const
{
// qDebug("Sink::getLogFormat");
return _logFormat;
}
QString Sink::getLogFormatInt() const
{
// qDebug("Sink::getLogFormatInt");
return _logFormatInt;
}
bool Sink::checkLogLevelsEnabled(LogLevel logLevel) const
{
// qDebug("Sink::checkLogLevelsEnabled");
return _enableLogLevels.enabled(logLevel);
}
bool Sink::checkFilter(const QString& text) const
{
// qDebug("Sink::checkFilter");
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
if (_reList.isEmpty()) {
return true;
}
// C++11: for(const QRegularExpression& re : _reList) {...}
for (int i = 0; i < _reList.size(); ++i) {
QRegularExpressionMatch match = _reList[i].match(text, 0);
if (match.hasMatch()) {
return true;
}
}
return false;
#else
Q_UNUSED(text);
return true;
#endif
}
// -------------------------------------------------------------------------------------------------
SinkConsoleLog::SinkConsoleLog(QObject* parent)
: Sink(parent)
{
// qDebug("SinkConsoleLog::SinkConsoleLog");
}
SinkConsoleLog::~SinkConsoleLog()
{
// qDebug("SinkConsoleLog::~SinkConsoleLog");
}
void SinkConsoleLog::slotLog(const QString& ts, const QString& tid, const QString& text, LogLevel logLevel, const QString& functionName, const QString& fileName, unsigned int lineNumber)
{
// qDebug("SinkConsoleLog::slotLog");
if (!ENABLE_LOG_SINK_CONSOLE) {
return;
}
if (!checkLogLevelsEnabled(logLevel)) {
return;
}
if (logLevel != LogLevel_INTERNAL && !checkFilter(text)) {
return;
}
QString textIsEmpty("?");
if (logLevel == LogLevel_FUNCTION) {
textIsEmpty = "-";
}
QTextStream out(stdout);
// out.setCodec("UTF-8");
if (ENABLE_CONSOLE_COLOR) {
// set text colors (foreground/background)
if (logLevel == LogLevel_FATAL) {
out << CONSOLE_COLOR_ANSI_ESC_CODES_FATAL_I << CONSOLE_LOG_LEVEL_LABEL_FATAL << CONSOLE_COLOR_ANSI_ESC_CODES_FATAL << ": ";
}
else if (logLevel == LogLevel_ERROR) {
out << CONSOLE_COLOR_ANSI_ESC_CODES_ERROR_I << CONSOLE_LOG_LEVEL_LABEL_ERROR << CONSOLE_COLOR_ANSI_ESC_CODES_ERROR << ": ";
}
else if (logLevel == LogLevel_WARNING) {
out << CONSOLE_COLOR_ANSI_ESC_CODES_WARNING_I << CONSOLE_LOG_LEVEL_LABEL_WARNING << CONSOLE_COLOR_ANSI_ESC_CODES_WARNING << ": ";
}
else if (logLevel == LogLevel_NOTE) {
out << CONSOLE_COLOR_ANSI_ESC_CODES_NOTE_I << CONSOLE_LOG_LEVEL_LABEL_NOTE << CONSOLE_COLOR_ANSI_ESC_CODES_RESET << ": ";
}
else if (logLevel == LogLevel_DEBUG) {
out << CONSOLE_COLOR_ANSI_ESC_CODES_DEBUG_I << CONSOLE_LOG_LEVEL_LABEL_DEBUG << CONSOLE_COLOR_ANSI_ESC_CODES_DEBUG << ": ";
}
else if (logLevel == LogLevel_FUNCTION) {
out << CONSOLE_COLOR_ANSI_ESC_CODES_FUNCTION;
}
}
else {
if (logLevel == LogLevel_FATAL) {
out << CONSOLE_LOG_LEVEL_LABEL_FATAL << ": ";
}
else if (logLevel == LogLevel_ERROR) {
out << CONSOLE_LOG_LEVEL_LABEL_ERROR << ": ";
}
else if (logLevel == LogLevel_WARNING) {
out << CONSOLE_LOG_LEVEL_LABEL_WARNING << ": ";
}
else if (logLevel == LogLevel_NOTE) {
out << CONSOLE_LOG_LEVEL_LABEL_NOTE << ": ";
}
else if (logLevel == LogLevel_DEBUG) {
out << CONSOLE_LOG_LEVEL_LABEL_DEBUG << ": ";
}
}
if (logLevel == LogLevel_INTERNAL) {
out << getLogFormatInt().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed());
}
else if (logLevel == LogLevel_FUNCTION) {
out << getLogFormat().append(DEFAULT_LOG_FORMAT_CONSOLE_FUNCTION_SUFFIX).replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<FUNC>", functionName).replace("<FILE>", fileName).replace("<LINE>", QString("%1").arg(lineNumber)).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed());
}
else {
out << getLogFormat().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<FUNC>", functionName).replace("<FILE>", fileName).replace("<LINE>", QString("%1").arg(lineNumber)).replace("<TEXT>", text.isEmpty() ? textIsEmpty : ENABLE_CONSOLE_TRIMMED ? text.trimmed() : text);
}
if (ENABLE_CONSOLE_COLOR) {
if (logLevel != LogLevel_NOTE && logLevel != LogLevel_INFO && logLevel != LogLevel_INTERNAL) {
out << CONSOLE_COLOR_ANSI_ESC_CODES_RESET;
}
}
out << '\n';
}
// -------------------------------------------------------------------------------------------------
SinkQDebugLog::SinkQDebugLog(QObject* parent)
: Sink(parent)
{
// qDebug("SinkQDebugLog::SinkQDebugLog");
}
SinkQDebugLog::~SinkQDebugLog()
{
// qDebug("SinkQDebugLog::~SinkQDebugLog");
}
void SinkQDebugLog::slotLog(const QString& ts, const QString& tid, const QString& text, LogLevel logLevel, const QString& functionName, const QString& fileName, unsigned int lineNumber)
{
// qDebug("SinkQDebugLog::slotLog");
if (!ENABLE_LOG_SINK_QDEBUG) {
return;
}
if (!checkLogLevelsEnabled(logLevel)) {
return;
}
if (logLevel != LogLevel_INTERNAL && !checkFilter(text)) {
return;
}
QString textIsEmpty("?");
if (logLevel == LogLevel_FUNCTION) {
textIsEmpty = "-";
}
if (logLevel == LogLevel_INTERNAL) {
qDebug("%s", getLogFormatInt().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed()).toStdString().c_str());
}
else {
qDebug("%s", getLogFormat().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<FUNC>", functionName).replace("<FILE>", fileName).replace("<LINE>", QString("%1").arg(lineNumber)).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed()).toStdString().c_str());
}
}
// -------------------------------------------------------------------------------------------------
SinkSignalLog::SinkSignalLog(QObject* parent)
: Sink(parent)
{
// qDebug("SinkSignalLog::SinkSignalLog");
}
SinkSignalLog::~SinkSignalLog()
{
// qDebug("SinkSignalLog::~SinkSignalLog");
}
void SinkSignalLog::slotLog(const QString& ts, const QString& tid, const QString& text, LogLevel logLevel, const QString& functionName, const QString& fileName, unsigned int lineNumber)
{
// qDebug("SinkSignalLog::slotLog");
if (!ENABLE_LOG_SINK_SIGNAL) {
return;
}
if (!checkLogLevelsEnabled(logLevel)) {
return;
}
if (logLevel != LogLevel_INTERNAL && !checkFilter(text)) {
return;
}
QString textIsEmpty("?");
if (logLevel == LogLevel_FUNCTION) {
textIsEmpty = "-";
}
if (logLevel == LogLevel_INTERNAL) {
emit signalLogMessage(logLevel, getLogFormatInt().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed()));
}
else {
emit signalLogMessage(logLevel, getLogFormat().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<FUNC>", functionName).replace("<FILE>", fileName).replace("<LINE>", QString("%1").arg(lineNumber)).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed()));
}
}
// -------------------------------------------------------------------------------------------------
SinkSyslogLog::SinkSyslogLog(QObject* parent)
: Sink(parent)
{
// qDebug("SinkSyslogLog::SinkSyslogLog");
#ifdef Q_OS_LINUX
openlog(NAME_LOG_SINK_SYSLOG.isEmpty() ? NULL : NAME_LOG_SINK_SYSLOG.toStdString().c_str(), LOG_PID, FACILITY_LOG_SINK_SYSLOG);
#endif
}
SinkSyslogLog::~SinkSyslogLog()
{
// qDebug("SinkSyslogLog::~SinkSyslogLog");
#ifdef Q_OS_LINUX
closelog(); // optional
#endif
}
void SinkSyslogLog::slotLog(const QString& ts, const QString& tid, const QString& text, LogLevel logLevel, const QString& functionName, const QString& fileName, unsigned int lineNumber)
{
// qDebug("SinkSyslogLog::slotLog");
#ifdef Q_OS_LINUX
if (!ENABLE_LOG_SINK_SYSLOG) {
return;
}
if (!checkLogLevelsEnabled(logLevel)) {
return;
}
if (logLevel != LogLevel_INTERNAL && !checkFilter(text)) {
return;
}
QString textIsEmpty("?");
if (logLevel == LogLevel_FUNCTION) {
textIsEmpty = "-";
}
if (logLevel == LogLevel_INTERNAL) {
syslog(LOG_INFO, "%s", getLogFormatInt().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed()).toStdString().c_str());
}
else {
int level = LOG_INFO;
if (logLevel == LogLevel_DEBUG) {
level = LOG_DEBUG;
}
else if (logLevel == LogLevel_NOTE) {
level = LOG_NOTICE;
}
else if (logLevel == LogLevel_WARNING) {
level = LOG_WARNING;
}
else if (logLevel == LogLevel_ERROR) {
level = LOG_ERR;
}
else if (logLevel == LogLevel_FATAL) {
level = LOG_CRIT;
}
syslog(level, "%s", getLogFormat().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<FUNC>", functionName).replace("<FILE>", fileName).replace("<LINE>", QString("%1").arg(lineNumber)).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed()).toStdString().c_str());
}
#else
Q_UNUSED(ts);
Q_UNUSED(tid);
Q_UNUSED(text);
Q_UNUSED(logLevel);
Q_UNUSED(functionName);
Q_UNUSED(fileName);
Q_UNUSED(lineNumber);
#endif
}
// -------------------------------------------------------------------------------------------------
SinkFileLog::SinkFileLog(QObject* parent, const QString& role)
: Sink(parent)
, _role(role)
, _logFileRotationSize(0)
, _logFileMaxNumber(0)
, _logFile(0) // TODO use C++11 nullptr
, _logFileActivity(false)
, _startMessage(false)
{
// qDebug("SinkFileLog::SinkFileLog");
}
SinkFileLog::~SinkFileLog()
{
// qDebug("SinkFileLog::~SinkFileLog");
// check close log file
if (_logFile) {
if (_logFile->isOpen()) {
_logFile->close();
}
delete _logFile;
_logFile = 0; // TODO use C++11 nullptr
}
}
bool SinkFileLog::setLogFileName(const QString& logFileName, unsigned int logFileRotationSize, unsigned int logFileMaxNumber)
{
// qDebug("SinkFileLog::setLogFileName");
// check valid log-file name ending
if (logFileName.right(4) != ".log") {
qWarning() << "Name of log-file not ending with '.log'" << logFileName << "role" << _role;
return false;
}
_logFileName = logFileName;
_logFileRotationSize = logFileRotationSize;
_logFileMaxNumber = logFileMaxNumber;
// check valid number ranges
if (_logFileRotationSize < 100) {
_logFileRotationSize = 100;
}
if (_logFileMaxNumber < 1) {
_logFileMaxNumber = 1;
}
if (_logFileMaxNumber > 99) {
_logFileMaxNumber = 99;
}
return checkLogFileOpen();
}
void SinkFileLog::slotLog(const QString& ts, const QString& tid, const QString& text, LogLevel logLevel, const QString& functionName, const QString& fileName, unsigned int lineNumber)
{
// qDebug("SinkFileLog::slotLog");
if (!ENABLE_LOG_SINK_FILE) {
return;
}
if (!checkLogLevelsEnabled(logLevel)) {
return;
}
if (logLevel != LogLevel_INTERNAL && !checkFilter(text)) {
return;
}
QString textIsEmpty("?");
if (logLevel == LogLevel_FUNCTION) {
textIsEmpty = "-";
}
// stream (append) to log file
if (_logFile && _logFile->isOpen()) {
QTextStream out(_logFile);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
out.setEncoding(QStringConverter::Utf8); // default
#else
out.setCodec("UTF-8");
#endif
if (logLevel == LogLevel_INTERNAL) {
out << getLogFormatInt().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed()) << '\n';
}
else {
out << getLogFormat().replace("<TS>", ts).replace("<TID>", tid).replace("<TID32>", tid.right(4 * 2)).replace("<LL>", QString(LOG_LEVEL_CHAR[logLevel])).replace("<FUNC>", functionName).replace("<FILE>", fileName).replace("<LINE>", QString("%1").arg(lineNumber)).replace("<TEXT>", text.isEmpty() ? textIsEmpty : text.trimmed()) << '\n';
}
_logFileActivity = true;
}
}
bool SinkFileLog::checkLogFileOpen()
{
// qDebug("SinkFileLog::checkLogFileOpen");
// check close and open log file
if (_logFile) {
if (_logFile->isOpen()) {
_logFile->close();
}
delete _logFile;
}
// open log-file
_logFile = new QFile(_logFileName);
if (!_logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
delete _logFile;
_logFile = 0; // TODO use C++11 nullptr
qWarning() << "Open log-file failed!" << _logFileName << "role" << _role;
}
if (!_logFile) {
return false;
}
if (ENABLE_CONSOLE_LOG_FILE_STATE) {
qDebug() << "Current log-file:" << _logFileName << "role" << _role;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
QTimer::singleShot(CHECK_LOG_FILE_ACTIVITY_INTERVAL, this, &SinkFileLog::slotCheckLogFileActivity);
#else
QTimer::singleShot(CHECK_LOG_FILE_ACTIVITY_INTERVAL, this, SLOT(slotCheckLogFileActivity()));
#endif
if (!_startMessage) {
_startMessage = true;
slotLog(SimpleQtLogger::timeStamp(), SimpleQtLogger::threadId(), QString("Start file-log '%1'").arg(_role), LogLevel_INTERNAL, "", "", 0);
}
return true;
}
void SinkFileLog::checkLogFileRolling()
{
// qDebug("SinkFileLog::checkLogFileRolling");
if (!_logFile) {
return;
}
// check current log-file size
QFileInfo logFileInfo(*_logFile);
qint64 logFileSize = logFileInfo.size();
if (logFileSize < _logFileRotationSize) {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
QTimer::singleShot(CHECK_LOG_FILE_ACTIVITY_INTERVAL, this, &SinkFileLog::slotCheckLogFileActivity);
#else
QTimer::singleShot(CHECK_LOG_FILE_ACTIVITY_INTERVAL, this, SLOT(slotCheckLogFileActivity()));
#endif
return;
}
slotLog(SimpleQtLogger::timeStamp(), SimpleQtLogger::threadId(), QString("Current log-file '%1' size=%2 (rotation-size=%3) --> rolling").arg(_role).arg(logFileSize).arg(_logFileRotationSize), LogLevel_INTERNAL, "", "", 0);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
QElapsedTimer timeRolling;
#else
QTime timeRolling;
#endif
timeRolling.start();
// handle file rolling
// delete last file
QString logFileName = _logFileName;
logFileName.replace(".log", QString("_%1.log").arg(_logFileMaxNumber, 2, 10, QLatin1Char('0')));
if (QFile::exists(logFileName)) {
if (QFile::remove(logFileName)) {
if (ENABLE_CONSOLE_LOG_FILE_STATE) {
qDebug() << "Removed" << logFileName << "role" << _role;
}
}
else {
qWarning() << "ERROR: Remove" << logFileName << "role" << _role;
}
}
// rolling files
for (int i = _logFileMaxNumber - 1; i > 0; --i) {
QString logFileNameFrom = _logFileName;
logFileNameFrom.replace(".log", QString("_%1.log").arg(i, 2, 10, QLatin1Char('0')));
QString logFileNameTo = _logFileName;
logFileNameTo.replace(".log", QString("_%1.log").arg(i + 1, 2, 10, QLatin1Char('0')));
if (QFile::exists(logFileNameFrom)) {
if (QFile::rename(logFileNameFrom, logFileNameTo)) {
if (ENABLE_CONSOLE_LOG_FILE_STATE) {
qDebug() << "Moved" << logFileNameFrom << "to" << logFileNameTo << "role" << _role;
}
}
else {
qWarning() << "ERROR: Move" << logFileNameFrom << "to" << logFileNameTo << "role" << _role;
}
}
}
_logFile->close();
// move first file
QString logFileNameTo = _logFileName;
logFileNameTo.replace(".log", QString("_%1.log").arg(1, 2, 10, QLatin1Char('0')));
if (QFile::exists(_logFileName)) {
if (QFile::rename(_logFileName, logFileNameTo)) {
if (ENABLE_CONSOLE_LOG_FILE_STATE) {
qDebug() << "Moved" << _logFileName << "to" << logFileNameTo << "role" << _role;
}
}
else {
qWarning() << "ERROR: Move" << _logFileName << "to" << logFileNameTo << "role" << _role;
}
}
checkLogFileOpen();
slotLog(SimpleQtLogger::timeStamp(), SimpleQtLogger::threadId(), QString("Log-file '%1' rolling done (time elapsed: %2 ms)").arg(_role).arg(timeRolling.elapsed()), LogLevel_INTERNAL, "", "", 0);
}
void SinkFileLog::slotCheckLogFileActivity()
{
// qDebug("SinkFileLog::slotCheckLogFileActivity");
if (!_logFile) {
return;
}
if (_logFileActivity) {
_logFileActivity = false;
checkLogFileRolling();
return;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 4, 0))
QTimer::singleShot(CHECK_LOG_FILE_ACTIVITY_INTERVAL, this, &SinkFileLog::slotCheckLogFileActivity);
#else
QTimer::singleShot(CHECK_LOG_FILE_ACTIVITY_INTERVAL, this, SLOT(slotCheckLogFileActivity()));
#endif
}
// -------------------------------------------------------------------------------------------------
SimpleQtLogger* SimpleQtLogger::instance = 0; // TODO use C++11 nullptr
/*static*/ SimpleQtLogger* SimpleQtLogger::createInstance(QObject* parent)
{
if (instance) {
delete instance;
}
instance = new SimpleQtLogger(parent);
return instance;
}
/*static*/ SimpleQtLogger* SimpleQtLogger::getInstance()
{
return instance;
}
SimpleQtLogger::SimpleQtLogger(QObject* parent)
: QObject(parent)
{
// qDebug("SimpleQtLogger::SimpleQtLogger");
qRegisterMetaType<LogLevel>("LogLevel"); // to use type in Qt::QueuedConnection
_sinkConsoleLog = new SinkConsoleLog(this);
_sinkQDebugLog = new SinkQDebugLog(this);
_sinkSignalLog = new SinkSignalLog(this);
#ifdef Q_OS_LINUX
_sinkSyslogLog = new SinkSyslogLog(this);
#endif
_sinkConsoleLog->setLogFormat(DEFAULT_LOG_FORMAT_CONSOLE, DEFAULT_LOG_FORMAT_INTERNAL);
#ifdef Q_OS_LINUX
_sinkSyslogLog->setLogFormat(DEFAULT_LOG_FORMAT_SYSLOG, DEFAULT_LOG_FORMAT_SYSLOG);
#endif
// Qt::ConnectionType is Qt::AutoConnection (Default)
// If the receiver lives in the thread that emits the signal, Qt::DirectConnection is used.
// Otherwise, Qt::QueuedConnection is used. The connection type is determined when the signal is emitted.
#if ENABLE_SQTL_LOG_SINK_CONSOLE > 0
QObject::connect(this, SIGNAL(signalLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)),
_sinkConsoleLog, SLOT(slotLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)));
#endif
#if ENABLE_SQTL_LOG_SINK_QDEBUG > 0
QObject::connect(this, SIGNAL(signalLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)),
_sinkQDebugLog, SLOT(slotLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)));
#endif
#if ENABLE_SQTL_LOG_SINK_SIGNAL > 0
QObject::connect(this, SIGNAL(signalLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)),
_sinkSignalLog, SLOT(slotLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)));
#endif
#if ENABLE_SQTL_LOG_SINK_SYSLOG > 0
#ifdef Q_OS_LINUX
QObject::connect(this, SIGNAL(signalLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)),
_sinkSyslogLog, SLOT(slotLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)));
#endif
#endif
addSinkFileLog("main");
}
SimpleQtLogger::~SimpleQtLogger()
{
// qDebug("SimpleQtLogger::~SimpleQtLogger");
}
void SimpleQtLogger::addSinkFileLog(const QString& role)
{
// qDebug("SimpleQtLogger::addSinkFileLog");
if (_sinkFileLogMap.contains(role)) {
return;
}
_sinkFileLogMap[role] = new SinkFileLog(this, role);
#if ENABLE_SQTL_LOG_SINK_FILE > 0
QObject::connect(this, SIGNAL(signalLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)),
_sinkFileLogMap[role], SLOT(slotLog(const QString&, const QString&, const QString&, LogLevel, const QString&, const QString&, unsigned int)));
#endif
}
void SimpleQtLogger::setLogFormat_file(const QString& logFormat, const QString& logFormatInt)
{
// qDebug("SimpleQtLogger::setLogFormat_file");
setLogFormat_file("main", logFormat, logFormatInt);
}
void SimpleQtLogger::setLogFormat_file(const QString& role, const QString& logFormat, const QString& logFormatInt)
{
// qDebug("SimpleQtLogger::setLogFormat_file");
if (_sinkFileLogMap.contains(role)) {
_sinkFileLogMap[role]->setLogFormat(logFormat, logFormatInt);
}
}
void SimpleQtLogger::setLogFormat_console(const QString& logFormat, const QString& logFormatInt)
{
// qDebug("SimpleQtLogger::setLogFormat_console");
_sinkConsoleLog->setLogFormat(logFormat, logFormatInt);
}
void SimpleQtLogger::setLogFormat_qDebug(const QString& logFormat, const QString& logFormatInt)
{
// qDebug("SimpleQtLogger::setLogFormat_qDebug");
_sinkQDebugLog->setLogFormat(logFormat, logFormatInt);
}
void SimpleQtLogger::setLogFormat_signal(const QString& logFormat, const QString& logFormatInt)
{
// qDebug("SimpleQtLogger::setLogFormat_signal");
_sinkSignalLog->setLogFormat(logFormat, logFormatInt);
}
void SimpleQtLogger::setLogFormat_syslog(const QString& logFormat, const QString& logFormatInt)
{
// qDebug("SimpleQtLogger::setLogFormat_syslog");
#ifdef Q_OS_LINUX
_sinkSyslogLog->setLogFormat(logFormat, logFormatInt);
#else
Q_UNUSED(logFormat);
Q_UNUSED(logFormatInt);
#endif
}
void SimpleQtLogger::setLogLevels_file(const EnableLogLevels& enableLogLevels)
{
// qDebug("SimpleQtLogger::setLogLevels_file");
setLogLevels_file("main", enableLogLevels);
}
void SimpleQtLogger::setLogLevels_file(const QString& role, const EnableLogLevels& enableLogLevels)
{
// qDebug("SimpleQtLogger::setLogLevels_file");
if (_sinkFileLogMap.contains(role)) {
_sinkFileLogMap[role]->setLogLevels(enableLogLevels);
}
}
void SimpleQtLogger::setLogLevels_console(const EnableLogLevels& enableLogLevels)
{
// qDebug("SimpleQtLogger::setLogLevels_console");
_sinkConsoleLog->setLogLevels(enableLogLevels);
}
void SimpleQtLogger::setLogLevels_qDebug(const EnableLogLevels& enableLogLevels)
{
// qDebug("SimpleQtLogger::setLogLevels_qDebug");
_sinkQDebugLog->setLogLevels(enableLogLevels);
}
void SimpleQtLogger::setLogLevels_signal(const EnableLogLevels& enableLogLevels)
{
// qDebug("SimpleQtLogger::setLogLevels_signal");
_sinkSignalLog->setLogLevels(enableLogLevels);
}
void SimpleQtLogger::setLogLevels_syslog(const EnableLogLevels& enableLogLevels)
{
// qDebug("SimpleQtLogger::setLogLevels_syslog");
#ifdef Q_OS_LINUX
_sinkSyslogLog->setLogLevels(enableLogLevels);
#else
Q_UNUSED(enableLogLevels);
#endif
}
EnableLogLevels SimpleQtLogger::getLogLevels_file() const // main
{
// qDebug("SimpleQtLogger::getLogLevels_file");
return getLogLevels_file("main");
}
EnableLogLevels SimpleQtLogger::getLogLevels_file(const QString& role) const
{
// qDebug("SimpleQtLogger::getLogLevels_file");
if (_sinkFileLogMap.contains(role)) {
return _sinkFileLogMap[role]->getLogLevels();
}
else {
return ENABLE_LOG_LEVELS;
}
}
EnableLogLevels SimpleQtLogger::getLogLevels_console() const
{
// qDebug("SimpleQtLogger::getLogLevels_console");
return _sinkConsoleLog->getLogLevels();
}
EnableLogLevels SimpleQtLogger::getLogLevels_qDebug() const
{
// qDebug("SimpleQtLogger::getLogLevels_qDebug");
return _sinkQDebugLog->getLogLevels();
}
EnableLogLevels SimpleQtLogger::getLogLevels_signal() const
{
// qDebug("SimpleQtLogger::getLogLevels_signal");
return _sinkSignalLog->getLogLevels();
}
EnableLogLevels SimpleQtLogger::getLogLevels_syslog() const
{
// qDebug("SimpleQtLogger::getLogLevels_syslog");
#ifdef Q_OS_LINUX
return _sinkSyslogLog->getLogLevels();
#else
return ENABLE_LOG_LEVELS;
#endif
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
bool SimpleQtLogger::addLogFilter_file(const QRegularExpression& re)
{
// qDebug("SimpleQtLogger::addLogFilter_file");
return addLogFilter_file("main", re);
}
bool SimpleQtLogger::addLogFilter_file(const QString& role, const QRegularExpression& re)
{
// qDebug("SimpleQtLogger::addLogFilter_file");
if (!re.isValid()) {
return false;
}
if (_sinkFileLogMap.contains(role)) {
return _sinkFileLogMap[role]->addLogFilter(re);
}
return false;
}
bool SimpleQtLogger::addLogFilter_console(const QRegularExpression& re)
{
// qDebug("SimpleQtLogger::addLogFilter_console");
if (!re.isValid()) {
return false;
}
return _sinkConsoleLog->addLogFilter(re);
}
bool SimpleQtLogger::addLogFilter_qDebug(const QRegularExpression& re)
{
// qDebug("SimpleQtLogger::addLogFilter_qDebug");
if (!re.isValid()) {
return false;
}
return _sinkQDebugLog->addLogFilter(re);
}
bool SimpleQtLogger::addLogFilter_signal(const QRegularExpression& re)
{
// qDebug("SimpleQtLogger::addLogFilter_signal");
if (!re.isValid()) {
return false;
}
return _sinkSignalLog->addLogFilter(re);
}
bool SimpleQtLogger::addLogFilter_syslog(const QRegularExpression& re)
{
// qDebug("SimpleQtLogger::addLogFilter_syslog");
#ifdef Q_OS_LINUX
if (!re.isValid()) {
return false;
}
return _sinkSyslogLog->addLogFilter(re);
#else
Q_UNUSED(re);
return false;
#endif
}
#endif
bool SimpleQtLogger::setLogFileName(const QString& logFileName, unsigned int logFileRotationSize, unsigned int logFileMaxNumber)
{
// qDebug("SimpleQtLogger::setLogFileName");
return setLogFileName("main", logFileName, logFileRotationSize, logFileMaxNumber);
}
bool SimpleQtLogger::setLogFileName(const QString& role, const QString& logFileName, unsigned int logFileRotationSize, unsigned int logFileMaxNumber)
{
// qDebug("SimpleQtLogger::setLogFileName");
if (_sinkFileLogMap.contains(role) && _sinkFileLogMap[role]->setLogFileName(logFileName, logFileRotationSize, logFileMaxNumber)) {
// log(QString("Start file-log '%1'").arg(role), LogLevel_INTERNAL, "", "", 0);
return true;
}
return false;
}
bool SimpleQtLogger::connectSinkSignalLog(const QObject* receiver, const char* method, Qt::ConnectionType type)
{
// qDebug("SimpleQtLogger::connectSinkSignalLog");
return QObject::connect(_sinkSignalLog, SIGNAL(signalLogMessage(simpleqtlogger::LogLevel, const QString&)), receiver, method, type);
}
/*static*/ QString SimpleQtLogger::timeStamp()
{
// qDebug("SimpleQtLogger::timeStamp");
// time-stamp
QDateTime dateTime = QDateTime::currentDateTime(); // or better use QDateTime::currentDateTimeUtc() instead
return dateTime.toString("yyyy-MM-dd hh:mm:ss.zzz");
}
/*static*/ QString SimpleQtLogger::threadId()
{
// qDebug("SimpleQtLogger::threadId");
// thread-id in hexadecimal
return QString("%1").arg((unsigned long long int)QThread::currentThreadId(), 16, 16, QLatin1Char('0')); // field-with for 64bit
}
/*static*/ QString SimpleQtLogger::toHexdump(const QByteArray& ba)
{
// qDebug("SimpleQtLogger::toHexdump");
QString hexdump(QString("Size: %1").arg(ba.size()));
QString text;
int offset = 0;
for (int i = 0; i < ba.size(); ++i) {
if (i % 16 == 0) {
hexdump.append(QString("\n%1 ").arg(i, 8, 16, QLatin1Char('0')));
offset = 0;
}
if (offset == 8) {
hexdump.append(" ");
text.append(" ");
}
hexdump.append(QString(" %1").arg((unsigned char)ba[i], 2, 16, QLatin1Char('0')));
if (ba[i] < 32 || ba[i] > 126) {
text.append('.');
}
else {
text.append(ba[i]);
}
++offset;
if (offset >= 16) {
hexdump.append(QString(" %1").arg(text));
text.clear();
}
// handle remaining
if (i + 1 >= ba.size() && offset <= 15) {