summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/logger/src/Logger.cpp
blob: 689bc42e8021fa4bad75435d1eba10f5c613698f (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
/*
  Copyright (c) 2012 Boris Moiseev (cyberbobs at gmail dot com)

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1
  as published by the Free Software Foundation and appearing in the file
  LICENSE.LGPL included in the packaging of this file.

  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 Lesser General Public License for more details.
*/
// Local
#include "Logger.h"
#include "AbstractAppender.h"
#include "AbstractStringAppender.h"

// Qt
#include <QCoreApplication>
#include <QReadWriteLock>
#include <QSemaphore>
#include <QDateTime>
#include <QIODevice>
#include <QTextCodec>

#if defined(Q_OS_ANDROID)
#  include <android/log.h>
#  include <AndroidAppender.h>
#endif

// STL
#include <iostream>


/**
 * \file Logger.h
 * \brief A file containing the description of Logger class and and additional useful macros for logging
 */


/**
 * \mainpage
 *
 * Logger is a simple way to write the history of your application lifecycle to any target logging device (which is
 * called Appender and may write to any target you will implement with it: console, text file, XML or something - you
 * choose) and to map logging message to a class, function, source file and line of code which it is called from.
 *
 * Some simple appenders (which may be considered an examples) are provided with the Logger itself: see ConsoleAppender
 * and FileAppender documentation.
 *
 * It supports using it in a multithreaded applications, so all of its functions are thread safe.
 *
 * Simple usage example:
 * \code
 * #include <QCoreApplication>
 *
 * #include <Logger.h>
 * #include <ConsoleAppender.h>
 *
 * int main(int argc, char* argv[])
 * {
 *   QCoreApplication app(argc, argv);
 *   ...
 *   ConsoleAppender* consoleAppender = new ConsoleAppender;
 *   consoleAppender->setFormat("[%{type:-7}] <%{Function}> %{message}\n");
 *   cuteLogger->registerAppender(consoleAppender);
 *   ...
 *   LOG_INFO("Starting the application");
 *   int result = app.exec();
 *   ...
 *   if (result)
 *     LOG_WARNING() << "Something went wrong." << "Result code is" << result;
 *
 *   return result;
 * }
 * \endcode
 *
 * Logger internally uses the lazy-initialized singleton object and needs no definite initialization, but you may
 * consider registering a log appender before calling any log recording functions or macros.
 *
 * The library design of Logger allows you to simply mass-replace all occurrences of qDebug and similar calls with
 * similar Logger macros (e.g. LOG_DEBUG())
 *
 * \note Logger uses a singleton global instance which lives through all the application life cycle and self-destroys
 *       destruction of the QCoreApplication (or QApplication) instance. It needs a QCoreApplication instance to be
 *       created before any of the Logger's functions are called.
 *
 * \sa cuteLogger
 * \sa LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL
 * \sa LOG_CTRACE, LOG_CDEBUG, LOG_CINFO, LOG_CWARNING, LOG_CERROR, LOG_CFATAL
 * \sa LOG_ASSERT
 * \sa LOG_TRACE_TIME, LOG_DEBUG_TIME, LOG_INFO_TIME
 * \sa AbstractAppender
 */


/**
 * \def cuteLogger
 *
 * \brief Macro returning the current instance of Logger object
 *
 * If you haven't created a local Logger object it returns the same value as the Logger::globalInstance() functions.
 * This macro is a recommended way to get an access to the Logger instance used in current class.
 *
 * Example:
 * \code
 * ConsoleAppender* consoleAppender = new ConsoleAppender;
 * cuteLogger->registerAppender(consoleAppender);
 * \endcode
 *
 * \sa Logger::globalInstance()
 */


/**
 * \def LOG_TRACE
 *
 * \brief Writes the trace log record
 *
 * This macro is the convinient way to call Logger::write(). It uses the common preprocessor macros \c __FILE__,
 * \c __LINE__ and the standart Qt \c Q_FUNC_INFO macros to automatically determine the needed parameters to call
 * Logger::write().
 *
 * \note This and other (LOG_INFO() etc...) macros uses the variadic macro arguments to give convinient usage form for
 * the different versions of Logger::write() (using the QString or const char* argument or returning the QDebug class
 * instance). Not all compilers will support this. Please, consider reviewing your compiler documentation to ensure
 * it support __VA_ARGS__ macro.
 *
 * \sa Logger::LogLevel
 * \sa Logger::write()
 */


/**
 * \def LOG_DEBUG
 *
 * \brief Writes the debug log record
 *
 * This macro records the debug log record using the Logger::write() function. It works similar to the LOG_TRACE()
 * macro.
 *
 * \sa LOG_TRACE()
 * \sa Logger::LogLevel
 * \sa Logger::write()
 */


/**
 * \def LOG_INFO
 *
 * \brief Writes the info log record
 *
 * This macro records the info log record using the Logger::write() function. It works similar to the LOG_TRACE()
 * macro.
 *
 * \sa LOG_TRACE()
 * \sa Logger::LogLevel
 * \sa Logger::write()
 */


/**
 * \def LOG_WARNING
 *
 * \brief Write the warning log record
 *
 * This macro records the warning log record using the Logger::write() function. It works similar to the LOG_TRACE()
 * macro.
 *
 * \sa LOG_TRACE()
 * \sa Logger::LogLevel
 * \sa Logger::write()
 */


/**
 * \def LOG_ERROR
 *
 * \brief Write the error log record
 * This macro records the error log record using the Logger::write() function. It works similar to the LOG_TRACE()
 * macro.
 *
 * \sa LOG_TRACE()
 * \sa Logger::LogLevel
 * \sa Logger::write()
 */


/**
 * \def LOG_FATAL
 *
 * \brief Write the fatal log record
 *
 * This macro records the fatal log record using the Logger::write() function. It works similar to the LOG_TRACE()
 * macro.
 *
 * \note Recording of the log record using the Logger::Fatal log level will lead to calling the STL abort()
 *       function, which will interrupt the running of your software and begin the writing of the core dump.
 *
 * \sa LOG_TRACE()
 * \sa Logger::LogLevel
 * \sa Logger::write()
 */


/**
 * \def LOG_CTRACE(category)
 *
 * \brief Writes the trace log record to the specific category
 *
 * This macro is the similar to the LOG_TRACE() macro, but has a category parameter
 * to write only to the category appenders (registered using Logger::registerCategoryAppender() method).
 *
 * \param category category name string
 *
 * \sa LOG_TRACE()
 * \sa Logger::LogLevel
 * \sa Logger::registerCategoryAppender()
 * \sa Logger::write()
 * \sa LOG_CATEGORY(), LOG_GLOBAL_CATEGORY()
 */


/**
 * \def LOG_CDEBUG
 *
 * \brief Writes the debug log record to the specific category
 *
 * This macro records the debug log record using the Logger::write() function. It works similar to the LOG_CTRACE()
 * macro.
 *
 * \sa LOG_CTRACE()
 */


/**
 * \def LOG_CINFO
 *
 * \brief Writes the info log record to the specific category
 *
 * This macro records the info log record using the Logger::write() function. It works similar to the LOG_CTRACE()
 * macro.
 *
 * \sa LOG_CTRACE()
 */


/**
 * \def LOG_CWARNING
 *
 * \brief Writes the warning log record to the specific category
 *
 * This macro records the warning log record using the Logger::write() function. It works similar to the LOG_CTRACE()
 * macro.
 *
 * \sa LOG_CTRACE()
 */


/**
 * \def LOG_CERROR
 *
 * \brief Writes the error log record to the specific category
 *
 * This macro records the error log record using the Logger::write() function. It works similar to the LOG_CTRACE()
 * macro.
 *
 * \sa LOG_CTRACE()
 */


/**
 * \def LOG_CFATAL
 *
 * \brief Write the fatal log record to the specific category
 *
 * This macro records the fatal log record using the Logger::write() function. It works similar to the LOG_CTRACE()
 * macro.
 *
 * \note Recording of the log record using the Logger::Fatal log level will lead to calling the STL abort()
 *       function, which will interrupt the running of your software and begin the writing of the core dump.
 *
 * \sa LOG_CTRACE()
 */


/**
 * \def LOG_CATEGORY(category)
 *
 * \brief Create logger instance inside your custom class to log all messages to the specified category
 *
 * This macro is used to pass all log messages inside your custom class to the specific category.
 * You must include this macro inside your class declaration (similarly to the Q_OBJECT macro).
 * Internally, this macro redefines cuteLoggerInstance() function, creates the local Logger object inside your class and
 * sets the default category to the specified parameter.
 *
 * Thus, any call to cuteLoggerInstance() (for example, inside LOG_TRACE() macro) will return the local Logger object,
 * so any logging message will be directed to the default category.
 *
 * \note This macro does not register any appender to the newly created logger instance. You should register
 * logger appenders manually, inside your class.
 *
 * Usage example:
 * \code
 * class CustomClass : public QObject
 * {
 *   Q_OBJECT
 *   LOG_CATEGORY("custom_category")
 *   ...
 * };
 *
 * CustomClass::CustomClass(QObject* parent) : QObject(parent)
 * {
 *   cuteLogger->registerAppender(new FileAppender("custom_category_log"));
 *   LOG_TRACE() << "Trace to the custom category log";
 * }
 * \endcode
 *
 * If used compiler supports C++11 standard, LOG_CATEGORY and LOG_GLOBAL_CATEGORY macros would also work when added
 * inside of any scope. It could be useful, for example, to log every single run of a method to a different file.
 *
 * \code
 * void foo()
 * {
 *   QString categoryName = QDateTime::currentDateTime().toString("yyyy-MM-ddThh-mm-ss-zzz");
 *   LOG_CATEGORY(categoryName);
 *   cuteLogger->registerAppender(new FileAppender(categoryName + ".log"));
 *   ...
 * }
 * \endcode
 *
 * \sa Logger::write()
 * \sa LOG_TRACE
 * \sa Logger::registerCategoryAppender()
 * \sa Logger::setDefaultCategory()
 * \sa LOG_GLOBAL_CATEGORY
 */


/**
 * \def LOG_GLOBAL_CATEGORY(category)
 *
 * \brief Create logger instance inside your custom class to log all messages both to the specified category and to
 * the global logger instance.
 *
 * This macro is similar to LOG_CATEGORY(), but also passes all log messages to the global logger instance appenders.
 * It is equal to defining the local category logger using LOG_CATEGORY macro and calling:
 * \code cuteLogger->logToGlobalInstance(cuteLogger->defaultCategory(), true); \endcode
 *
 * \sa LOG_CATEGORY
 * \sa Logger::logToGlobalInstance()
 * \sa Logger::defaultCategory()
 * \sa Logger::registerCategoryAppender()
 * \sa Logger::write()
 */



/**
 * \def LOG_ASSERT
 *
 * \brief Check the assertion
 *
 * This macro is a convinient and recommended to use way to call Logger::writeAssert() function. It uses the
 * preprocessor macros (as the LOG_DEBUG() does) to fill the necessary arguments of the Logger::writeAssert() call. It
 * also uses undocumented but rather mature and stable \c qt_noop() function (which does nothing) when the assertion
 * is true.
 *
 * Example:
 * \code
 * bool b = checkSomething();
 * ...
 * LOG_ASSERT(b == true);
 * \endcode
 *
 * \sa Logger::writeAssert()
 */


/**
 * \def LOG_TRACE_TIME
 *
 * \brief Logs the processing time of current function / code block
 *
 * This macro automagically measures the function or code of block execution time and outputs it as a Logger::Trace
 * level log record.
 *
 * Example:
 * \code
 * int foo()
 * {
 *   LOG_TRACE_TIME();
 *   ... // Do some long operations
 *   return 0;
 * } // Outputs: Function foo finished in <time> ms.
 * \endcode
 *
 * If you are measuring a code of block execution time you may also add a name of block to the macro:
 * \code
 * int bar(bool doFoo)
 * {
 *   LOG_TRACE_TIME();
 *
 *   if (doFoo)
 *   {
 *     LOG_TRACE_TIME("Foo");
 *     ...
 *   }
 *
 *   ...
 * }
 * // Outputs:
 * // "Foo" finished in <time1> ms.
 * // Function bar finished in <time2> ms.
 * \endcode
 *
 * \note Macro switches to logging the seconds instead of milliseconds when the execution time reaches 10000 ms.
 * \sa LOG_DEBUG_TIME, LOG_INFO_TIME
 */


/**
 * \def LOG_DEBUG_TIME
 *
 * \brief Logs the processing time of current function / code block
 *
 * This macro automagically measures the function or code of block execution time and outputs it as a Logger::Debug
 * level log record. It works similar to LOG_TRACE_TIME() macro.
 *
 * \sa LOG_TRACE_TIME
 */


/**
 * \def LOG_INFO_TIME
 *
 * \brief Logs the processing time of current function / code block
 *
 * This macro automagically measures the function or code of block execution time and outputs it as a Logger::Info
 * level log record. It works similar to LOG_TRACE_TIME() macro.
 *
 * \sa LOG_TRACE_TIME
 */


/**
 * \class Logger
 *
 * \brief Very simple but rather powerful component which may be used for logging your application activities.
 *
 * Global logger instance created on a first access to it (e.g. registering appenders, calling a LOG_DEBUG() macro
 * etc.) registers itself as a Qt default message handler and captures all the qDebug/qWarning/qCritical output.
 *
 * \note Qt 4 qDebug set of macro doesn't support capturing source function name, file name or line number so we
 *       recommend to use LOG_DEBUG() and other Logger macros instead.
 *
 * \sa cuteLogger
 * \sa [CuteLogger Documentation](index.html)
 */

// Forward declarations
static void cleanupLoggerGlobalInstance();

#if QT_VERSION >= 0x050000
static void qtLoggerMessageHandler(QtMsgType, const QMessageLogContext& context, const QString& msg);
#else
static void qtLoggerMessageHandler(QtMsgType type, const char* msg);
#endif

/**
 * \internal
 *
 * LoggerPrivate class implements the Singleton pattern in a thread-safe way. It contains a static pointer to the
 * global logger instance protected by QReadWriteLock
 */
class LoggerPrivate
{
  public:
    static Logger* globalInstance;
    static QReadWriteLock globalInstanceLock;

    QList<AbstractAppender*> appenders;
    QMutex loggerMutex;

    QMap<QString, bool> categories;
    QMultiMap<QString, AbstractAppender*> categoryAppenders;
    QStringList noAppendersCategories; //<! Categories without appenders that was already warned about
    QString defaultCategory;
    bool writeDefaultCategoryToGlobalInstance;
};


// Static fields initialization
Logger* LoggerPrivate::globalInstance = nullptr;
QReadWriteLock LoggerPrivate::globalInstanceLock;


static void cleanupLoggerGlobalInstance()
{
  QWriteLocker locker(&LoggerPrivate::globalInstanceLock);

  delete LoggerPrivate::globalInstance;
  LoggerPrivate::globalInstance = nullptr;
}


#if QT_VERSION >= 0x050000
static void qtLoggerMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
  Logger::LogLevel level = Logger::Debug;
  switch (type)
  {
    case QtDebugMsg:
      level = Logger::Debug;
      break;
#if QT_VERSION >= 0x050500
    case QtInfoMsg:
      level = Logger::Info;
      break;
#endif
    case QtWarningMsg:
      level = Logger::Warning;
      break;
    case QtCriticalMsg:
      level = Logger::Error;
      break;
    case QtFatalMsg:
      level = Logger::Fatal;
      break;
  }

  bool isDefaultCategory = QString::fromLatin1(context.category) == "default";
  Logger::globalInstance()->write(level, context.file, context.line, context.function, isDefaultCategory ? nullptr : context.category, msg);
}

