-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasylogging++.h
4003 lines (3749 loc) · 149 KB
/
easylogging++.h
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
///////////////////////////////////////////////////////////////////////////////////
// //
// easylogging++.h - Core of EasyLogging++ //
// //
// EasyLogging++ v8.91 //
// Cross platform logging made easy for C++ applications //
// Author Majid Khan <[email protected]> //
// http://www.icplusplus.com/tools/easylogging //
// https://github.com/mkhan3189/EasyLoggingPP //
// //
// Copyright (c) 2012-2013 Majid Khan //
// //
// This software is provided 'as-is', without any express or implied //
// warranty. In no event will the authors be held liable for any damages //
// arising from the use of this software. //
// //
// Permission is granted to anyone to use this software for any purpose, //
// including commercial applications, and to alter it and redistribute //
// it freely, subject to the following restrictions: //
// //
// 1. The origin of this software must not be misrepresented; you must //
// not claim that you wrote the original software. If you use this //
// software in a product, an acknowledgment in the product documentation //
// would be appreciated but is not required. //
// //
// 2. Altered source versions must be plainly marked as such, and must //
// not be misrepresented as being the original software. //
// //
// 3. This notice may not be removed or altered from any source //
// distribution //
// //
// PLEASE NOTE: THIS FILE MAY BE CHANGED. TO GET ORIGINAL VERSION //
// EITHER DOWNLOAD IT FROM http://www.icplusplus.com/tools/easylogging/ //
// OR PULL IT FROM https://github.com/mkhan3189/EasyLoggingPP (master branch) //
// //
///////////////////////////////////////////////////////////////////////////////////
#ifndef EASYLOGGINGPP_H
#define EASYLOGGINGPP_H
//
// Log location macros
//
#if !defined(__FILE__)
# define __FILE__ ""
#endif // !defined(__FILE__)
#if !defined(__LINE__)
# define __LINE__ 0
#endif // !defined(__LINE__)
// Appropriate function macro
#if defined(__func__)
# undef __func__
#endif // defined(__func__)
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# define __func__ __FUNCSIG__
#elif defined(__GNUC__) && (__GNUC__ >= 2)
# define __func__ __PRETTY_FUNCTION__
#elif defined(__clang__) && (__clang__ == 1)
# define __func__ __PRETTY_FUNCTION__
#else
# define __func__ ""
#endif // defined(_MSC_VER) && (_MSC_VER >= 1020)
//
// Compiler evaluation
// http://isocpp.org/blog/2013/05/gcc-4.8.1-released-c11-feature-complete
// http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx
//
// GNU
#if defined(__GNUC__)
# define _ELPP_GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
# if defined(__GXX_EXPERIMENTAL_CXX0X__)
# define _ELPP_CXX0X 1
# elif (_ELPP_GCC_VERSION >= 40801)
# define _ELPP_CXX11 1
# endif // defined(__GXX_EXPERIMENTAL_CXX0X__)
#endif // defined(__GNUC__)
// VC++
#if defined(_MSC_VER)
# if (_MSC_VER >= 1400) // VC++ 8.0
# define _ELPP_CRT_DBG_WARNINGS 1
# else
# define _ELPP_CRT_DBG_WARNINGS 0
# endif // (_MSC_VER >= 1400)
# if (_MSC_VER == 1600)
# define _ELPP_CXX0X 1
# elif (_MSC_VER == 1700)
# define _ELPP_CXX11 1
# endif // (_MSC_VER == 1600)
#else
# define _ELPP_CRT_DBG_WARNINGS 0
#endif // defined(_MSC_VER)
// Clang
#if defined(__clang__) && (__clang__ == 1)
# define _ELPP_CLANG_VERSION (__clang_major__ * 10000 \
+ __clang_minor__ * 100 \
+ __clang_patchlevel__)
# if (_ELPP_CLANG_VERSION >= 30300)
# define _ELPP_CXX11 1
# endif // (_ELPP_CLANG_VERSION >= 30300)
#endif // defined(__clang__) && (__clang__ == 1)
// MinGW
#if defined(__MINGW32__) || defined(__MINGW64__)
# define _ELPP_MINGW 1
#else
# define _ELPP_MINGW 0
#endif // defined(__MINGW32__) || defined(__MINGW64__)
#if defined(__ANDROID__)
# define _ELPP_NDK 1
#else
# define _ELPP_NDK 0
#endif // defined(__ANDROID__)
// Some special functions that are special for VC++
// This is to prevent CRT security warnings and to override deprecated methods but at the same time
// MinGW does not support some functions, so we need to make sure that proper function is used.
#if _ELPP_CRT_DBG_WARNINGS
# define SPRINTF sprintf_s
# define STRTOK(a,b,c) strtok_s(a,b,c)
#else
# define SPRINTF sprintf
# define STRTOK(a,b,c) strtok(a,b)
#endif
// std::thread availablity
#if defined(__GNUC__) && (!_ELPP_NDK) && (_ELPP_CXX0X || _ELPP_CXX11)
# define _ELPP_STD_THREAD_AVAILABLE 1
#elif defined(_MSC_VER) && (!_ELPP_NDK) && (_ELPP_CXX11)
# define _ELPP_STD_THREAD_AVAILABLE 1
#elif defined(__clang__) && (!_ELPP_NDK) && (__clang__ == 1) && (_ELPP_CXX11)
# define _ELPP_STD_THREAD_AVAILABLE 1
#else
# define _ELPP_STD_THREAD_AVAILABLE 0
#endif // defined(__GNUC__) && (_ELPP_CXX0X || _ELPP_CXX11)
// Qt
#if defined(QT_CORE_LIB)
# if (defined(QT_VERSION) && QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
# define _ELPP_QT_5 1
# else
# define _ELPP_QT_5 0
# endif // (defined(QT_VERSION) && QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
#endif // defined(QT_CORE_LIB)
//
// High-level log evaluation
//
#if (defined(_DISABLE_LOGS))
# define _ENABLE_EASYLOGGING 0
#else
# define _ENABLE_EASYLOGGING 1
#endif // (!defined(_DISABLE_LOGS))
//
// OS evaluation
//
// Windows
#if defined(_WIN32) || defined(_WIN64)
# define _ELPP_OS_WINDOWS 1
#else
# define _ELPP_OS_WINDOWS 0
#endif // defined(_WIN32) || defined(_WIN64)
// Linux
#if (defined(__linux) || defined(__linux__))
# define _ELPP_OS_LINUX 1
#else
# define _ELPP_OS_LINUX 0
#endif // (defined(__linux) || defined(__linux__))
// Mac
#if defined(__APPLE__)
# define _ELPP_OS_MAC 1
#else
# define _ELPP_OS_MAC 0
#endif // defined(__APPLE__)
// Unix
#define _ELPP_OS_UNIX ((_ELPP_OS_LINUX || _ELPP_OS_MAC) && (!_ELPP_OS_WINDOWS))
// Assembly
#if (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))) || \
(defined(_MSC_VER) && (defined(_M_IX86)))
# define _ELPP_ASSEMBLY_SUPPORTED 1
#else
# define _ELPP_ASSEMBLY_SUPPORTED 0
#endif
#if (!defined(_DISABLE_ELPP_ASSERT))
# if (defined(_STOP_ON_FIRST_ELPP_ASSERTION))
# define __EASYLOGGINGPP_ASSERT(expr, msg) if (!(expr)) { std::cerr << "EASYLOGGING++ ASSERTION FAILED (LINE: " << __LINE__ << ") [" #expr << "] with message \"" << msg << "\"" << std::endl; exit(1); }
# else
# define __EASYLOGGINGPP_ASSERT(expr, msg) if (!(expr)) { std::cerr << "EASYLOGGING++ ASSERTION FAILED (LINE: " << __LINE__ << ") [" #expr << "] with message \"" << msg << "\"" << std::endl; }
# endif // (defined(_STOP_ON_FIRST_ELPP_ASSERTION))
#else
# define __EASYLOGGINGPP_ASSERT(x, y)
#endif // (!defined(_DISABLE_ELPP_ASSERT))
#define __EASYLOGGINGPP_SUPPRESS_UNSED(x) (void)x;
#if _ELPP_OS_UNIX
// Log file permissions for unix-based systems
# define _LOG_PERMS S_IRUSR | S_IWUSR | S_IXUSR | S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IXOTH
#endif // _ELPP_OS_UNIX
#if (!defined(_DISABLE_MUTEX) && (_ENABLE_EASYLOGGING))
# define _ELPP_ENABLE_MUTEX 1
#else
# define _ELPP_ENABLE_MUTEX 0
#endif // (!defined(_DISABLE_MUTEX) && (_ENABLE_EASYLOGGING))
#if (!defined(_DISABLE_DEBUG_LOGS) && (_ENABLE_EASYLOGGING) && ((defined(_DEBUG)) || (!defined(NDEBUG))))
# define _ELPP_DEBUG_LOG 1
#else
# define _ELPP_DEBUG_LOG 0
#endif // (!defined(_DISABLE_DEBUG_LOGS) && (_ENABLE_EASYLOGGING) && ((defined(_DEBUG)) || (!defined(NDEBUG))))
#if (!defined(_DISABLE_INFO_LOGS) && (_ENABLE_EASYLOGGING))
# define _ELPP_INFO_LOG 1
#else
# define _ELPP_INFO_LOG 0
#endif // (!defined(_DISABLE_INFO_LOGS) && (_ENABLE_EASYLOGGING))
#if (!defined(_DISABLE_WARNING_LOGS) && (_ENABLE_EASYLOGGING))
# define _ELPP_WARNING_LOG 1
#else
# define _ELPP_WARNING_LOG 0
#endif // (!defined(_DISABLE_WARNING_LOGS) && (_ENABLE_EASYLOGGING))
#if (!defined(_DISABLE_ERROR_LOGS) && (_ENABLE_EASYLOGGING))
# define _ELPP_ERROR_LOG 1
#else
# define _ELPP_ERROR_LOG 0
#endif // (!defined(_DISABLE_ERROR_LOGS) && (_ENABLE_EASYLOGGING))
#if (!defined(_DISABLE_FATAL_LOGS) && (_ENABLE_EASYLOGGING))
# define _ELPP_FATAL_LOG 1
#else
# define _ELPP_FATAL_LOG 0
#endif // (!defined(_DISABLE_FATAL_LOGS) && (_ENABLE_EASYLOGGING))
#if (defined(_QUALITY_ASSURANCE) && (_ENABLE_EASYLOGGING))
# define _ELPP_QA_LOG 1
#else
# define _ELPP_QA_LOG 0
#endif // (defined(_QUALITY_ASSURANCE) && (_ENABLE_EASYLOGGING))
#if (!defined(_DISABLE_TRACE_LOGS) && (_ENABLE_EASYLOGGING))
# define _ELPP_TRACE_LOG 1
#else
# define _ELPP_TRACE_LOG 0
#endif // (!defined(_DISABLE_TRACE_LOGS) && (_ENABLE_EASYLOGGING))
#if (!defined(_DISABLE_VERBOSE_LOGS) && (_ENABLE_EASYLOGGING))
# define _ELPP_VERBOSE_LOG 1
#else
# define _ELPP_VERBOSE_LOG 0
#endif // (!defined(_DISABLE_VERBOSE_LOGS) && (_ENABLE_EASYLOGGING))
#define ELPP_FOR_EACH(variableName, initialValue, operation, limit) unsigned int variableName = initialValue; \
do { \
operation \
variableName = variableName << 1; \
if (variableName == 0) { ++variableName; } \
} while (variableName <= limit)
#define ELPP_FOR_EACH_LEVEL(variableName, initialValue, operation) \
ELPP_FOR_EACH(variableName, initialValue, operation, easyloggingpp::Level::kMaxValid)
#define ELPP_FOR_EACH_CONFIGURATION(variableName, initialValue, operation) \
ELPP_FOR_EACH(variableName, initialValue, operation, easyloggingpp::ConfigurationType::kMaxValid)
// Includes
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cwchar>
#if _ELPP_NDK
# include <sys/system_properties.h>
#endif // _ELPP_NDK
#if _ELPP_OS_UNIX
# include <sys/stat.h>
# include <sys/time.h>
# if (_ELPP_ENABLE_MUTEX)
# if (_ELPP_ASSEMBLY_SUPPORTED)
# include <sched.h>
# else
# include <pthread.h>
# endif // (_ELPP_ASSEMBLY_SUPPORTED)
# endif // (_ELPP_ENABLE_MUTEX)
#elif _ELPP_OS_WINDOWS
# include <direct.h>
# include <Windows.h>
#endif // _ELPP_OS_UNIX
#include <string>
#include <vector>
#include <map>
#include <functional>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#if (_ELPP_STD_THREAD_AVAILABLE)
# include <thread>
#endif // _ELPP_STD_THREAD_AVAILABLE
#if defined(_ELPP_STL_LOGGING)
// For logging STL based templates
# include <list>
# include <utility>
# include <queue>
# include <deque>
# include <set>
# include <bitset>
# include <stack>
#endif // defined(_ELPP_STL_LOGGING)
#if defined(QT_CORE_LIB) && defined(_ELPP_QT_LOGGING)
// For logging Qt based classes & templates
# include <QString>
# include <QVector>
# include <QList>
# include <QPair>
# include <QMap>
# include <QQueue>
# include <QSet>
# include <QLinkedList>
# include <QHash>
# include <QMultiHash>
# include <QStack>
#endif // defined(QT_CORE_LIB) && defined(_ELPP_QT_LOGGING)
namespace easyloggingpp {
namespace internal {
class NoCopy {
protected:
NoCopy(void) {}
private:
NoCopy(const NoCopy&);
NoCopy& operator=(const NoCopy&);
};
class StaticClass {
private:
StaticClass(void);
StaticClass(const StaticClass&);
StaticClass& operator=(const StaticClass&);
};
} // namespace internal
struct Level : private internal::StaticClass {
public:
enum {
All = 0, Debug = 1, Info = 2, Warning = 4, Error = 8,
Fatal = 16, Verbose = 32, QA = 64, Trace = 128, Unknown = 1010
};
static const unsigned int kMinValid = All;
static const unsigned int kMaxValid = Trace;
static std::string convertToString(unsigned int level_) {
switch (level_) {
case All:
return std::string("ALL");
case Debug:
return std::string("DEBUG");
case Info:
return std::string("INFO");
case Warning:
return std::string("WARNING");
case Error:
return std::string("ERROR");
case Fatal:
return std::string("FATAL");
case QA:
return std::string("QA");
case Verbose:
return std::string("VERBOSE");
case Trace:
return std::string("TRACE");
default:
return std::string("UNKNOWN");
}
}
static unsigned int convertFromString(const std::string& levelStr) {
if (levelStr == "all" || levelStr == "ALL") return Level::All;
if (levelStr == "debug" || levelStr == "DEBUG") return Level::Debug;
if (levelStr == "info" || levelStr == "INFO") return Level::Info;
if (levelStr == "warning" || levelStr == "WARNING") return Level::Warning;
if (levelStr == "error" || levelStr == "ERROR") return Level::Error;
if (levelStr == "fatal" || levelStr == "FATAL") return Level::Fatal;
if (levelStr == "qa" || levelStr == "QA") return Level::QA;
if (levelStr == "verbose" || levelStr == "VERBOSE") return Level::Verbose;
if (levelStr == "trace" || levelStr == "TRACE") return Level::Trace;
return Level::Unknown;
}
};
struct ConfigurationType : private internal::StaticClass {
public:
enum {
Enabled = 0, ToFile = 1, ToStandardOutput = 2, Format = 4, Filename = 8,
MillisecondsWidth = 16, PerformanceTracking = 32, RollOutSize = 64, Unknown = 1010
};
static const unsigned int kMinValid = Enabled;
static const unsigned int kMaxValid = RollOutSize;
static std::string convertToString(unsigned int configurationType_) {
switch (configurationType_) {
case Enabled:
return std::string("ENABLED");
case Filename:
return std::string("FILENAME");
case Format:
return std::string("FORMAT");
case ToFile:
return std::string("TO_FILE");
case ToStandardOutput:
return std::string("TO_STANDARD_OUTPUT");
case MillisecondsWidth:
return std::string("MILLISECONDS_WIDTH");
case PerformanceTracking:
return std::string("PERFORMANCE_TRACKING");
case RollOutSize:
return std::string("ROLL_OUT_SIZE");
default: return std::string("UNKNOWN");
}
}
static unsigned int convertFromString(const std::string& configStr) {
if (configStr == "enabled" || configStr == "ENABLED") return ConfigurationType::Enabled;
if (configStr == "to_file" || configStr == "TO_FILE") return ConfigurationType::ToFile;
if (configStr == "to_standard_output" || configStr == "TO_STANDARD_OUTPUT") return ConfigurationType::ToStandardOutput;
if (configStr == "format" || configStr == "FORMAT") return ConfigurationType::Format;
if (configStr == "filename" || configStr == "FILENAME") return ConfigurationType::Filename;
if (configStr == "milliseconds_width" || configStr == "MILLISECONDS_WIDTH") return ConfigurationType::MillisecondsWidth;
if (configStr == "performance_tracking" || configStr == "PERFORMANCE_TRACKING") return ConfigurationType::PerformanceTracking;
if (configStr == "roll_out_size" || configStr == "ROLL_OUT_SIZE") return ConfigurationType::RollOutSize;
return ConfigurationType::Unknown;
}
};
namespace internal {
struct Aspect : private internal::StaticClass {
public:
enum {
Normal = 0, Conditional = 1, Interval = 2
};
};
//!
//! Used internally. You should not need this class.
//!
class Constants : private internal::NoCopy {
public:
Constants (void) :
//
// Log level name outputs
//
LOG_INFO_LEVEL_VALUE ("INFO") ,
LOG_DEBUG_LEVEL_VALUE ("DEBUG"),
LOG_WARNING_LEVEL_VALUE("WARN"),
LOG_ERROR_LEVEL_VALUE ("ERROR"),
LOG_FATAL_LEVEL_VALUE ("FATAL"),
LOG_VERBOSE_LEVEL_VALUE("VER"),
LOG_QA_LEVEL_VALUE ("QA"),
LOG_TRACE_LEVEL_VALUE ("TRACE"),
//
// Format specifiers
//
APP_NAME_FORMAT_SPECIFIER ("%app"),
LOGGER_ID_FORMAT_SPECIFIER ("%logger"),
THREAD_ID_FORMAT_SPECIFIER ("%thread"),
LEVEL_FORMAT_SPECIFIER ("%level"),
DATE_ONLY_FORMAT_SPECIFIER ("%date"),
TIME_ONLY_FORMAT_SPECIFIER ("%time"),
DATE_TIME_FORMAT_SPECIFIER ("%datetime"),
LOCATION_FORMAT_SPECIFIER ("%loc"),
FUNCTION_FORMAT_SPECIFIER ("%func"),
USER_FORMAT_SPECIFIER ("%user"),
HOST_FORMAT_SPECIFIER ("%host"),
LOG_MESSAGE_FORMAT_SPECIFIER ("%log"),
VERBOSE_LEVEL_FORMAT_SPECIFIER ("%vlevel"),
//
// Others
//
NULL_POINTER ("nullptr"),
FORMAT_SPECIFIER_ESCAPE_CHAR ('E'),
MAX_LOG_PER_CONTAINER (100),
MAX_LOG_PER_COUNTER (100000),
DEFAULT_MILLISECOND_OFFSET (1000),
MAX_VERBOSE_LEVEL (9),
CURRENT_VERBOSE_LEVEL (0), // Set dynamically from registeredLoggers
#if _ELPP_OS_UNIX
PATH_SLASH ("/"),
#elif _ELPP_OS_WINDOWS
PATH_SLASH ("\\"),
#endif // _ELPP_OS_UNIX,
DEFAULT_LOG_FILENAME ("myeasylog.log")
{
// Trivial logger configuration - only to set format (difference: not using %logger)
std::stringstream ss;
ss << " * ALL:\n";
ss << " FORMAT = %datetime %level %log\n";
ss << "* DEBUG:\n";
ss << " FORMAT = %datetime %level [%user@%host] [%func] [%loc] %log\n";
// INFO and WARNING uses is defined by ALL
ss << "* ERROR:\n";
ss << " FORMAT = %datetime %level %log\n";
ss << "* FATAL:\n";
ss << " FORMAT = %datetime %level %log\n";
ss << "* QA:\n";
ss << " FORMAT = %datetime %level %log\n";
ss << "* VERBOSE:\n";
ss << " FORMAT = %datetime %level-%vlevel %log\n";
ss << "* TRACE:\n";
ss << " FORMAT = %datetime %level [%func] [%loc] %log\n";
DEFAULT_LOGGER_CONFIGURATION = ss.str();
} // C'tor
//
// Log level name outputs
//
const std::string LOG_INFO_LEVEL_VALUE;
const std::string LOG_DEBUG_LEVEL_VALUE;
const std::string LOG_WARNING_LEVEL_VALUE;
const std::string LOG_ERROR_LEVEL_VALUE;
const std::string LOG_FATAL_LEVEL_VALUE;
const std::string LOG_VERBOSE_LEVEL_VALUE;
const std::string LOG_QA_LEVEL_VALUE;
const std::string LOG_TRACE_LEVEL_VALUE;
//
// Format specifiers
//
const std::string APP_NAME_FORMAT_SPECIFIER;
const std::string LOGGER_ID_FORMAT_SPECIFIER;
const std::string THREAD_ID_FORMAT_SPECIFIER;
const std::string LEVEL_FORMAT_SPECIFIER;
const std::string DATE_ONLY_FORMAT_SPECIFIER;
const std::string TIME_ONLY_FORMAT_SPECIFIER;
const std::string DATE_TIME_FORMAT_SPECIFIER;
const std::string LOCATION_FORMAT_SPECIFIER;
const std::string FUNCTION_FORMAT_SPECIFIER;
const std::string USER_FORMAT_SPECIFIER;
const std::string HOST_FORMAT_SPECIFIER;
const std::string LOG_MESSAGE_FORMAT_SPECIFIER;
const std::string VERBOSE_LEVEL_FORMAT_SPECIFIER;
//
// Others
//
const std::string NULL_POINTER;
const char FORMAT_SPECIFIER_ESCAPE_CHAR;
const unsigned int MAX_LOG_PER_CONTAINER;
const unsigned int MAX_LOG_PER_COUNTER;
const unsigned int DEFAULT_MILLISECOND_OFFSET;
const int MAX_VERBOSE_LEVEL;
int CURRENT_VERBOSE_LEVEL;
const std::string PATH_SLASH;
const std::string DEFAULT_LOG_FILENAME;
std::string DEFAULT_LOGGER_CONFIGURATION;
enum kFormatFlags {
kDateOnly = 2,
kTimeOnly = 4,
kDateTime = 8,
kLoggerId = 16,
kLocation = 32,
kFunction = 64,
kUser = 128,
kHost = 256,
kLogMessage = 512,
kVerboseLevel = 1024,
kAppName = 2048,
kThreadId = 4096
};
}; // class Constants
namespace threading {
//!
//! To take care of shared resources in multi-threaded application. Used internally, you should not need it.
//!
class Mutex {
public:
#if _ELPP_ASSEMBLY_SUPPORTED
# if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
# define _ELPP_MUTEX_LOCK_GNU_ASM(lf_, old_) "movl $1,%%eax\n" \
"\txchg %%eax,%0\n" \
"\tmovl %%eax,%1\n" \
"\t" : "=m" (lf_), "=m" (old_) : : "%eax", "memory"
# define _ELPP_MUTEX_UNLOCK_GNU_ASM(lf_) "movl $0,%%eax\n" \
"\txchg %%eax,%0\n" \
"\t" : "=m" (lf_) : : "%eax", "memory"
# endif // defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
Mutex(void) : lockerFlag_(0) {
}
#else
Mutex(void) {
# if _ELPP_OS_UNIX
pthread_mutex_init(&underlyingMutex_, NULL);
# elif _ELPP_OS_WINDOWS
InitializeCriticalSection(&underlyingMutex_);
# endif // _ELPP_OS_UNIX
}
virtual ~Mutex(void) {
# if _ELPP_OS_UNIX
pthread_mutex_destroy(&underlyingMutex_);
# elif _ELPP_OS_WINDOWS
DeleteCriticalSection(&underlyingMutex_);
# endif // _ELPP_OS_UNIX
}
#endif // _ELPP_ASSEMBLY_SUPPORTED
inline void lock(void) {
#if _ELPP_ASSEMBLY_SUPPORTED
bool locked = false;
while (!locked) {
locked = tryLock();
if (!locked) {
# if _ELPP_OS_UNIX
sched_yield();
# elif _ELPP_OS_WINDOWS
Sleep(0);
# endif
}
}
#else
# if _ELPP_OS_UNIX
pthread_mutex_lock(&underlyingMutex_);
# elif _ELPP_OS_WINDOWS
EnterCriticalSection(&underlyingMutex_);
# endif // _ELPP_OS_UNIX
#endif // _ELPP_ASSEMBLY_SUPPORTED
}
inline bool tryLock(void) {
#if _ELPP_ASSEMBLY_SUPPORTED
int oldLock_;
# if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
asm volatile (_ELPP_MUTEX_LOCK_GNU_ASM(lockerFlag_, oldLock_));
# elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
int *ptrLock = &lockerFlag_;
__asm {
mov eax,1
mov ecx,ptrLock
xchg eax,[ecx]
mov oldLock_,eax
}
# endif // defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
return (oldLock_ == 0);
#else
# if _ELPP_OS_UNIX
return (pthread_mutex_trylock(&underlyingMutex_) == 0) ? true : false;
# elif _ELPP_OS_WINDOWS
return TryEnterCriticalSection(&underlyingMutex_) ? true : false;
# endif // _ELPP_OS_UNIX
#endif // _ELPP_ASSEMBLY_SUPPORTED
}
inline void unlock(void) {
#if _ELPP_ASSEMBLY_SUPPORTED
# if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
asm volatile (_ELPP_MUTEX_UNLOCK_GNU_ASM(lockerFlag_));
# elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
int *ptrLock = &lockerFlag_;
__asm {
mov eax,0
mov ecx,ptrLock
xchg eax,[ecx]
}
# endif // defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
#else
# if _ELPP_OS_UNIX
pthread_mutex_unlock(&underlyingMutex_);
# elif _ELPP_OS_WINDOWS
LeaveCriticalSection(&underlyingMutex_);
# endif // _ELPP_OS_UNIX
#endif // _ELPP_ASSEMBLY_SUPPORTED
}
private:
#if _ELPP_ASSEMBLY_SUPPORTED
int lockerFlag_;
#else
# if _ELPP_OS_UNIX
pthread_mutex_t underlyingMutex_;
# elif _ELPP_OS_WINDOWS
CRITICAL_SECTION underlyingMutex_;
# endif // _ELPP_OS_UNIX
#endif // _ELPP_ASSEMBLY_SUPPORTED
}; // class Mutex
//!
//! Scoped mutex that works same as C++11 <code>std::lock_guard<std::mutex></code>. Used internally, you should not use it.
//!
class ScopedLock : private internal::NoCopy {
public:
explicit ScopedLock(Mutex& m_) {
mutex_ = &m_;
mutex_->lock();
}
virtual ~ScopedLock(void) {
mutex_->unlock();
}
private:
Mutex* mutex_;
ScopedLock(void);
}; // class ScopedLock
//!
//! \return ID of current thread. If std::thread is available it uses get_id() otherwise if on windows it uses
//! GetCurrentThreadId() otherwise empty string. Used internally, you should not use it.
//!
inline std::string getCurrentThreadId(void) {
std::stringstream ss;
#if (_ELPP_STD_THREAD_AVAILABLE)
ss << std::this_thread::get_id();
#else
# if (_ELPP_OS_WINDOWS)
ss << GetCurrentThreadId();
# endif // (_ELPP_OS_WINDOWS)
#endif
return ss.str();
}
} // namespace threading
namespace utilities {
template <typename T>
inline void safeDelete(T*& pointer, bool checkNullity = true) {
if (checkNullity && pointer == NULL) return;
delete pointer;
pointer = NULL;
}
//!
//! String utilities class used internally. You should not use it.
//!
class StringUtils : private internal::StaticClass {
public:
static inline std::string trim(const std::string &str) {
std::size_t s = str.find_first_not_of(" \n\r\t");
std::size_t e = str.find_last_not_of(" \n\r\t");
if ((s == std::string::npos) || (e == std::string::npos)) {
return "";
}
else {
return str.substr(s, e - s + 1);
}
}
static inline bool startsWith(const std::string& str, const std::string& start) {
return (str.length() >= start.length()) && (str.compare(0, start.length(), start) == 0);
}
static inline bool endsWith(const std::string& str, const std::string& end) {
return (str.length() >= end.length()) && (str.compare(str.length() - end.length(), end.length(), end) == 0);
}
static inline std::vector<std::string>& split(const std::string& s, char delim, std::vector<std::string>& elems) {
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
static inline std::string replaceAll(const std::string& str, const std::string& replaceWhat, const std::string& replaceWith) {
if (replaceWhat == replaceWith)
return str;
std::string result = str;
std::size_t foundAt = std::string::npos;
while ((foundAt = result.find(replaceWhat)) != std::string::npos) {
result.replace(foundAt, replaceWhat.length(), replaceWith);
}
return result;
}
static inline std::string stripAllWhiteSpaces(const std::string& str) {
std::string result = replaceAll(str, " ", "");
result = replaceAll(result, "\n", "");
result = replaceAll(result, "\r", "");
result = replaceAll(result, "\t", "");
return result;
}
static inline void tolower(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
}
};
//!
//! Operating System utilities class used internally. You should not use it.
//!
class OSUtils : private internal::StaticClass {
public:
#if _ELPP_OS_WINDOWS
static const char* getWindowsEnvironmentVariable(const char* variableName) {
const DWORD bufferLen = 50;
static char buffer[bufferLen];
if (GetEnvironmentVariableA(variableName, buffer, bufferLen)) {
return buffer;
}
return NULL;
}
#endif // _ELPP_OS_WINDOWS
#if _ELPP_NDK
static std::string getProperty(const char* prop) {
char propVal[PROP_VALUE_MAX + 1];
__system_property_get(prop, propVal);
return std::string(propVal);
}
static std::string getDeviceName(void) {
std::stringstream ss;
std::string manufacturer = getProperty("ro.product.manufacturer");
std::string model = getProperty("ro.product.model");
if (manufacturer.empty() && model.empty()) {
return std::string();
}
ss << manufacturer << " " << model;
return ss.str();
}
#endif // _ELPP_NDK
// Runs command on terminal and returns the output.
// This is applicable only on linux and mac, for all other OS, an empty string is returned.
static const std::string getBashOutput(const char* command_) {
if (command_ == NULL) {
return std::string();
}
#if _ELPP_OS_UNIX && !_ELPP_NDK
FILE* proc = NULL;
if ((proc = popen(command_, "r")) == NULL) {
std::cerr << "\nUnable to run command [" << command_ << "]" << std::endl;
return std::string();
}
char hBuff[4096];
if (fgets(hBuff, sizeof(hBuff), proc) != NULL) {
pclose(proc);
if (hBuff[strlen(hBuff) - 1] == '\n') {
hBuff[strlen(hBuff) - 1] = '\0';
}
return std::string(hBuff);
}
return std::string();
#else
return std::string();
#endif // _ELPP_OS_UNIX
}
static std::string getEnvironmentVariable(const char* variableName, const char* defaultVal, const char* alternativeBashCommand = NULL) {
#if _ELPP_OS_UNIX
const char* val = getenv(variableName);
#elif _ELPP_OS_WINDOWS
const char* val = getWindowsEnvironmentVariable(variableName);
#endif // _ELPP_OS_UNIX
if ((val == NULL) || ((strcmp(val, "") == 0))) {
#if _ELPP_OS_UNIX
// Try harder on unix-based systems
std::string valBash = internal::utilities::OSUtils::getBashOutput(alternativeBashCommand);
if (valBash.empty()) {
return std::string(defaultVal);
} else {
return valBash;
}
#elif _ELPP_OS_WINDOWS
return std::string(defaultVal);
#endif // _ELPP_OS_WINDOWS
}
return std::string(val);
}
// Gets current username.
static const std::string currentUser(void) {
#if _ELPP_OS_UNIX && !_ELPP_NDK
return getEnvironmentVariable("USER", "user", "whoami");
#elif _ELPP_OS_WINDOWS
return getEnvironmentVariable("USERNAME", "user");
#elif _ELPP_NDK
return std::string("android");
#else
return std::string();
#endif // _ELPP_OS_UNIX
}
// Gets current host name or computer name.
static const std::string currentHost(void) {
#if _ELPP_OS_UNIX && !_ELPP_NDK
return getEnvironmentVariable("HOSTNAME", "unknown-host", "hostname");
#elif _ELPP_OS_WINDOWS
return getEnvironmentVariable("COMPUTERNAME", "unknown-host");
#elif _ELPP_NDK
return getDeviceName();
#else
return std::string();
#endif // _ELPP_OS_UNIX
}
// Determines whether or not provided path_ exist in current file system
static inline bool pathExists(const char* path_) {
if (path_ == NULL) {
return false;
}
#if _ELPP_OS_UNIX
struct stat st;
return (stat(path_, &st) == 0);
#elif _ELPP_OS_WINDOWS
DWORD fileType = GetFileAttributesA(path_);
if (fileType == INVALID_FILE_ATTRIBUTES) {
return false;
}
return (fileType & FILE_ATTRIBUTE_DIRECTORY) == 0 ? false : true;
#endif // _ELPP_OS_UNIX
}
// Creates path as specified
static bool createPath(const std::string& path_) {
if (path_.empty()) {
return false;
}
if (internal::utilities::OSUtils::pathExists(path_.c_str())) {
return true;
}
#if _ELPP_OS_UNIX
const char* pathDelim_ = "/";
#elif _ELPP_OS_WINDOWS
char pathDelim_[] = "\\";
#endif // _ELPP_OS_UNIX
int status = -1;
char* currPath_ = const_cast<char*>(path_.c_str());
std::string buildingPath_ = std::string();
#if _ELPP_OS_UNIX
if (path_[0] == '/') {
buildingPath_ = "/";
}
currPath_ = STRTOK(currPath_, pathDelim_, 0);
#elif _ELPP_OS_WINDOWS
// Use secure functions API
char* nextTok_;
currPath_ = STRTOK(currPath_, pathDelim_, &nextTok_);
#endif // _ELPP_OS_UNIX
while (currPath_ != NULL) {
buildingPath_.append(currPath_);
buildingPath_.append(pathDelim_);
#if _ELPP_OS_UNIX
status = mkdir(buildingPath_.c_str(), _LOG_PERMS);
currPath_ = STRTOK(NULL, pathDelim_, 0);
#elif _ELPP_OS_WINDOWS
status = _mkdir(buildingPath_.c_str());
currPath_ = STRTOK(NULL, pathDelim_, &nextTok_);
#endif // _ELPP_OS_UNIX
}
if (status == -1) {
return false;
}
return true;
}
static std::string getPathFromFilename(const std::string& fullPath_, internal::Constants* constants_) {
if (fullPath_ == "" || fullPath_.find(constants_->PATH_SLASH) == std::string::npos) {
return fullPath_;
}
std::size_t lastSlashAt = fullPath_.find_last_of(constants_->PATH_SLASH);
if (lastSlashAt == 0) {
return constants_->PATH_SLASH;
}
return fullPath_.substr(0, lastSlashAt + 1);
}
}; // class OSUtils
//!
//! Contains static functions related to log manipulation used internally. You should not use it.
//!
class LogManipulator : private internal::StaticClass {
public:
// Updates the formatSpecifier_ for currentFormat_ to value_ provided
static void updateFormatValue(const std::string& formatSpecifier_,
const std::string& value_, std::string& currentFormat_,
internal::Constants* constants_) {
std::size_t foundAt = std::string::npos;
while ((foundAt = currentFormat_.find(formatSpecifier_, foundAt + 1)) != std::string::npos){
if (currentFormat_[foundAt > 0 ? foundAt - 1 : 0] == constants_->FORMAT_SPECIFIER_ESCAPE_CHAR) {
currentFormat_.erase(foundAt > 0 ? foundAt - 1 : 0, 1);
++foundAt;
} else {
currentFormat_ = currentFormat_.replace(foundAt, formatSpecifier_.size(), value_);
return;
}
}
}
}; // class LogManipulator
//!
//! Contains utility functions related to date/time used internally. You should not use it.
//!
class DateUtils : private internal::StaticClass {
public:
#if _ELPP_OS_WINDOWS
static void gettimeofday(struct timeval *tv) {
if (tv != NULL) {
# if defined(_MSC_EXTENSIONS)
const unsigned __int64 delta_ = 11644473600000000Ui64;
# else
const unsigned __int64 delta_ = 11644473600000000ULL;
# endif // defined(_MSC_EXTENSIONS)
const double secOffSet = 0.000001;
const unsigned long usecOffSet = 1000000;
FILETIME fileTime_;
GetSystemTimeAsFileTime(&fileTime_);
unsigned __int64 present_ = 0;
present_ |= fileTime_.dwHighDateTime;
present_ = present_ << 32;
present_ |= fileTime_.dwLowDateTime;
present_ /= 10; // mic-sec
// Subtract the difference
present_ -= delta_;
tv->tv_sec = static_cast<long>(present_ * secOffSet);
tv->tv_usec = static_cast<long>(present_ % usecOffSet);
}
}
#endif // _ELPP_OS_WINDOWS
// Gets current date and time with milliseconds.
static std::string getDateTime(const std::string& bufferFormat_, unsigned int type_, internal::Constants* constants_, std::size_t milliSecondOffset_ = 1000) {