OR-Tools  8.1
raw_logging.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_BASE_RAW_LOGGING_H_
15 #define OR_TOOLS_BASE_RAW_LOGGING_H_
16 
19 
20 namespace google {
21 
22 // This is similar to LOG(severity) << format... and VLOG(level) << format..,
23 // but
24 // * it is to be used ONLY by low-level modules that can't use normal LOG()
25 // * it is desiged to be a low-level logger that does not allocate any
26 // memory and does not need any locks, hence:
27 // * it logs straight and ONLY to STDERR w/o buffering
28 // * it uses an explicit format and arguments list
29 // * it will silently chop off really long message strings
30 // Usage example:
31 // RAW_LOG(ERROR, "Failed foo with %i: %s", status, error);
32 // RAW_VLOG(3, "status is %i", status);
33 // These will print an almost standard log lines like this to stderr only:
34 // E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file
35 // I0821 211317 file.cc:142] RAW: status is 20
36 #define RAW_LOG(severity, ...) \
37  do { \
38  switch (google::GLOG_##severity) { \
39  case 0: \
40  RAW_LOG_INFO(__VA_ARGS__); \
41  break; \
42  case 1: \
43  RAW_LOG_WARNING(__VA_ARGS__); \
44  break; \
45  case 2: \
46  RAW_LOG_ERROR(__VA_ARGS__); \
47  break; \
48  case 3: \
49  RAW_LOG_FATAL(__VA_ARGS__); \
50  break; \
51  default: \
52  break; \
53  } \
54  } while (0)
55 
56 // The following STRIP_LOG testing is performed in the header file so that it's
57 // possible to completely compile out the logging code and the log messages.
58 #if STRIP_LOG == 0
59 #define RAW_VLOG(verboselevel, ...) \
60  do { \
61  if (VLOG_IS_ON(verboselevel)) { \
62  RAW_LOG_INFO(__VA_ARGS__); \
63  } \
64  } while (0)
65 #else
66 #define RAW_VLOG(verboselevel, ...) RawLogStub__(0, __VA_ARGS__)
67 #endif // STRIP_LOG == 0
68 
69 #if STRIP_LOG == 0
70 #define RAW_LOG_INFO(...) \
71  google::RawLog__(google::GLOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
72 #else
73 #define RAW_LOG_INFO(...) google::RawLogStub__(0, __VA_ARGS__)
74 #endif // STRIP_LOG == 0
75 
76 #if STRIP_LOG <= 1
77 #define RAW_LOG_WARNING(...) \
78  google::RawLog__(google::GLOG_WARNING, __FILE__, __LINE__, __VA_ARGS__)
79 #else
80 #define RAW_LOG_WARNING(...) google::RawLogStub__(0, __VA_ARGS__)
81 #endif // STRIP_LOG <= 1
82 
83 #if STRIP_LOG <= 2
84 #define RAW_LOG_ERROR(...) \
85  google::RawLog__(google::GLOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
86 #else
87 #define RAW_LOG_ERROR(...) google::RawLogStub__(0, __VA_ARGS__)
88 #endif // STRIP_LOG <= 2
89 
90 #if STRIP_LOG <= 3
91 #define RAW_LOG_FATAL(...) \
92  google::RawLog__(google::GLOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
93 #else
94 #define RAW_LOG_FATAL(...) \
95  do { \
96  google::RawLogStub__(0, __VA_ARGS__); \
97  exit(1); \
98  } while (0)
99 #endif // STRIP_LOG <= 3
100 
101 // Similar to CHECK(condition) << message,
102 // but for low-level modules: we use only RAW_LOG that does not allocate memory.
103 // We do not want to provide args list here to encourage this usage:
104 // if (!cond) RAW_LOG(FATAL, "foo ...", hard_to_compute_args);
105 // so that the args are not computed when not needed.
106 #define RAW_CHECK(condition, message) \
107  do { \
108  if (!(condition)) { \
109  RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \
110  } \
111  } while (0)
112 
113 // Debug versions of RAW_LOG and RAW_CHECK
114 #ifndef NDEBUG
115 
116 #define RAW_DLOG(severity, ...) RAW_LOG(severity, __VA_ARGS__)
117 #define RAW_DCHECK(condition, message) RAW_CHECK(condition, message)
118 
119 #else // NDEBUG
120 
121 #define RAW_DLOG(severity, ...) \
122  while (false) RAW_LOG(severity, __VA_ARGS__)
123 #define RAW_DCHECK(condition, message) \
124  while (false) RAW_CHECK(condition, message)
125 
126 #endif // NDEBUG
127 
128 // Stub log function used to work around for unused variable warnings when
129 // building with STRIP_LOG > 0.
130 static inline void RawLogStub__(int /* ignored */, ...) {}
131 
132 // Helper function to implement RAW_LOG and RAW_VLOG
133 // Logs format... at "severity" level, reporting it
134 // as called from file:line.
135 // This does not allocate memory or acquire locks.
136 GOOGLE_GLOG_DLL_DECL void RawLog__(LogSeverity severity, const char* file,
137  int line, const char* format, ...);
138 
139 } // namespace google
140 
141 #endif // OR_TOOLS_BASE_RAW_LOGGING_H_
GOOGLE_GLOG_DLL_DECL
#define GOOGLE_GLOG_DLL_DECL
Definition: logging_export.h:27
LogSeverity
int LogSeverity
Definition: log_severity.h:22
google::RawLogStub__
static void RawLogStub__(int,...)
Definition: raw_logging.h:130
log_severity.h
vlog_is_on.h
file
Definition: file.cc:141
google::RawLog__
void RawLog__(LogSeverity severity, const char *file, int line, const char *format,...)
Definition: raw_logging.cc:77
google
Definition: log_severity.h:24