#else

static void qtLoggerMessageHandler(QtMsgType type, const char* msg)
{
  switch (type)
  {
    case QtDebugMsg:
      cuteLoggerInstance()->write(Logger::Debug, "", 0, "qDebug", 0, msg);
      break;
    case QtWarningMsg:
      cuteLoggerInstance()->write(Logger::Warning, "", 0, "qDebug", 0, msg);
      break;
    case QtCriticalMsg:
      cuteLoggerInstance()->write(Logger::Error, "", 0, "qDebug", 0, msg);
      break;
    case QtFatalMsg:
      cuteLoggerInstance()->write(Logger::Fatal, "", 0, "qDebug", 0, msg);
      break;
  }
}
#endif


//! Construct the instance of Logger
/**
 * If you're only using one global instance of logger you wouldn't probably need to use this constructor manually.
 * Consider using [cuteLogger](@ref cuteLogger) macro instead to access the logger instance
 */
Logger::Logger()
  : d_ptr(new LoggerPrivate)
{
  Q_D(Logger);
  d->writeDefaultCategoryToGlobalInstance = false;
}


//! Construct the instance of Logger and set logger default category
/**
 * If you're only using one global instance of logger you wouldn't probably need to use this constructor manually.
 * Consider using [cuteLogger](@ref cuteLogger) macro instead to access the logger instance and call
 * [setDefaultCategory](@ref setDefaultCategory) method.
 *
 * \sa Logger()
 * \sa setDefaultCategory()
 */
