-
Notifications
You must be signed in to change notification settings - Fork 0
/
qxtlogger.cpp
1132 lines (1013 loc) · 39.7 KB
/
qxtlogger.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
/****************************************************************************
** Copyright (c) 2006 - 2011, the LibQxt project.
** See the Qxt AUTHORS file for a list of authors and copyright holders.
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** * Neither the name of the LibQxt project nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
** <http://libqxt.org> <[email protected]>
*****************************************************************************/
/*!
\class QxtLogger
\brief The QxtLogger class is an easy to use, easy to extend logging tool.
\inmodule QxtCore
\section1 Overview
QxtLogger is an easy to use, easy to extend, thread-safe logging tool. It was designed to be used "out of the box".
\code
#include <QxtLogger>
...
QxtLogger::getInstance()->debug("Hi!"); // without using the macro
qxtLog->debug("Hi!"); // using the macro
\endcode
\sa getInstance()
\section1 Usage
QxtLogger is designed to work "out of the box". The Logger itself is a singleton object that manages all of the logging
that is requested. It provides 8 methods to actually log content; they are listed from the most logically verbose to the
most logically important:
\list
\o trace()
\o debug()
\o info()
\o warning()
\o error()
\o critical()
\o fatal()
\o write()
\endlist
These named members only have meaning to the person who uses them. For example, you could call qxtLog->trace() from
many parts of a complicated, massively recursive function to trace it's output; use qxtLog->info() to log that
an event such as "Logging has started" has happened; use qxtLog->fatal() when an unhandled exception is thrown. Or,
you could use qxtLog->write() for everything.
Each of these members comes in two forms: the first takes up to ten QVariants (for moc compatibility), the second
form takes a QList<QVariant>. Thus, you can invoke the info() member in the following ways:
\code
// Using the 10-param members.
qxtLog->info(15);
qxtLog->info("I am a test");
qxtLog->info(QTime::currentTime(), "something happened", 3.14);
// Now with QList<QVariant>
qxtLog->info(QList<QVariant>() << "test" << 15 << QTime::currentTime());
\endcode
The real power behind QxtLogger comes from telling it which log levels you actually want to see. Calling qxtLog->enableAllLogLevels()
can give you a lot of data if you need it. But if you only want to see warnings and errors, qxtLog->setMinimumLogLevel(WarningLevel) might
be more useful.
\section1 Extending
The functionality of QxtLogger can be extended by creating plugins derived from QxtLoggerEngine. Logger Engines
are the little workers that actually take the raw data, format it, and spit it out into meaningful forms.
*/
/*!
\macro qxtLog
\relates QxtLogger
A global pointer referring to the unique logger object. It is
equivalent to the pointer returned by the QxtLogger::instance() function.
\sa QxtLogger::getInstance()
*/
/*!
\fn void QxtLogger::loggerEngineAdded(const QString& engineName)
This signal is emitted when an engine with \a engineName has been added.
\sa loggerEngineRemoved()
*/
/*!
\fn void QxtLogger::loggerEngineRemoved(const QString& engineName)
This signal is emitted when an engine with \a engineName has been removed.
\sa loggerEngineAdded()
*/
/*!
\fn void QxtLogger::loggerEngineEnabled(const QString& engineName)
This signal is emitted when an engine with \a engineName has been enabled.
\sa loggerEngineDisabled()
*/
/*!
\fn void QxtLogger::loggerEngineDisabled(const QString& engineName)
This signal is emitted when an engine with \a engineName has been disabled.
\sa loggerEngineEnabled()
*/
#include "qxtlogger.h"
#include "qxtlogger_p.h"
#include "qxtlogstream.h"
#include "qxtbasicstdloggerengine.h"
#include <QtDebug>
#include <QMutex>
#include <QMutexLocker>
#include <QtCore>
#include <functional>
/*******************************************************************************
Constructor for QxtLogger's private data
*******************************************************************************/
QxtLoggerPrivate::QxtLoggerPrivate()
{
mut_lock = new QMutex(QMutex::Recursive);
}
/*******************************************************************************
Destructor for QxtLogger's private data
*******************************************************************************/
QxtLoggerPrivate::~QxtLoggerPrivate()
{
Q_FOREACH(QxtLoggerEngine *eng, map_logEngineMap)
{
if (eng)
{
eng->killLoggerEngine();
delete eng;
}
}
delete mut_lock;
mut_lock = NULL;
}
void QxtLoggerPrivate::log(const QString &logTarget, const QList<QVariant>& msgList)
{
static std::function< void(QxtLoggerEngine*) > lf= [&logTarget,&msgList](QxtLoggerEngine *eng) ->void{
if (eng && eng->isInitialized() && eng->isLoggingEnabled() && eng->isLogLevelEnabled(logTarget))
{
eng->writeFormatted(level, msgList);
}
};
QtConcurrent::map(map_logEngineMap, lf);
}
void QxtLoggerPrivate::setQxtLoggerEngineMinimumLevel(QxtLoggerEngine *eng, QxtLogger::LogLevel level)
{
QMutexLocker lock(mut_lock);
if (!eng) return;
(QxtLogger::TraceLevel < level) ? eng->disableLogLevels(QxtLogger::TraceLevel) : eng->enableLogLevels(QxtLogger::TraceLevel);
(QxtLogger::DebugLevel < level) ? eng->disableLogLevels(QxtLogger::DebugLevel) : eng->enableLogLevels(QxtLogger::DebugLevel);
(QxtLogger::InfoLevel < level) ? eng->disableLogLevels(QxtLogger::InfoLevel) : eng->enableLogLevels(QxtLogger::InfoLevel);
(QxtLogger::WarningLevel < level) ? eng->disableLogLevels(QxtLogger::WarningLevel) : eng->enableLogLevels(QxtLogger::WarningLevel);
(QxtLogger::ErrorLevel < level) ? eng->disableLogLevels(QxtLogger::ErrorLevel) : eng->enableLogLevels(QxtLogger::ErrorLevel);
(QxtLogger::CriticalLevel < level) ? eng->disableLogLevels(QxtLogger::CriticalLevel) : eng->enableLogLevels(QxtLogger::CriticalLevel);
(QxtLogger::FatalLevel < level) ? eng->disableLogLevels(QxtLogger::FatalLevel) : eng->enableLogLevels(QxtLogger::FatalLevel);
(QxtLogger::WriteLevel < level) ? eng->disableLogLevels(QxtLogger::WriteLevel) : eng->enableLogLevels(QxtLogger::WriteLevel);
}
/*! \brief Returns the named Engine.
*/
QxtLoggerEngine *QxtLogger::engine(const QString &engineName)
{
if (! isLoggerEngine(engineName)) return 0;
else return qxt_d().map_logEngineMap.value(engineName);
}
/*! \brief Opens a stream to write a message to all Engines with the InfoLevel set.
The parameterless logging functions return a QxtLogStream for use similar to qDebug().
\code
qxtLog->info() << "informational message";
\endcode
*/
QxtLogStream QxtLogger::info()
{
return stream(QxtLogger::InfoLevel);
}
/*! \brief Opens a stream to write a message to all Engines with the TraceLevel set.
The parameterless logging functions return a QxtLogStream for use similar to qDebug().
\code
qxtLog->trace() << "detailed trace message";
\endcode
*/
QxtLogStream QxtLogger::trace()
{
return stream(QxtLogger::TraceLevel);
}
/*! \brief Opens a stream to write a message to all Engines with the ErrorLevel set.
The parameterless logging functions return a QxtLogStream for use similar to qDebug().
\code
qxtLog->error() << "error message";
\endcode
*/
QxtLogStream QxtLogger::error()
{
return stream(QxtLogger::ErrorLevel);
}
/*! \brief Opens a stream to write a message to all Engines with the WarningLevel set.
The parameterless logging functions return a QxtLogStream for use similar to qDebug().
\code
qxtLog->warning() << "warning message";
\endcode
*/
QxtLogStream QxtLogger::warning()
{
return stream(QxtLogger::WarningLevel);
}
/*! \brief Opens a stream to write a message to all Engines with the DebugLevel set.
The parameterless logging functions return a QxtLogStream for use similar to qDebug().
\code
qxtLog->debug() << "debugging log message";
\endcode
*/
QxtLogStream QxtLogger::debug()
{
return stream(QxtLogger::DebugLevel);
}
/*! \brief Opens a stream to write a message to all Engines with the CriticalLevel set.
The parameterless logging functions return a QxtLogStream for use similar to qDebug().
\code
qxtLog->critical() << "critical error message";
\endcode
*/
QxtLogStream QxtLogger::critical()
{
return stream(QxtLogger::CriticalLevel);
}
/*! \brief Opens a stream to write a message to all Engines with the FatalLevel set.
The parameterless logging functions return a QxtLogStream for use similar to qDebug().
\code
qxtLog->fatal() << "fatal error message";
\endcode
*/
QxtLogStream QxtLogger::fatal()
{
return stream(QxtLogger::FatalLevel);
}
/*! \brief Opens a stream to write a message to all Engines with the WriteLevel set.
The parameterless logging functions return a QxtLogStream for use similar to qDebug().
\code
qxtLog->write() << "log message";
\endcode
*/
QxtLogStream QxtLogger::write()
{
return stream(QxtLogger::WriteLevel);
}
/*! \brief Writes a message to all Engines with the InfoLevel set.
The 10-parameter logging functions are designed to be used with Qt's Signals and Slots, since moc
currently only accepts functions with up to 10 parameters. They can take any value that
QVariant can take as an argument.
*/
void QxtLogger::info(const QVariant &message, const QVariant &msg1, const QVariant &msg2, const QVariant &msg3, const QVariant &msg4, const QVariant &msg5, const QVariant &msg6, const QVariant &msg7, const QVariant &msg8 , const QVariant &msg9)
{
QMutexLocker lock(qxt_d().mut_lock);
QList<QVariant> args;
args.push_back(message);
if (!msg1.isNull()) args.push_back(msg1);
if (!msg2.isNull()) args.push_back(msg2);
if (!msg3.isNull()) args.push_back(msg3);
if (!msg4.isNull()) args.push_back(msg4);
if (!msg5.isNull()) args.push_back(msg5);
if (!msg6.isNull()) args.push_back(msg6);
if (!msg7.isNull()) args.push_back(msg7);
if (!msg8.isNull()) args.push_back(msg8);
if (!msg9.isNull()) args.push_back(msg9);
info(args);
}
/*! \brief Writes a message to all Engines with the TraceLevel set.
The 10-parameter logging functions are designed to be used with Qt's Signals and Slots, since moc
currently only accepts functions with up to 10 parameters. They can take any value that
QVariant can take as an argument.
*/
void QxtLogger::trace(const QVariant &message, const QVariant &msg1 , const QVariant &msg2 , const QVariant &msg3 , const QVariant &msg4 , const QVariant &msg5 , const QVariant &msg6 , const QVariant &msg7 , const QVariant &msg8 , const QVariant &msg9)
{
QMutexLocker lock(qxt_d().mut_lock);
QList<QVariant> args;
args.push_back(message);
if (!msg1.isNull()) args.push_back(msg1);
if (!msg2.isNull()) args.push_back(msg2);
if (!msg3.isNull()) args.push_back(msg3);
if (!msg4.isNull()) args.push_back(msg4);
if (!msg5.isNull()) args.push_back(msg5);
if (!msg6.isNull()) args.push_back(msg6);
if (!msg7.isNull()) args.push_back(msg7);
if (!msg8.isNull()) args.push_back(msg8);
if (!msg9.isNull()) args.push_back(msg9);
trace(args);
}
/*! \brief Writes a message to all Engines with the WarningLevel set.
The 10-parameter logging functions are designed to be used with Qt's Signals and Slots, since moc
currently only accepts functions with up to 10 parameters. They can take any value that
QVariant can take as an argument.
*/
void QxtLogger::warning(const QVariant &message, const QVariant &msg1 , const QVariant &msg2 , const QVariant &msg3 , const QVariant &msg4 , const QVariant &msg5 , const QVariant &msg6 , const QVariant &msg7 , const QVariant &msg8 , const QVariant &msg9)
{
QMutexLocker lock(qxt_d().mut_lock);
QList<QVariant> args;
args.push_back(message);
if (!msg1.isNull()) args.push_back(msg1);
if (!msg2.isNull()) args.push_back(msg2);
if (!msg3.isNull()) args.push_back(msg3);
if (!msg4.isNull()) args.push_back(msg4);
if (!msg5.isNull()) args.push_back(msg5);
if (!msg6.isNull()) args.push_back(msg6);
if (!msg7.isNull()) args.push_back(msg7);
if (!msg8.isNull()) args.push_back(msg8);
if (!msg9.isNull()) args.push_back(msg9);
warning(args);
}
/*! \brief Writes a message to all Engines with the ErrorLevel set.
The 10-parameter logging functions are designed to be used with Qt's Signals and Slots, since moc
currently only accepts functions with up to 10 parameters. They can take any value that
QVariant can take as an argument.
*/
void QxtLogger::error(const QVariant &message, const QVariant &msg1 , const QVariant &msg2 , const QVariant &msg3 , const QVariant &msg4 , const QVariant &msg5 , const QVariant &msg6 , const QVariant &msg7 , const QVariant &msg8 , const QVariant &msg9)
{
QMutexLocker lock(qxt_d().mut_lock);
QList<QVariant> args;
args.push_back(message);
if (!msg1.isNull()) args.push_back(msg1);
if (!msg2.isNull()) args.push_back(msg2);
if (!msg3.isNull()) args.push_back(msg3);
if (!msg4.isNull()) args.push_back(msg4);
if (!msg5.isNull()) args.push_back(msg5);
if (!msg6.isNull()) args.push_back(msg6);
if (!msg7.isNull()) args.push_back(msg7);
if (!msg8.isNull()) args.push_back(msg8);
if (!msg9.isNull()) args.push_back(msg9);
error(args);
}
/*! \brief Writes a message to all Engines with the DebugLevel set.
The 10-parameter logging functions are designed to be used with Qt's Signals and Slots, since moc
currently only accepts functions with up to 10 parameters. They can take any value that
QVariant can take as an argument.
*/
void QxtLogger::debug(const QVariant &message, const QVariant &msg1 , const QVariant &msg2 , const QVariant &msg3 , const QVariant &msg4 , const QVariant &msg5 , const QVariant &msg6 , const QVariant &msg7 , const QVariant &msg8 , const QVariant &msg9)
{
QMutexLocker lock(qxt_d().mut_lock);
QList<QVariant> args;
args.push_back(message);
if (!msg1.isNull()) args.push_back(msg1);
if (!msg2.isNull()) args.push_back(msg2);
if (!msg3.isNull()) args.push_back(msg3);
if (!msg4.isNull()) args.push_back(msg4);
if (!msg5.isNull()) args.push_back(msg5);
if (!msg6.isNull()) args.push_back(msg6);
if (!msg7.isNull()) args.push_back(msg7);
if (!msg8.isNull()) args.push_back(msg8);
if (!msg9.isNull()) args.push_back(msg9);
debug(args);
}
/*! \brief Writes a message to all Engines with the WriteLevel set.
The 10-parameter logging functions are designed to be used with Qt's Signals and Slots, since moc
currently only accepts functions with up to 10 parameters. They can take any value that
QVariant can take as an argument.
*/
void QxtLogger::write(const QVariant &message, const QVariant &msg1 , const QVariant &msg2, const QVariant &msg3 , const QVariant &msg4 , const QVariant &msg5 , const QVariant &msg6 , const QVariant &msg7 , const QVariant &msg8 , const QVariant &msg9)
{
QMutexLocker lock(qxt_d().mut_lock);
QList<QVariant> args;
args.push_back(message);
if (!msg1.isNull()) args.push_back(msg1);
if (!msg2.isNull()) args.push_back(msg2);
if (!msg3.isNull()) args.push_back(msg3);
if (!msg4.isNull()) args.push_back(msg4);
if (!msg5.isNull()) args.push_back(msg5);
if (!msg6.isNull()) args.push_back(msg6);
if (!msg7.isNull()) args.push_back(msg7);
if (!msg8.isNull()) args.push_back(msg8);
if (!msg9.isNull()) args.push_back(msg9);
write(args);
}
/*! \brief Writes a message to all Engines with the CriticalLevel set.
The 10-parameter logging functions are designed to be used with Qt's Signals and Slots, since moc
currently only accepts functions with up to 10 parameters. They can take any value that
QVariant can take as an argument.
*/
void QxtLogger::critical(const QVariant &message, const QVariant &msg1 , const QVariant &msg2 , const QVariant &msg3 , const QVariant &msg4 , const QVariant &msg5 , const QVariant &msg6 , const QVariant &msg7 , const QVariant &msg8 , const QVariant &msg9)
{
QMutexLocker lock(qxt_d().mut_lock);
QList<QVariant> args;
args.push_back(message);
if (!msg1.isNull()) args.push_back(msg1);
if (!msg2.isNull()) args.push_back(msg2);
if (!msg3.isNull()) args.push_back(msg3);
if (!msg4.isNull()) args.push_back(msg4);
if (!msg5.isNull()) args.push_back(msg5);
if (!msg6.isNull()) args.push_back(msg6);
if (!msg7.isNull()) args.push_back(msg7);
if (!msg8.isNull()) args.push_back(msg8);
if (!msg9.isNull()) args.push_back(msg9);
critical(args);
}
/*! \brief Writes a message to all Engines with the FatalLevel set.
The 10-parameter logging functions are designed to be used with Qt's Signals and Slots, since moc
currently only accepts functions with up to 10 parameters. They can take any value that
QVariant can take as an argument.
*/
void QxtLogger::fatal(const QVariant &message, const QVariant &msg1 , const QVariant &msg2 , const QVariant &msg3 , const QVariant &msg4 , const QVariant &msg5 , const QVariant &msg6 , const QVariant &msg7 , const QVariant &msg8 , const QVariant &msg9)
{
QMutexLocker lock(qxt_d().mut_lock);
QList<QVariant> args;
args.push_back(message);
if (!msg1.isNull()) args.push_back(msg1);
if (!msg2.isNull()) args.push_back(msg2);
if (!msg3.isNull()) args.push_back(msg3);
if (!msg4.isNull()) args.push_back(msg4);
if (!msg5.isNull()) args.push_back(msg5);
if (!msg6.isNull()) args.push_back(msg6);
if (!msg7.isNull()) args.push_back(msg7);
if (!msg8.isNull()) args.push_back(msg8);
if (!msg9.isNull()) args.push_back(msg9);
fatal(args);
}
/*! \brief Writes a message to all Engines with the InfoLevel set.
The 1-parameter logging messages can take any number of arguments in the
form of a QList<QVariant>, or QList<QVariant>.
*/
void QxtLogger::info(const QList<QVariant> &args)
{
log(QxtLogger::InfoLevel, args);
}
/*! \brief Writes a message to all Engines with the TraceLevel set.
The 1-parameter logging messages can take any number of arguments in the
form of a QList<QVariant>, or QList<QVariant>.
*/
void QxtLogger::trace(const QList<QVariant> &args)
{
log(QxtLogger::TraceLevel, args);
}
/*! \brief Writes a message to all Engines with the WarningLevel set.
The 1-parameter logging messages can take any number of arguments in the
form of a QList<QVariant>, or QList<QVariant>.
*/
void QxtLogger::warning(const QList<QVariant> &args)
{
log(QxtLogger::WarningLevel, args);
}
/*! \brief Writes a message to all Engines with the ErrorLevel set.
The 1-parameter logging messages can take any number of arguments in the
form of a QList<QVariant>, or QList<QVariant>.
*/
void QxtLogger::error(const QList<QVariant> &args)
{
log(QxtLogger::ErrorLevel, args);
}
/*! \brief Writes a message to all Engines with the DebugLevel set.
The 1-parameter logging messages can take any number of arguments in the
form of a QList<QVariant>, or QList<QVariant>.
*/
void QxtLogger::debug(const QList<QVariant> &args)
{
log(QxtLogger::DebugLevel, args);
}
/*! \brief Writes a message to all Engines with the CriticalLevel set.
The 1-parameter logging messages can take any number of arguments in the
form of a QList<QVariant>, or QList<QVariant>.
*/
void QxtLogger::critical(const QList<QVariant> &args)
{
log(QxtLogger::CriticalLevel, args);
}
/*! \brief Writes a message to all Engines with the FatalLevel set.
The 1-parameter logging messages can take any number of arguments in the
form of a QList<QVariant>, or QList<QVariant>.
*/
void QxtLogger::fatal(const QList<QVariant> &args)
{
log(QxtLogger::FatalLevel, args);
}
/*! \brief Writes a message to all Engines with the WriteLevel set.
The 1-parameter logging messages can take any number of arguments in the
form of a QList<QVariant>, or QList<QVariant>.
*/
void QxtLogger::write(const QList<QVariant> &args)
{
log(QxtLogger::WriteLevel, args);
}
/*! A Generic Logging Function that takes a LogLevel and a QList<QVariant> of messages
This function is provided for convenience.
*/
void QxtLogger::log(LogLevel level, const QList<QVariant>& args)
{
QMetaObject::invokeMethod(&qxt_d(), "log", Qt::AutoConnection, Q_ARG(QString, logLevelToString(level)), Q_ARG(QList<QVariant>, args));
}
void QxtLogger::Log(const QString &logTarget, const QList<QVariant>& args)
{
QMetaObject::invokeMethod(&qxt_d(), "log", Qt::AutoConnection, Q_ARG(QString, logTarget), Q_ARG(QList<QVariant>, args));
}
/*******************************************************************************
Message Handler for qdebug, qerror, qwarning, etc...
When QxtLogger is enabled as a message handler for Qt, this function
redirects message calls like qdebug, qwarning, qfatal.
\sa QxtLogger::installAsMessageHandler
\sa QxtLogger::removeAsMessageHandler
*******************************************************************************/
void QxtLoggerMessageHandler(QtMsgType type, const char *msg)
{
switch (type)
{
case QtDebugMsg:
QxtLogger::getInstance()->debug(msg, "qdebug");
break;
case QtWarningMsg:
QxtLogger::getInstance()->warning(msg, "qwarning");
break;
case QtCriticalMsg:
QxtLogger::getInstance()->critical(msg, "qcritical");
break;
case QtFatalMsg:
QxtLogger::getInstance()->fatal(msg, "qfatal");
abort();
}
}
/*! \brief Installs QxtLogger as Qt's message handler.
This will make Qt macros use QxtLogger instead of the default
mechanism:
\list
\o qDebug()
\o qWarning()
\o qCritical()
\o qFatal() will call abort() and terminate your application.
\endlist
*/
void QxtLogger::installAsMessageHandler()
{
QMutexLocker lock(qxt_d().mut_lock);
qInstallMsgHandler(QxtLoggerMessageHandler);
}
/*! \brief Tells Qt to use it's own message handling again.
*/
void QxtLogger::removeAsMessageHandler()
{
QMutexLocker lock(qxt_d().mut_lock);
qInstallMsgHandler(0);
}
/*****************************************************************************
Constructor
Private, since QxtLogger is a singleton.
*****************************************************************************/
QxtLogger::QxtLogger()
{
QXT_INIT_PRIVATE(QxtLogger);
qRegisterMetaType<QxtLogger::LogLevel>();
qRegisterMetaType<QxtLogger::LogLevels>();
addLoggerEngine("DEFAULT", new QxtBasicSTDLoggerEngine);
setMinimumLevel("DEFAULT", QxtLogger::InfoLevel);
}
/***************************************************************************//*!
Destructor.
The Destructor for QxtLogger iterates through all the currently installed
QxtLoggerEngines, calls their killLoggerEngine functions through QxtLoggerEngine::killLoggerEngine(),
and then deletes them from the map.
*******************************************************************************/
QxtLogger::~QxtLogger()
{
// implicit destruction only
}
/*! \brief Returns a pointer to the instance of the Logger.
QxtLogger is implemented as a singleton, a single object, that
manages all of the logging done in an application. The easiest way
to use it is by calling the qxtLog macro:
\code
#include <QxtLogger>
...
qxtLog->info("I can log things!");
\endcode
qxtLog expands to QxtLogger::getInstance, which returns a pointer to the logger.
QxtLogger manages it's own memory, so please remember the second rule of pointers:
don't delete it unless you instantiated it yourself.
\code
delete qxtLog; // Will horribly crash your app, and possibly your system
\endcode
*/
QxtLogger &QxtLogger::getInstance()
{
static QxtLogger objectInstance;
return objectInstance;
}
/*! \brief Returns a QString of the given LogLevel.
This function is provided for convenience.
*/
QString QxtLogger::logLevelToString(LogLevel level)
{
switch (level)
{
case TraceLevel:
return "TraceLevel";
case DebugLevel:
return "DebugLevel";
case InfoLevel:
return "InfoLevel";
case WarningLevel:
return "WarningLevel";
case ErrorLevel:
return "ErrorLevel";
case CriticalLevel:
return "CriticalLevel";
case FatalLevel:
return "FatalLevel";
case WriteLevel:
return "WriteLevel";
case AllLevels:
return "AllLevels";
default:
return "NoLevels";
}
}
/*! \brief Returns a LogLevel for the given string, or QxtLogger::NoLevels if invalid.
This function is provided for convenience.
*/
QxtLogger::LogLevel QxtLogger::stringToLogLevel(const QString& level)
{
if (level.toLower() == "alllevels") return QxtLogger::AllLevels;
else if (level.toLower() == "writelevel") return QxtLogger::WriteLevel;
else if (level.toLower() == "fatallevel") return QxtLogger::FatalLevel;
else if (level.toLower() == "criticallevel") return QxtLogger::CriticalLevel;
else if (level.toLower() == "errorlevel") return QxtLogger::ErrorLevel;
else if (level.toLower() == "warnlevel") return QxtLogger::WarningLevel;
else if (level.toLower() == "infolevel") return QxtLogger::InfoLevel;
else if (level.toLower() == "debuglevel") return QxtLogger::DebugLevel;
else if (level.toLower() == "tracelevel") return QxtLogger::TraceLevel;
else return QxtLogger::NoLevels;
}
/*! \brief Enables the given LogLevels across all Engines.
\code
qxtLog->enableLogLevels(QxtLogger::NoLevels);
qxtLog->write("I don't do anything!");
qxtLog->enableLogLevels(QxtLogger::AllLevels);
qxtLog->write("Hi there!");
\endcode
\a levels A bitmask of LogLevels
*/
void QxtLogger::enableLogLevels(LogLevels levels)
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.empty()) return;
Q_FOREACH(QxtLoggerEngine *eng, qxt_d().map_logEngineMap)
{
if (eng)
{
eng->enableLogLevels(levels);
}
}
}
/*! \brief Returns a reference to a refcounted stream.
This is still in its early phases and is in dire need of testing and debugging.
\code
QxtLogger::stream(QxtLogger::WriteLevel) << "This should write stuff" << 1.5 << QString();
\endcode
*/
QxtLogStream QxtLogger::stream(LogLevel level)
{
return QxtLogStream(this, level, QList<QVariant>());
}
/*! \brief Enables the given LogLevels on a named Engine.
This will use the given engine name to tell a loaded QxtLoggerEngine
what LogLevels it should enable.
\code
qxtLog->addLoggerEngine("test", "libTestLogger");
qxtLog->enableLogLevels("test", QxtLoger::AllLevels);
qxtLog->write("You can see me through your 'test' logger now!");
\endcode
\a engineName The name of a QxtLoggerEngine.
\a levels A LogLevel or LogLevels to enable.
\sa addLoggerEngine()
*******************************************************************************/
void QxtLogger::enableLogLevels(const QString &engineName, LogLevels levels)
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.contains(engineName))
{
if (qxt_d().map_logEngineMap.value(engineName))
{
qxt_d().map_logEngineMap.value(engineName)->enableLogLevels(levels);
}
}
}
/*! \brief Turns on all log levels for a named engine.
This is a function provided for convenience, and is equivalent to
calling:
\code
qxtLog->enableLogLevels("test", QxtLogger::AllLevels);
\endcode
*/
void QxtLogger::enableAllLogLevels(const QString &engineName)
{
enableLogLevels(engineName, QxtLogger::AllLevels);
}
/*! \brief Turns on all log levels for all engines.
This is a function provided for convenience, and is equivalent to
calling:
\code
qxtLog->enableLogLevels(QxtLogger::AllLevels);
\endcode
*/
void QxtLogger::enableAllLogLevels()
{
enableLogLevels(QxtLogger::AllLevels);
}
/*! \brief Enables a named engine if it is currently disabled.
\a engineName the name of a QxtLoggerEngine.
*/
void QxtLogger::enableLoggerEngine(const QString &engineName)
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.contains(engineName))
{
if (qxt_d().map_logEngineMap.value(engineName))
{
qxt_d().map_logEngineMap.value(engineName)->enableLogging();
emit loggerEngineEnabled(engineName);
}
}
}
/*! \brief Unflags the given LogLevels across all Engines.
Disables the given LogLevel across all QxtLoggersEngines. Note that some
\a levels A LogLevel or LogLevels to disable.
*/
void QxtLogger::disableLogLevels(LogLevels levels)
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.empty()) return;
Q_FOREACH(QxtLoggerEngine *eng, qxt_d().map_logEngineMap)
{
if (eng)
{
eng->disableLogLevels(levels);
}
}
}
/*! \brief Disables the named Engine.
Disables the the named QxtLoggerEngine if it exists.
\a engineName The name of a log Engine to disable.
*/
void QxtLogger::disableLoggerEngine(const QString &engineName)
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.contains(engineName))
{
if (qxt_d().map_logEngineMap.value(engineName))
{
qxt_d().map_logEngineMap.value(engineName)->disableLogging();
emit loggerEngineDisabled(engineName);
}
}
}
/*! \brief Sets the minimumlog level for all Engines, as well as the levels above it.
\a level The single LogLevel to set as minimum.
*/
void QxtLogger::setMinimumLevel(LogLevel level)
{
QMutexLocker lock(qxt_d().mut_lock);
Q_FOREACH(QxtLoggerEngine *eng, qxt_d().map_logEngineMap)
{
if (eng)
{
qxt_d().setQxtLoggerEngineMinimumLevel(eng, level);
}
}
}
/*! \brief Sets the minimumlog level for the named Engine, as well as the levels above it.
\a engineName The name of a QxtLoggerEngine.
\a level The single LogLevel to set as minimum.
*/
void QxtLogger::setMinimumLevel(const QString &engineName, LogLevel level)
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.contains(engineName))
{
if (qxt_d().map_logEngineMap.value(engineName))
{
qxt_d().setQxtLoggerEngineMinimumLevel(qxt_d().map_logEngineMap.value(engineName), level);
}
}
}
/*! \brief Calls QxtLoggerEngine::initLoggerEngine() for the named Engine.
Some QxtLoggerEngine plugins might require additional initialization. Check the documentation
for your plugin. Most basic plugins will not require special tasks.
\a engineName The name of a QxtLoggerEngine.
*/
void QxtLogger::initLoggerEngine(const QString &engineName)
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.contains(engineName))
{
if (qxt_d().map_logEngineMap.value(engineName))
{
qxt_d().map_logEngineMap.value(engineName)->initLoggerEngine();
}
}
}
/*! \brief Calls QxtLoggerEngine::killLoggerEngine() for the named Engine.
Some QxtLoggerEngine plugins might require special cleanup before destruction.
Check the documentation for your plugin. Most basic plugins will not require this.
\a engineName The name of a QxtLoggerEngine.
*/
void QxtLogger::killLoggerEngine(const QString &engineName)
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.contains(engineName))
{
if (qxt_d().map_logEngineMap.value(engineName))
{
qxt_d().map_logEngineMap.value(engineName)->killLoggerEngine();
}
}
}
/*! \brief Checks if the named Engine has the given LogLevel enabled.
\a engineName The name of a QxtLoggerEngine to query
\a level A LogLevel or LogLevels to disable.
Returns true or false.
*/
bool QxtLogger::isLogLevelEnabled(const QString &engineName, LogLevel level) const
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.contains(engineName))
{
return qxt_d().map_logEngineMap.value(engineName)->isLogLevelEnabled(level);
}
else return false;
}
/*! \brief Disables the given LogLevel across the named QxtLoggersEngines.
\a engineName The name of a QxtLoggerEngine.
\a level A LogLevel or LogLevels to disable.
*/
void QxtLogger::disableLogLevels(const QString &engineName, LogLevels levels)
{
QMutexLocker lock(qxt_d().mut_lock);
if (qxt_d().map_logEngineMap.contains(engineName))
{
if (qxt_d().map_logEngineMap.value(engineName))
{
qxt_d().map_logEngineMap.value(engineName)->disableLogLevels(levels);
}
}
}
/*! \brief Disables all log levels for the named Engine.
\a engineName The name of an Engine.
*/
void QxtLogger::disableAllLogLevels(const QString &engineName)
{
disableLogLevels(engineName, QxtLogger::AllLevels);
}
/*! \brief Disables all log levels for all named Engines.
*/
void QxtLogger::disableAllLogLevels()
{
disableLogLevels(QxtLogger::AllLevels);
}
/*! \brief Gives QxtLogger an already-instantiated QxtLoggerEngine to use.
addLoggerEngine inserts a subclass of QxtLoggerEngine for QxtLogger
to manage. QxtLogger takes ownership of the engine and will
manage memory on its own.
\code
#include <QxtLogger>
...
class MyLoggerEngine : public QxtLoggerEngine;
...
qxtLog->addLoggerEngine("my engine", new MyLoggerEngine);
\endcode
\sa QxtLoggerEngine
*/
void QxtLogger::addLoggerEngine(const QString &engineName, QxtLoggerEngine *engine)
{
QMutexLocker lock(qxt_d().mut_lock);
if (!qxt_d().map_logEngineMap.contains(engineName) && engine)
{
qxt_d().map_logEngineMap.insert(engineName, engine);
emit loggerEngineAdded(engineName);
}
}
/*
\brief Gives QxtLogger an already-instantiated QLibrary.
This is an overloaded functionand not the preferred method of adding Engines.
It is useful to load plugins that are not in the applications Path.
\code
QLibrary *lib = new QLibrary("/path/to/plugin");
qxtLog->addLogger("my engine", lib);
\endcode
void QxtLogger::addLoggerEngine(const QString &engineName, QLibrary *engineLib)
{
QMutexLocker lock(qxt_d().mut_lock);
typedef QxtLoggerEngine* (*LibInstance)();
LibInstance instance = (LibInstance)engineLib->resolve("getInstance");
qWarning() << engineLib->errorString();
if (!instance) return;
else if (!qxt_d().map_logEngineMap.contains(engineName) && engineLib)
{
qxt_d().map_logEngineMap.insert(engineName, instance());
emit loggerEngineAdded(engineName);
}
}
\brief Loads an Engine from a plugin in the current path.