forked from rte-france/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
1441 lines (1239 loc) · 58.3 KB
/
log.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
// Copyright 2010-2022 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef OR_TOOLS_BASE_LOG_H_
#define OR_TOOLS_BASE_LOG_H_
#include <errno.h>
#include <string.h>
#include <time.h>
#include <cassert>
#include <cstddef>
#include <iosfwd>
#include <ostream>
#include <sstream>
#include <string>
#if defined(__GNUC__) && defined(__linux__)
#include <unistd.h>
#endif
#include <vector>
#include "ortools/base/flags.h"
#include "ortools/base/integral_types.h"
#include "ortools/base/log_severity.h"
#include "ortools/base/logging_export.h"
#include "ortools/base/macros.h"
#include "ortools/base/vlog_is_on.h"
#define QCHECK CHECK
#define QCHECK_EQ CHECK_EQ
#define QCHECK_GE CHECK_GE
#define QCHECK_GT CHECK_GT
#define ABSL_DIE_IF_NULL CHECK_NOTNULL
#define CHECK_OK(x) CHECK((x).ok())
#define QCHECK_OK CHECK_OK
// used by or-tools non C++ ports to bridge with the C++ layer.
void FixFlagsAndEnvironmentForSwig();
#if defined(_MSC_VER)
#define GLOG_MSVC_PUSH_DISABLE_WARNING(n) \
__pragma(warning(push)) __pragma(warning(disable : n))
#define GLOG_MSVC_POP_WARNING() __pragma(warning(pop))
#define ATTRIBUTE_NOINLINE
#define ATTRIBUTE_NORETURN __declspec(noreturn)
#else
#define GLOG_MSVC_PUSH_DISABLE_WARNING(n)
#define GLOG_MSVC_POP_WARNING()
#define ATTRIBUTE_NOINLINE __attribute__((noinline))
#define ATTRIBUTE_NORETURN __attribute__((noreturn))
#endif
// The global value of GOOGLE_STRIP_LOG. All the messages logged to
// LOG(XXX) with severity less than GOOGLE_STRIP_LOG will not be displayed.
// If it can be determined at compile time that the message will not be
// printed, the statement will be compiled out.
//
// Example: to strip out all INFO and WARNING messages, use the value
// of 2 below. To make an exception for WARNING messages from a single
// file, add "#define GOOGLE_STRIP_LOG 1" to that file _before_ including
// base/logging.h
#ifndef GOOGLE_STRIP_LOG
#define GOOGLE_STRIP_LOG 0
#endif
// GCC can be told that a certain branch is not likely to be taken (for
// instance, a CHECK failure), and use that information in static analysis.
// Giving it this information can help it optimize for the common case in
// the absence of better information (ie. -fprofile-arcs).
//
#ifndef GOOGLE_PREDICT_BRANCH_NOT_TAKEN
#if !defined(_MSC_VER)
#define GOOGLE_PREDICT_BRANCH_NOT_TAKEN(x) (__builtin_expect(x, 0))
#else
#define GOOGLE_PREDICT_BRANCH_NOT_TAKEN(x) x
#endif
#endif
#ifndef GOOGLE_PREDICT_FALSE
#if !defined(_MSC_VER)
#define GOOGLE_PREDICT_FALSE(x) (__builtin_expect(x, 0))
#else
#define GOOGLE_PREDICT_FALSE(x) x
#endif
#endif
#ifndef GOOGLE_PREDICT_TRUE
#if !defined(_MSC_VER)
#define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
#else
#define GOOGLE_PREDICT_TRUE(x) x
#endif
#endif
// Make a bunch of macros for logging. The way to log things is to stream
// things to LOG(<a particular severity level>). E.g.,
//
// LOG(INFO) << "Found " << num_cookies << " cookies";
//
// You can capture log messages in a string, rather than reporting them
// immediately:
//
// vector<string> errors;
// LOG_STRING(ERROR, &errors) << "Couldn't parse cookie #" << cookie_num;
//
// This pushes back the new error onto 'errors'; if given a null pointer,
// it reports the error via LOG(ERROR).
//
// You can also do conditional logging:
//
// LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
//
// You can also do occasional logging (log every n'th occurrence of an
// event):
//
// LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
//
// The above will cause log messages to be output on the 1st, 11th, 21st, ...
// times it is executed. Note that the special google::COUNTER value is used
// to identify which repetition is happening.
//
// You can also do occasional conditional logging (log every n'th
// occurrence of an event, when condition is satisfied):
//
// LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER
// << "th big cookie";
//
// You can log messages the first N times your code executes a line. E.g.
//
// LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";
//
// Outputs log messages for the first 20 times it is executed.
//
// Analogous SYSLOG, SYSLOG_IF, and SYSLOG_EVERY_N macros are available.
// These log to syslog as well as to the normal logs. If you use these at
// all, you need to be aware that syslog can drastically reduce performance,
// especially if it is configured for remote logging! Don't use these
// unless you fully understand this and have a concrete need to use them.
// Even then, try to minimize your use of them.
//
// There are also "debug mode" logging macros like the ones above:
//
// DLOG(INFO) << "Found cookies";
//
// DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
//
// DLOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
//
// All "debug mode" logging is compiled away to nothing for non-debug mode
// compiles.
//
// We also have
//
// LOG_ASSERT(assertion);
// DLOG_ASSERT(assertion);
//
// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
//
// There are "verbose level" logging macros. They look like
//
// VLOG(1) << "I'm printed when you run the program with --v=1 or more";
// VLOG(2) << "I'm printed when you run the program with --v=2 or more";
//
// These always log at the INFO log level (when they log at all).
// The verbose logging can also be turned on module-by-module. For instance,
// --vmodule=mapreduce=2,file=1,gfs*=3 --v=0
// will cause:
// a. VLOG(2) and lower messages to be printed from mapreduce.{h,cc}
// b. VLOG(1) and lower messages to be printed from file.{h,cc}
// c. VLOG(3) and lower messages to be printed from files prefixed with "gfs"
// d. VLOG(0) and lower messages to be printed from elsewhere
//
// The wildcarding functionality shown by (c) supports both '*' (match
// 0 or more characters) and '?' (match any single character) wildcards.
//
// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
//
// if (VLOG_IS_ON(2)) {
// // do some logging preparation and logging
// // that can't be accomplished with just VLOG(2) << ...;
// }
//
// There is also a VLOG_EVERY_N "verbose level"
// condition macros for sample cases, when some extra computation and
// preparation for logs is not needed.
// VLOG_EVERY_N(1, 10)
// << "I'm printed every 10th occurrence, and when you run the program "
// "with --v=1 or more. Present occurrence is " << google::COUNTER;
//
// The supported severity levels for macros that allow you to specify one
// are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
// Note that messages of a given severity are logged not only in the
// logfile for that severity, but also in all logfiles of lower severity.
// E.g., a message of severity FATAL will be logged to the logfiles of
// severity FATAL, ERROR, WARNING, and INFO.
//
// There is also the special severity of DFATAL, which logs FATAL in
// debug mode, ERROR in normal mode.
//
// Very important: logging a message at the FATAL severity level causes
// the program to terminate (after the message is logged).
//
// Unless otherwise specified, logs will be written to the filename
// "<program name>.<hostname>.<user name>.log.<severity level>.", followed
// by the date, time, and pid (you can't prevent the date, time, and pid
// from being in the filename).
//
// The logging code takes two flags:
// --v=# set the verbose level
// --logtostderr log all the messages to stderr instead of to logfiles
// LOG LINE PREFIX FORMAT
//
// Log lines have this form:
//
// Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
//
// where the fields are defined as follows:
//
// L A single character, representing the log level
// (eg 'I' for INFO)
// mm The month (zero padded; ie May is '05')
// dd The day (zero padded)
// hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds
// threadid The space-padded thread ID as returned by GetTID()
// (this matches the PID on Linux)
// file The file name
// line The line number
// msg The user-supplied message
//
// Example:
//
// I1103 11:57:31.739339 24395 google.cc:2341] Command line: ./some_prog
// I1103 11:57:31.739403 24395 google.cc:2342] Process id 24395
//
// NOTE: although the microseconds are useful for comparing events on
// a single machine, clocks on different machines may not be well
// synchronized. Hence, use caution when comparing the low bits of
// timestamps from different machines.
// Log messages below the GOOGLE_STRIP_LOG level will be compiled away for
// security reasons. See LOG(severtiy) below.
// A few definitions of macros that don't generate much code. Since
// LOG(INFO) and its ilk are used all over our code, it's
// better to have compact code for these operations.
#if GOOGLE_STRIP_LOG == 0
#define COMPACT_GOOGLE_LOG_INFO google::LogMessage(__FILE__, __LINE__)
#define LOG_TO_STRING_INFO(message) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_INFO, message)
#else
#define COMPACT_GOOGLE_LOG_INFO google::NullStream()
#define LOG_TO_STRING_INFO(message) google::NullStream()
#endif
#if GOOGLE_STRIP_LOG <= 1
#define COMPACT_GOOGLE_LOG_WARNING \
google::LogMessage(__FILE__, __LINE__, google::GLOG_WARNING)
#define LOG_TO_STRING_WARNING(message) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_WARNING, message)
#else
#define COMPACT_GOOGLE_LOG_WARNING google::NullStream()
#define LOG_TO_STRING_WARNING(message) google::NullStream()
#endif
#if GOOGLE_STRIP_LOG <= 2
#define COMPACT_GOOGLE_LOG_ERROR \
google::LogMessage(__FILE__, __LINE__, google::GLOG_ERROR)
#define LOG_TO_STRING_ERROR(message) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_ERROR, message)
#else
#define COMPACT_GOOGLE_LOG_ERROR google::NullStream()
#define LOG_TO_STRING_ERROR(message) google::NullStream()
#endif
#if GOOGLE_STRIP_LOG <= 3
#define COMPACT_GOOGLE_LOG_FATAL google::LogMessageFatal(__FILE__, __LINE__)
#define LOG_TO_STRING_FATAL(message) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_FATAL, message)
#else
#define COMPACT_GOOGLE_LOG_FATAL google::NullStreamFatal()
#define LOG_TO_STRING_FATAL(message) google::NullStreamFatal()
#endif
#define COMPACT_GOOGLE_LOG_QFATAL COMPACT_GOOGLE_LOG_FATAL
#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
#define DCHECK_IS_ON() 0
#else
#define DCHECK_IS_ON() 1
#endif
// For DFATAL, we want to use LogMessage (as opposed to
// LogMessageFatal), to be consistent with the original behavior.
#if !DCHECK_IS_ON()
#define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_ERROR
#elif GOOGLE_STRIP_LOG <= 3
#define COMPACT_GOOGLE_LOG_DFATAL \
google::LogMessage(__FILE__, __LINE__, google::GLOG_FATAL)
#else
#define COMPACT_GOOGLE_LOG_DFATAL google::NullStreamFatal()
#endif
#define GOOGLE_LOG_INFO(counter) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_INFO, counter, \
&google::LogMessage::SendToLog)
#define SYSLOG_INFO(counter) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_INFO, counter, \
&google::LogMessage::SendToSyslogAndLog)
#define GOOGLE_LOG_WARNING(counter) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_WARNING, counter, \
&google::LogMessage::SendToLog)
#define SYSLOG_WARNING(counter) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_WARNING, counter, \
&google::LogMessage::SendToSyslogAndLog)
#define GOOGLE_LOG_ERROR(counter) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_ERROR, counter, \
&google::LogMessage::SendToLog)
#define SYSLOG_ERROR(counter) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_ERROR, counter, \
&google::LogMessage::SendToSyslogAndLog)
#define GOOGLE_LOG_FATAL(counter) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_FATAL, counter, \
&google::LogMessage::SendToLog)
#define SYSLOG_FATAL(counter) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_FATAL, counter, \
&google::LogMessage::SendToSyslogAndLog)
#define GOOGLE_LOG_DFATAL(counter) \
google::LogMessage(__FILE__, __LINE__, google::DFATAL_LEVEL, counter, \
&google::LogMessage::SendToLog)
#define SYSLOG_DFATAL(counter) \
google::LogMessage(__FILE__, __LINE__, google::DFATAL_LEVEL, counter, \
&google::LogMessage::SendToSyslogAndLog)
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || \
defined(__CYGWIN__) || defined(__CYGWIN32__)
// A very useful logging macro to log windows errors:
#define LOG_SYSRESULT(result) \
if (FAILED(HRESULT_FROM_WIN32(result))) { \
LPSTR message = nullptr; \
LPSTR msg = reinterpret_cast<LPSTR>(&message); \
DWORD message_length = FormatMessageA( \
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, \
result, 0, msg, 100, nullptr); \
if (message_length > 0) { \
google::LogMessage(__FILE__, __LINE__, google::GLOG_ERROR, 0, \
&google::LogMessage::SendToLog) \
.stream() \
<< reinterpret_cast<const char*>(message); \
LocalFree(message); \
} \
}
#endif
// We use the preprocessor's merging operator, "##", so that, e.g.,
// LOG(INFO) becomes the token GOOGLE_LOG_INFO. There's some funny
// subtle difference between ostream member streaming functions (e.g.,
// ostream::operator<<(int) and ostream non-member streaming functions
// (e.g., ::operator<<(ostream&, string&): it turns out that it's
// impossible to stream something like a string directly to an unnamed
// ostream. We employ a neat hack by calling the stream() member
// function of LogMessage which seems to avoid the problem.
#define LOG(severity) COMPACT_GOOGLE_LOG_##severity.stream()
#define SYSLOG(severity) SYSLOG_##severity(0).stream()
namespace google {
// Initialize google's logging library. You will see the program name
// specified by argv0 in log outputs.
GOOGLE_GLOG_DLL_DECL void InitGoogleLogging(const char* argv0);
// Shutdown google's logging library.
GOOGLE_GLOG_DLL_DECL void ShutdownGoogleLogging();
// Install a function which will be called after LOG(FATAL).
GOOGLE_GLOG_DLL_DECL void InstallFailureFunction(void (*fail_func)());
class LogSink; // defined below
// If a non-null sink pointer is given, we push this message to that sink.
// For LOG_TO_SINK we then do normal LOG(severity) logging as well.
// This is useful for capturing messages and passing/storing them
// somewhere more specific than the global log of the process.
// Argument types:
// LogSink* sink;
// LogSeverity severity;
// The cast is to disambiguate null pointer arguments.
#define LOG_TO_SINK(sink, severity) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_##severity, \
static_cast<google::LogSink*>(sink), true) \
.stream()
#define LOG_TO_SINK_BUT_NOT_TO_LOGFILE(sink, severity) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_##severity, \
static_cast<google::LogSink*>(sink), false) \
.stream()
// If a non-null string pointer is given, we write this message to that string.
// We then do normal LOG(severity) logging as well. This is useful for capturing
// messages and storing them somewhere more specific than the global log of the
// process.
// Argument types:
// string* message;
// LogSeverity severity;
// The cast is to disambiguate null pointer arguments.
// NOTE: LOG(severity) expands to LogMessage().stream() for the specified
// severity.
#define LOG_TO_STRING(severity, message) \
LOG_TO_STRING_##severity(static_cast<string*>(message)).stream()
// If a non-null pointer is given, we push the message onto the end
// of a vector of strings; otherwise, we report it with LOG(severity).
// This is handy for capturing messages and perhaps passing them back
// to the caller, rather than reporting them immediately.
// Argument types:
// LogSeverity severity;
// vector<string> *outvec;
// The cast is to disambiguate null pointer arguments.
#define LOG_STRING(severity, outvec) \
LOG_TO_STRING_##severity(static_cast<std::vector<std::string>*>(outvec)) \
.stream()
#define LOG_IF(severity, condition) \
static_cast<void>(0), \
!(condition) ? (void)0 : google::LogMessageVoidify() & LOG(severity)
#define SYSLOG_IF(severity, condition) \
static_cast<void>(0), \
!(condition) ? (void)0 : google::LogMessageVoidify() & SYSLOG(severity)
#define LOG_ASSERT(condition) \
LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
#define SYSLOG_ASSERT(condition) \
SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
// CHECK dies with a fatal error if condition is not true. It is *not*
// controlled by DCHECK_IS_ON(), so the check will be executed regardless of
// compilation mode. Therefore, it is safe to do things like:
// CHECK(fp->Write(x) == 4)
#define CHECK(condition) \
LOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN(!(condition))) \
<< "Check failed: " #condition " "
// A container for a string pointer which can be evaluated to a bool -
// true iff the pointer is null.
struct CheckOpString {
CheckOpString(std::string* str) : str_(str) {}
// No destructor: if str_ is non-null, we're about to LOG(FATAL),
// so there's no point in cleaning up str_.
operator bool() const {
return GOOGLE_PREDICT_BRANCH_NOT_TAKEN(str_ != nullptr);
}
std::string* str_;
};
// Function is overloaded for integral types to allow static const
// integrals declared in classes and not defined to be used as arguments to
// CHECK* macros. It's not encouraged though.
template <class T>
inline const T& GetReferenceableValue(const T& t) {
return t;
}
inline char GetReferenceableValue(char t) { return t; }
inline unsigned char GetReferenceableValue(unsigned char t) { return t; }
inline signed char GetReferenceableValue(signed char t) { return t; }
inline int16_t GetReferenceableValue(int16_t t) { return t; }
inline uint16_t GetReferenceableValue(uint16_t t) { return t; }
inline int GetReferenceableValue(int t) { return t; }
inline unsigned int GetReferenceableValue(unsigned int t) { return t; }
inline int64_t GetReferenceableValue(int64_t t) { return t; }
inline uint64_t GetReferenceableValue(uint64_t t) { return t; }
// This is a dummy class to define the following operator.
struct DummyClassToDefineOperator {};
} // namespace google
// Define global operator<< to declare using ::operator<<.
// This declaration will allow use to use CHECK macros for user
// defined classes which have operator<< (e.g., stl_logging.h).
inline std::ostream& operator<<(std::ostream& out,
const google::DummyClassToDefineOperator&) {
return out;
}
namespace google {
// This formats a value for a failing CHECK_XX statement. Ordinarily,
// it uses the definition for operator<<, with a few special cases below.
template <typename T>
inline void MakeCheckOpValueString(std::ostream* os, const T& v) {
(*os) << v;
}
// Overrides for char types provide readable values for unprintable
// characters.
template <>
GOOGLE_GLOG_DLL_DECL void MakeCheckOpValueString(std::ostream* os,
const char& v);
template <>
GOOGLE_GLOG_DLL_DECL void MakeCheckOpValueString(std::ostream* os,
const signed char& v);
template <>
GOOGLE_GLOG_DLL_DECL void MakeCheckOpValueString(std::ostream* os,
const unsigned char& v);
// Build the error message string. Specify no inlining for code size.
template <typename T1, typename T2>
std::string* MakeCheckOpString(const T1& v1, const T2& v2,
const char* exprtext) ATTRIBUTE_NOINLINE;
// Provide printable value for std::nullptr_t
template <>
GOOGLE_GLOG_DLL_DECL void MakeCheckOpValueString(std::ostream* os,
const std::nullptr_t& v);
namespace base {
namespace internal {
// If "s" is less than INFO, returns INFO.
// If "s" is greater than FATAL, returns
// ERROR. Otherwise, returns "s".
LogSeverity NormalizeSeverity(LogSeverity s);
} // namespace internal
// A helper class for formatting "expr (V1 vs. V2)" in a CHECK_XX
// statement. See MakeCheckOpString for sample usage. Other
// approaches were considered: use of a template method (e.g.,
// base::BuildCheckOpString(exprtext, base::Print<T1>, &v1,
// base::Print<T2>, &v2), however this approach has complications
// related to volatile arguments and function-pointer arguments).
class GOOGLE_GLOG_DLL_DECL CheckOpMessageBuilder {
public:
// Inserts "exprtext" and " (" to the stream.
explicit CheckOpMessageBuilder(const char* exprtext);
// Deletes "stream_".
~CheckOpMessageBuilder();
// For inserting the first variable.
std::ostream* ForVar1() { return stream_; }
// For inserting the second variable (adds an intermediate " vs. ").
std::ostream* ForVar2();
// Get the result (inserts the closing ")").
std::string* NewString();
private:
std::ostringstream* stream_;
};
} // namespace base
template <typename T1, typename T2>
std::string* MakeCheckOpString(const T1& v1, const T2& v2,
const char* exprtext) {
base::CheckOpMessageBuilder comb(exprtext);
MakeCheckOpValueString(comb.ForVar1(), v1);
MakeCheckOpValueString(comb.ForVar2(), v2);
return comb.NewString();
}
// Helper functions for CHECK_OP macro.
// The (int, int) specialization works around the issue that the compiler
// will not instantiate the template version of the function on values of
// unnamed enum type - see comment below.
#define DEFINE_CHECK_OP_IMPL(name, op) \
template <typename T1, typename T2> \
inline std::string* name##Impl(const T1& v1, const T2& v2, \
const char* exprtext) { \
if (GOOGLE_PREDICT_TRUE(v1 op v2)) \
return nullptr; \
else \
return MakeCheckOpString(v1, v2, exprtext); \
} \
inline std::string* name##Impl(int v1, int v2, const char* exprtext) { \
return name##Impl<int, int>(v1, v2, exprtext); \
}
// We use the full name Check_EQ, Check_NE, etc. in case the file including
// base/logging.h provides its own #defines for the simpler names EQ, NE, etc.
// This happens if, for example, those are used as token names in a
// yacc grammar.
DEFINE_CHECK_OP_IMPL(Check_EQ,
==) // Compilation error with CHECK_EQ(nullptr, x)?
DEFINE_CHECK_OP_IMPL(Check_NE, !=) // Use CHECK(x == nullptr) instead.
DEFINE_CHECK_OP_IMPL(Check_LE, <=)
DEFINE_CHECK_OP_IMPL(Check_LT, <)
DEFINE_CHECK_OP_IMPL(Check_GE, >=)
DEFINE_CHECK_OP_IMPL(Check_GT, >)
#undef DEFINE_CHECK_OP_IMPL
// Helper macro for binary operators.
// Don't use this macro directly in your code, use CHECK_EQ et al below.
#if defined(STATIC_ANALYSIS)
// Only for static analysis tool to know that it is equivalent to assert
#define CHECK_OP_LOG(name, op, val1, val2, log) CHECK((val1)op(val2))
#elif DCHECK_IS_ON()
// In debug mode, avoid constructing CheckOpStrings if possible,
// to reduce the overhead of CHECK statements by 2x.
// Real DCHECK-heavy tests have seen 1.5x speedups.
// The meaning of "string" might be different between now and
// when this macro gets invoked (e.g., if someone is experimenting
// with other string implementations that get defined after this
// file is included). Save the current meaning now and use it
// in the macro.
typedef std::string _Check_string;
#define CHECK_OP_LOG(name, op, val1, val2, log) \
while (google::_Check_string* _result = google::Check##name##Impl( \
google::GetReferenceableValue(val1), \
google::GetReferenceableValue(val2), #val1 " " #op " " #val2)) \
log(__FILE__, __LINE__, google::CheckOpString(_result)).stream()
#else
// In optimized mode, use CheckOpString to hint to compiler that
// the while condition is unlikely.
#define CHECK_OP_LOG(name, op, val1, val2, log) \
while (google::CheckOpString _result = google::Check##name##Impl( \
google::GetReferenceableValue(val1), \
google::GetReferenceableValue(val2), #val1 " " #op " " #val2)) \
log(__FILE__, __LINE__, _result).stream()
#endif // STATIC_ANALYSIS, DCHECK_IS_ON()
#if GOOGLE_STRIP_LOG <= 3
#define CHECK_OP(name, op, val1, val2) \
CHECK_OP_LOG(name, op, val1, val2, google::LogMessageFatal)
#else
#define CHECK_OP(name, op, val1, val2) \
CHECK_OP_LOG(name, op, val1, val2, google::NullStreamFatal)
#endif // STRIP_LOG <= 3
// Equality/Inequality checks - compare two values, and log a FATAL message
// including the two values when the result is not as expected. The values
// must have operator<<(ostream, ...) defined.
//
// You may append to the error message like so:
// CHECK_NE(1, 2) << ": The world must be ending!";
//
// We are very careful to ensure that each argument is evaluated exactly
// once, and that anything which is legal to pass as a function argument is
// legal here. In particular, the arguments may be temporary expressions
// which will end up being destroyed at the end of the apparent statement,
// for example:
// CHECK_EQ(string("abc")[1], 'b');
//
// WARNING: These don't compile correctly if one of the arguments is a pointer
// and the other is NULL. To work around this, simply static_cast NULL to the
// type of the desired pointer.
#define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2)
#define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2)
#define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2)
#define CHECK_LT(val1, val2) CHECK_OP(_LT, <, val1, val2)
#define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2)
#define CHECK_GT(val1, val2) CHECK_OP(_GT, >, val1, val2)
// Check that the input is non-null. This very useful in constructor initializer
// lists.
#define CHECK_NOTNULL(val) \
google::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non null", (val))
// Helper functions for string comparisons.
// To avoid bloat, the definitions are in logging.cc.
#define DECLARE_CHECK_STROP_IMPL(func, expected) \
GOOGLE_GLOG_DLL_DECL std::string* Check##func##expected##Impl( \
const char* s1, const char* s2, const char* names);
DECLARE_CHECK_STROP_IMPL(strcmp, true)
DECLARE_CHECK_STROP_IMPL(strcmp, false)
DECLARE_CHECK_STROP_IMPL(strcasecmp, true)
DECLARE_CHECK_STROP_IMPL(strcasecmp, false)
#undef DECLARE_CHECK_STROP_IMPL
// Helper macro for string comparisons.
// Don't use this macro directly in your code, use CHECK_STREQ et al below.
#define CHECK_STROP(func, op, expected, s1, s2) \
while (google::CheckOpString _result = google::Check##func##expected##Impl( \
(s1), (s2), #s1 " " #op " " #s2)) \
LOG(FATAL) << *_result.str_
// String (char*) equality/inequality checks.
// CASE versions are case-insensitive.
//
// Note that "s1" and "s2" may be temporary strings which are destroyed
// by the compiler at the end of the current "full expression"
// (e.g. CHECK_STREQ(Foo().c_str(), Bar().c_str())).
#define CHECK_STREQ(s1, s2) CHECK_STROP(strcmp, ==, true, s1, s2)
#define CHECK_STRNE(s1, s2) CHECK_STROP(strcmp, !=, false, s1, s2)
#define CHECK_STRCASEEQ(s1, s2) CHECK_STROP(strcasecmp, ==, true, s1, s2)
#define CHECK_STRCASENE(s1, s2) CHECK_STROP(strcasecmp, !=, false, s1, s2)
#define CHECK_INDEX(I, A) CHECK(I < (sizeof(A) / sizeof(A[0])))
#define CHECK_BOUND(B, A) CHECK(B <= (sizeof(A) / sizeof(A[0])))
#define CHECK_DOUBLE_EQ(val1, val2) \
do { \
CHECK_LE((val1), (val2) + 0.000000000000001L); \
CHECK_GE((val1), (val2)-0.000000000000001L); \
} while (0)
#define CHECK_NEAR(val1, val2, margin) \
do { \
CHECK_LE((val1), (val2) + (margin)); \
CHECK_GE((val1), (val2) - (margin)); \
} while (0)
// perror()..googly style!
//
// PLOG() and PLOG_IF() and PCHECK() behave exactly like their LOG* and
// CHECK equivalents with the addition that they postpend a description
// of the current state of errno to their output lines.
#define PLOG(severity) GOOGLE_PLOG(severity, 0).stream()
#define GOOGLE_PLOG(severity, counter) \
google::ErrnoLogMessage(__FILE__, __LINE__, google::GLOG_##severity, \
counter, &google::LogMessage::SendToLog)
#define PLOG_IF(severity, condition) \
static_cast<void>(0), \
!(condition) ? (void)0 : google::LogMessageVoidify() & PLOG(severity)
// A CHECK() macro that postpends errno if the condition is false. E.g.
//
// if (poll(fds, nfds, timeout) == -1) { PCHECK(errno == EINTR); ... }
#define PCHECK(condition) \
PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN(!(condition))) \
<< "Check failed: " #condition " "
// A CHECK() macro that lets you assert the success of a function that
// returns -1 and sets errno in case of an error. E.g.
//
// CHECK_ERR(mkdir(path, 0700));
//
// or
//
// int fd = open(filename, flags); CHECK_ERR(fd) << ": open " << filename;
#define CHECK_ERR(invocation) \
PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
<< #invocation
// Use macro expansion to create, for each use of LOG_EVERY_N(), static
// variables with the __LINE__ expansion as part of the variable name.
#define LOG_EVERY_N_VARNAME(base, line) LOG_EVERY_N_VARNAME_CONCAT(base, line)
#define LOG_EVERY_N_VARNAME_CONCAT(base, line) base##line
#define LOG_OCCURRENCES LOG_EVERY_N_VARNAME(occurrences_, __LINE__)
#define LOG_OCCURRENCES_MOD_N LOG_EVERY_N_VARNAME(occurrences_mod_n_, __LINE__)
#define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
++LOG_OCCURRENCES; \
if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
if (LOG_OCCURRENCES_MOD_N == 1) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_##severity, \
LOG_OCCURRENCES, &what_to_do) \
.stream()
#define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
++LOG_OCCURRENCES; \
if (condition && \
((LOG_OCCURRENCES_MOD_N = (LOG_OCCURRENCES_MOD_N + 1) % n) == (1 % n))) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_##severity, \
LOG_OCCURRENCES, &what_to_do) \
.stream()
#define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
++LOG_OCCURRENCES; \
if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
if (LOG_OCCURRENCES_MOD_N == 1) \
google::ErrnoLogMessage(__FILE__, __LINE__, google::GLOG_##severity, \
LOG_OCCURRENCES, &what_to_do) \
.stream()
#define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
static int LOG_OCCURRENCES = 0; \
if (LOG_OCCURRENCES <= n) ++LOG_OCCURRENCES; \
if (LOG_OCCURRENCES <= n) \
google::LogMessage(__FILE__, __LINE__, google::GLOG_##severity, \
LOG_OCCURRENCES, &what_to_do) \
.stream()
namespace logging_internal {
template <bool>
struct CompileAssert {};
struct CrashReason;
} // namespace logging_internal
#define LOG_EVERY_N(severity, n) \
SOME_KIND_OF_LOG_EVERY_N(severity, (n), google::LogMessage::SendToLog)
#define SYSLOG_EVERY_N(severity, n) \
SOME_KIND_OF_LOG_EVERY_N(severity, (n), \
google::LogMessage::SendToSyslogAndLog)
#define PLOG_EVERY_N(severity, n) \
SOME_KIND_OF_PLOG_EVERY_N(severity, (n), google::LogMessage::SendToLog)
#define LOG_FIRST_N(severity, n) \
SOME_KIND_OF_LOG_FIRST_N(severity, (n), google::LogMessage::SendToLog)
#define LOG_IF_EVERY_N(severity, condition, n) \
SOME_KIND_OF_LOG_IF_EVERY_N(severity, (condition), (n), \
google::LogMessage::SendToLog)
// We want the special COUNTER value available for LOG_EVERY_X()'ed messages
enum PRIVATE_Counter { COUNTER };
#ifdef _MSC_VER
// wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
// substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
// to keep using this syntax, we define this macro to do the same thing
// as COMPACT_GOOGLE_LOG_ERROR.
#define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
#define SYSLOG_0 SYSLOG_ERROR
#define LOG_TO_STRING_0 LOG_TO_STRING_ERROR
// Needed for LOG_IS_ON(ERROR).
const LogSeverity GLOG_0 = GLOG_ERROR;
#endif
// Plus some debug-logging macros that get compiled to nothing for production
#if DCHECK_IS_ON()
#define DLOG(severity) LOG(severity)
#define DVLOG(verboselevel) VLOG(verboselevel)
#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
#define DLOG_EVERY_N(severity, n) LOG_EVERY_N(severity, n)
#define DLOG_IF_EVERY_N(severity, condition, n) \
LOG_IF_EVERY_N(severity, condition, n)
#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
// debug-only checking. executed if DCHECK_IS_ON().
#define DCHECK(condition) CHECK(condition)
#define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
#define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
#define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
#define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
#define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
#define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
#define DCHECK_NOTNULL(val) CHECK_NOTNULL(val)
#define DCHECK_STREQ(str1, str2) CHECK_STREQ(str1, str2)
#define DCHECK_STRCASEEQ(str1, str2) CHECK_STRCASEEQ(str1, str2)
#define DCHECK_STRNE(str1, str2) CHECK_STRNE(str1, str2)
#define DCHECK_STRCASENE(str1, str2) CHECK_STRCASENE(str1, str2)
#else // !DCHECK_IS_ON()
#define DLOG(severity) \
static_cast<void>(0), \
true ? (void)0 : google::LogMessageVoidify() & LOG(severity)
#define DVLOG(verboselevel) \
static_cast<void>(0), (true || !VLOG_IS_ON(verboselevel)) \
? (void)0 \
: google::LogMessageVoidify() & LOG(INFO)
#define DLOG_IF(severity, condition) \
static_cast<void>(0), (true || !(condition)) \
? (void)0 \
: google::LogMessageVoidify() & LOG(severity)
#define DLOG_EVERY_N(severity, n) \
static_cast<void>(0), \
true ? (void)0 : google::LogMessageVoidify() & LOG(severity)
#define DLOG_IF_EVERY_N(severity, condition, n) \
static_cast<void>(0), (true || !(condition)) \
? (void)0 \
: google::LogMessageVoidify() & LOG(severity)
#define DLOG_ASSERT(condition) \
static_cast<void>(0), true ? (void)0 : LOG_ASSERT(condition)
// MSVC warning C4127: conditional expression is constant
#define DCHECK(condition) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK(condition)
#define DCHECK_EQ(val1, val2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_EQ(val1, val2)
#define DCHECK_NE(val1, val2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_NE(val1, val2)
#define DCHECK_LE(val1, val2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_LE(val1, val2)
#define DCHECK_LT(val1, val2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_LT(val1, val2)
#define DCHECK_GE(val1, val2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_GE(val1, val2)
#define DCHECK_GT(val1, val2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_GT(val1, val2)
// You may see warnings in release mode if you don't use the return
// value of DCHECK_NOTNULL. Please just use DCHECK for such cases.
#define DCHECK_NOTNULL(val) (val)
#define DCHECK_STREQ(str1, str2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_STREQ(str1, str2)
#define DCHECK_STRCASEEQ(str1, str2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_STRCASEEQ(str1, str2)
#define DCHECK_STRNE(str1, str2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_STRNE(str1, str2)
#define DCHECK_STRCASENE(str1, str2) \
GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
while (false) GLOG_MSVC_POP_WARNING() CHECK_STRCASENE(str1, str2)
#endif // DCHECK_IS_ON()
// Log only in verbose mode.
#define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel))
#define VLOG_EVERY_N(verboselevel, n) \
LOG_IF_EVERY_N(INFO, VLOG_IS_ON(verboselevel), n)
namespace base_logging {
// LogMessage::LogStream is a std::ostream backed by this streambuf.
// This class ignores overflow and leaves two bytes at the end of the
// buffer to allow for a '\n' and '\0'.
class GOOGLE_GLOG_DLL_DECL LogStreamBuf : public std::streambuf {
public:
// REQUIREMENTS: "len" must be >= 2 to account for the '\n' and '\0'.
LogStreamBuf(char* buf, int len) { setp(buf, buf + len - 2); }
// This effectively ignores overflow.
virtual int_type overflow(int_type ch) { return ch; }
// Legacy public ostrstream method.
size_t pcount() const { return pptr() - pbase(); }
char* pbase() const { return std::streambuf::pbase(); }
};
} // namespace base_logging
//
// This class more or less represents a particular log message. You
// create an instance of LogMessage and then stream stuff to it.
// When you finish streaming to it, ~LogMessage is called and the
// full message gets streamed to the appropriate destination.
//
// You shouldn't actually use LogMessage's constructor to log things,
// though. You should use the LOG() macro (and variants thereof)
// above.
class GOOGLE_GLOG_DLL_DECL LogMessage {
public:
enum {
// Passing kNoLogPrefix for the line number disables the
// log-message prefix. Useful for using the LogMessage
// infrastructure as a printing utility. See also the --log_prefix
// flag for controlling the log-message prefix on an
// application-wide basis.
kNoLogPrefix = -1
};
// LogStream inherit from non-DLL-exported class (std::ostrstream)
// and VC++ produces a warning for this situation.
// However, MSDN says "C4275 can be ignored in Microsoft Visual C++
// 2005 if you are deriving from a type in the Standard C++ Library"
// http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
// Let's just ignore the warning.
GLOG_MSVC_PUSH_DISABLE_WARNING(4275)
class GOOGLE_GLOG_DLL_DECL LogStream : public std::ostream {
GLOG_MSVC_POP_WARNING()
public:
LogStream(char* buf, int len, int ctr)
: std::ostream(nullptr), streambuf_(buf, len), ctr_(ctr), self_(this) {
rdbuf(&streambuf_);
}
int ctr() const { return ctr_; }
void set_ctr(int ctr) { ctr_ = ctr; }
LogStream* self() const { return self_; }