Logger::Logger(const QString& defaultCategory, bool writeToGlobalInstance)
  : d_ptr(new LoggerPrivate)
{
  Q_D(Logger);
  d->writeDefaultCategoryToGlobalInstance = writeToGlobalInstance;

  setDefaultCategory(defaultCategory);
}


//! Destroy the instance of Logger
/**
 * You probably wouldn't need to use this function directly. Global instance of logger will be destroyed automatically
 * at the end of your QCoreApplication execution
 */
Logger::~Logger()
{
  Q_D(Logger);

  // Cleanup appenders
  QMutexLocker appendersLocker(&d->loggerMutex);
#if QT_VERSION >= 0x050e00
  QSet<AbstractAppender*> deleteList(QSet<AbstractAppender*>(d->appenders.begin(), d->appenders.end()));
  deleteList.unite(QSet<AbstractAppender*>(d->categoryAppenders.values().begin(), d->categoryAppenders.values().end()));
#else
  QSet<AbstractAppender*> deleteList(QSet<AbstractAppender*>::fromList(d->appenders));
  deleteList.unite(QSet<AbstractAppender*>::fromList(d->categoryAppenders.values()));
#endif
  qDeleteAll(deleteList);

  appendersLocker.unlock();

  delete d_ptr;
}


//! Converts the LogLevel enum value to its string representation
/**
 * \param logLevel Log level to convert
 *
 * \sa LogLevel
 * \sa levelFromString()
 */
QString Logger::levelToString(Logger::LogLevel logLevel)
{
  switch (logLevel)
  {
    case Trace:
      return QLatin1String("Trace");
    case Debug:
      return QLatin1String("Debug");
    case Info:
      return QLatin1String("Info");
    case Warning:
      return QLatin1String("Warning");
    case Error:
      return QLatin1String("Error");
    case Fatal:
      return QLatin1String("Fatal");
  }

  return QString();
}


//! Converts the LogLevel string representation to enum value
/**
 * Comparation of the strings is case independent. If the log level string provided cannot be understood
 * Logger::Debug is returned.
 *
 * \param s String to be decoded
 *
 * \sa LogLevel
 * \sa levelToString()
 */
Logger::LogLevel Logger::levelFromString(const QString& s)
{
  QString str = s.trimmed().toLower();

  LogLevel result = Debug;

  if (str == QLatin1String("trace"))
    result = Trace;
  else if (str == QLatin1String("debug"))
    result = Debug;
  else if (str == QLatin1String("info"))
    result = Info;
  else if (str == QLatin1String("warning"))
    result = Warning;
  else if (str == QLatin1String("error"))
    result = Error;
  else if (str == QLatin1String("fatal"))
    result = Fatal;

  return result;
}


//! Returns the global instance of Logger
/**
 * In a most cases you shouldn't use this function directly. Consider using [cuteLogger](@ref cuteLogger) macro instead.
 *
 * \sa cuteLogger
 */
Logger* Logger::globalInstance()
{
  Logger* result = nullptr;
  {
    QReadLocker locker(&LoggerPrivate::globalInstanceLock);
    result = LoggerPrivate::globalInstance;
  }

  if (!result)
  {
    QWriteLocker locker(&LoggerPrivate::globalInstanceLock);
    LoggerPrivate::globalInstance = new Logger;

#if QT_VERSION >= 0x050000
    qInstallMessageHandler(qtLoggerMessageHandler);
#else
    qInstallMsgHandler(qtLoggerMessageHandler);
#endif
    qAddPostRoutine(cleanupLoggerGlobalInstance);
    result = LoggerPrivate::globalInstance;
  }

  return result;
}


//! Registers the appender to write the log records to
/**
 * On the log writing call (using one of the macros or the write() function) Logger traverses through the list of
 * the appenders and writes a log records to the each of them. Please, look through the AbstractAppender
 * documentation to understand the concept of appenders.
 *
 * If no appenders was added to Logger, it falls back to logging into the \c std::cerr STL stream.
 *
 * \param appender Appender to register in the Logger
 *
 * \note Logger takes ownership on the appender and it will delete it on the application exit. According to this,
 *       appenders must be created on heap to prevent double destruction of the appender.
 *
 * \sa registerCategoryAppender
 * \sa AbstractAppender
 */
void Logger::registerAppender(AbstractAppender* appender)
{
  Q_D(Logger);

  QMutexLocker locker(&d->loggerMutex);

  if (!d->appenders.contains(appender))
    d->appenders.append(appender);
  else
    std::cerr << "Trying to register appender that was already registered" << std::endl;
}

//! Registers the appender to write the log records to the specific category
/**
 * Calling this method, you can link some appender with the named category.
 * On the log writing call to the specific category (calling write() with category parameter directly,
 * writing to the default category, or using special LOG_CDEBUG(), LOG_CWARNING() etc. macros),
 * Logger writes the log message only to the list of registered category appenders.
 *
 * You can call logToGlobalInstance() to pass all category log messages to the global logger instance appenders
 * (registered using registerAppender()).
 * If no category appenders with specific name was registered to the Logger,
 * it falls back to logging into the \c std::cerr STL stream, both with simple warning message.
 *
 * \param category Category name
 * \param appender Appender to register in the Logger
 *
 * \note Logger takes ownership on the appender and it will delete it on the application exit. According to this,
 *       appenders must be created on heap to prevent double destruction of the appender.
 *
 * \sa registerAppender
 * \sa LOG_CTRACE(), LOG_CDEBUG(), LOG_CINFO(), LOG_CWARNING(), LOG_CERROR(), LOG_CFATAL()
 * \sa LOG_CATEGORY(), LOG_GLOBAL_CATEGORY()
 * \sa logToGlobalInstance
 * \sa setDefaultCategory
 */
void Logger::registerCategoryAppender(const QString& category, AbstractAppender* appender)
{
  Q_D(Logger);

  QMutexLocker locker(&d->loggerMutex);

  if (!d->categoryAppenders.values().contains(appender))
    d->categoryAppenders.insert(category, appender);
  else
    std::cerr << "Trying to register appender that was already registered" << std::endl;
}


//! Removes the registered appender from logger
/**
 * After calling this function logger stops writing any of the records to the appender.
 *
 * \param appender Pointer to appender to remove from logger
 * \note Removed appender will not be deleted on the application shutdown and you will need to destroy the object
 *       yourself.
 * \sa registerAppender
 */
void Logger::removeAppender(AbstractAppender* appender)
{
  Q_D(Logger);

  QMutexLocker locker(&d->loggerMutex);

  d->appenders.removeAll(appender);
  for (QMultiMap<QString,AbstractAppender*>::iterator it = d->categoryAppenders.begin(); it != d->categoryAppenders.end();)
  {
    if (it.value() == appender)
      it = d->categoryAppenders.erase(it);
    else
      ++it;
  }
}


//! Sets default logging category
/**
 * All log messages to this category appenders will also be written to general logger instance appenders (registered
 * using [registerAppender](@ref registerAppender) method), and vice versa.
 * In particular, any calls to the LOG_DEBUG() macro will be treated as category logging,
 * so you needn't to specify category name using LOG_CDEBUG() macro.
 *
 * To unset the default category, pass a null string as a parameter.
 *
 * \param category Category name
 *
 * \note "category" format marker will be set to the category name for all of these messages
 * (see [AbstractStringAppender::setFormat](@ref AbstractStringAppender::setFormat)).
 *
 * \sa defaultCategory()
 * \sa registerCategoryAppender()
 * \sa logToGlobalInstance()
 */
void Logger::setDefaultCategory(const QString& category)
{
  Q_D(Logger);

  QMutexLocker locker(&d->loggerMutex);

  d->defaultCategory = category;
}

//! Returns default logging category name
/**
 * \sa setDefaultCategory
 */
QString Logger::defaultCategory() const
{
  Q_D(const Logger);
  return d->defaultCategory;
}

//! Links some logging category with the global logger instance appenders.
/**
 * If set to true, all log messages to the specified category appenders will also be written to the global logger instance appenders,
 * registered using registerAppender().
 *
 * By default, all messages to the specific category are written only to the specific category appenders
 * (registered using registerCategoryAppender()).
 *
 * \param category Category name
 * \param logToGlobal Link or onlink the category from global logger instance appender
 *
 * \sa globalInstance
 * \sa registerAppender
 * \sa registerCategoryAppender
 */
void Logger::logToGlobalInstance(const QString& category, bool logToGlobal)
{
  Q_D(Logger);

  if (this == globalInstance())
  {
    QMutexLocker locker(&d->loggerMutex);
    d->categories.insert(category, logToGlobal);
  }
  else
  {
    globalInstance()->logToGlobalInstance(category, logToGlobal);
  }
}


void Logger::write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category,
                   const QString& message, bool fromLocalInstance)
{
  Q_D(Logger);

  QMutexLocker locker(&d->loggerMutex);

  QString logCategory = QString::fromLatin1(category);
  if (logCategory.isNull() && !d->defaultCategory.isNull())
    logCategory = d->defaultCategory;

  bool wasWritten = false;
  bool isGlobalInstance = this == globalInstance();
  bool linkedToGlobal = isGlobalInstance && d->categories.value(logCategory, false);

  if (!logCategory.isNull())
  {
    QList<AbstractAppender*> appenders = d->categoryAppenders.values(logCategory);
    if (appenders.length() == 0)
    {
      if (logCategory != d->defaultCategory && !linkedToGlobal && !fromLocalInstance && !d->noAppendersCategories.contains(logCategory))
      {
        std::cerr << "No appenders associated with category " << qPrintable(logCategory) << std::endl;
        d->noAppendersCategories.append(logCategory);
      }
    }
    else
    {
      foreach (AbstractAppender* appender, appenders)
        appender->write(timeStamp, logLevel, file, line, function, logCategory, message);
      wasWritten = true;
    }
  }

  // the default category is linked to the main logger appenders
  // global logger instance also writes all linked categories to the main appenders
  if (logCategory.isNull() || logCategory == d->defaultCategory || linkedToGlobal)
  {
    if (!d->appenders.isEmpty())
    {
      foreach (AbstractAppender* appender, d->appenders)
        appender->write(timeStamp, logLevel, file, line, function, logCategory, message);
      wasWritten = true;
    }
    else
    {
      static bool noAppendersWarningShown = false;
      if (!noAppendersWarningShown)
      {
#if defined(Q_OS_ANDROID)
        __android_log_write(ANDROID_LOG_WARN, "Logger", "No appenders registered with logger");
#else
        std::cerr << "No appenders registered with logger" << std::endl;
#endif
        noAppendersWarningShown = true;
      }
    }
  }

  // local logger instances send category messages to the global instance
  if (!isGlobalInstance)
  {
    if (!logCategory.isNull())
    {
      globalInstance()->write(timeStamp, logLevel, file, line, function, logCategory.toLatin1(), message, true);
      wasWritten = true;
    }

    if (d->writeDefaultCategoryToGlobalInstance && logCategory == d->defaultCategory)
    {
      globalInstance()->write(timeStamp, logLevel, file, line, function, nullptr, message, true);
      wasWritten = true;
    }
  }

  if (!wasWritten && !fromLocalInstance)
  {
    // Fallback
#if defined(Q_OS_ANDROID)
    QString result = QString(QLatin1String("<%2> %3")).arg(AbstractStringAppender::stripFunctionName(function)).arg(message);
    __android_log_write(AndroidAppender::androidLogPriority(logLevel), "Logger", qPrintable(result));
#else
    QString result = QString(QLatin1String("[%1] <%2> %3")).arg(levelToString(logLevel), -7)
                     .arg(AbstractStringAppender::stripFunctionName(function)).arg(message);
    std::cerr << qPrintable(result) << std::endl;
#endif
  }

  if (logLevel == Logger::Fatal)
    abort();
}


//! Writes the log record
/**
 * Writes the log records with the supplied arguments to all the registered appenders.
 *
 * \note It is not recommended to call this function directly. Instead of this you can just call one of the macros
 *       (LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL) that will supply all the needed
 *       information to this function.
 *
 * \param timeStamp - the time stamp of the record
 * \param logLevel - the log level of the record
 * \param file - the name of the source file that requested the log record
 * \param line - the line of the code of source file that requested the log record
 * \param function - name of the function that requested the log record
 * \param category - logging category (0 for default category)
 * \param message - log message
 *
 * \note Recording of the log record using the Logger::Fatal log level will lead to calling the STL abort()
 *       function, which will interrupt the running of your software and begin the writing of the core dump.
 *
 * \sa LogLevel
 * \sa LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL
 * \sa AbstractAppender
 */
void Logger::write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category,
                   const QString& message)
{
  write(timeStamp, logLevel, file, line, function, category, message, /* fromLocalInstance = */ false);
}

/**
 * This is the overloaded function provided for the convinience. It behaves similar to the above function.
 *
 * This function uses the current timestamp obtained with \c QDateTime::currentDateTime().
 *
 * \sa write()
 */
void Logger::write(LogLevel logLevel, const char* file, int line, const char* function, const char* category,
                   const QString& message)
{
  write(QDateTime::currentDateTime(), logLevel, file, line, function, category, message);
}


//! Writes the assertion
/**
 * This function writes the assertion record using the write() function.
 *
 * The assertion record is always written using the Logger::Fatal log level which leads to the abortation of the
 * program and generation of the core dump (if supported).
 *
 * The message written to the appenders will be identical to the \c condition argument prefixed with the
 * <tt>ASSERT:</tt> notification.
 *
 * \note It is not recommended to call this function directly. Instead of this you can just call the LOG_ASSERT
 *       macro that will supply all the needed information to this function.
 *
 * \sa LOG_ASSERT
 * \sa write()
 */
void Logger::writeAssert(const char* file, int line, const char* function, const char* condition)
{
  write(Logger::Fatal, file, line, function, nullptr, QString("ASSERT: \"%1\"").arg(condition));
}


Logger* cuteLoggerInstance()
{
  return Logger::globalInstance();
}



void LoggerTimingHelper::start(const char* msg, ...)
{
  va_list va;
  va_start(va, msg);
#if QT_VERSION >= 0x050500
  m_block = QString().vasprintf(msg, va);
#else
  m_block = QString().vsprintf(msg, va);
#endif
  va_end(va);

  m_time.start();
}


void LoggerTimingHelper::start(const QString& block)
{
  m_block = block;
  m_time.start();
}


void LoggerTimingHelper::start(Logger::TimingMode mode, const QString& block)
{
  m_timingMode = mode;
  m_block = block;
  m_time.start();
}


LoggerTimingHelper::~LoggerTimingHelper()
{
  QString message;
  if (m_block.isEmpty())
    message = QString(QLatin1String("Function %1 finished in ")).arg(AbstractStringAppender::stripFunctionName(m_function));
  else
    message = QString(QLatin1String("\"%1\" finished in ")).arg(m_block);

  qint64 elapsed = m_time.elapsed();
  if (elapsed >= 10000 && m_timingMode == Logger::TimingAuto)
    message += QString(QLatin1String("%1 s.")).arg(elapsed / 1000);
  else
    message += QString(QLatin1String("%1 ms.")).arg(elapsed);

  m_logger->write(m_logLevel, m_file, m_line, m_function, nullptr, message);
}


CuteMessageLogger::~CuteMessageLogger()
{
  m_l->write(m_level, m_file, m_line, m_function, m_category, m_message);
}

void CuteMessageLogger::write(const char* msg, ...)
{
  va_list va;
  va_start(va, msg);
  m_message = QString::vasprintf(msg, va);
  va_end(va);
}


void CuteMessageLogger::write(const QString& msg)
{
  m_message = msg;
}


QDebug CuteMessageLogger::write()
{
  QDebug d(&m_message);
  return d;
}