OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_LOGGING_H_ | 5 #ifndef BASE_LOGGING_H_ |
6 #define BASE_LOGGING_H_ | 6 #define BASE_LOGGING_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <cstring> | 10 #include <cstring> |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 // compiles. LOG_IF and development flags also work well together | 67 // compiles. LOG_IF and development flags also work well together |
68 // because the code can be compiled away sometimes. | 68 // because the code can be compiled away sometimes. |
69 // | 69 // |
70 // We also have | 70 // We also have |
71 // | 71 // |
72 // LOG_ASSERT(assertion); | 72 // LOG_ASSERT(assertion); |
73 // DLOG_ASSERT(assertion); | 73 // DLOG_ASSERT(assertion); |
74 // | 74 // |
75 // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; | 75 // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion; |
76 // | 76 // |
| 77 // There are "verbose level" logging macros. They look like |
| 78 // |
| 79 // VLOG(1) << "I'm printed when you run the program with --v=1 or more"; |
| 80 // VLOG(2) << "I'm printed when you run the program with --v=2 or more"; |
| 81 // |
| 82 // These always log at the INFO log level (when they log at all). |
| 83 // The verbose logging can also be turned on module-by-module. For instance, |
| 84 // --vmodule=profile=2,icon_loader=1,browser_*=3 --v=0 |
| 85 // will cause: |
| 86 // a. VLOG(2) and lower messages to be printed from profile.{h,cc} |
| 87 // b. VLOG(1) and lower messages to be printed from icon_loader.{h,cc} |
| 88 // c. VLOG(3) and lower messages to be printed from files prefixed with |
| 89 // "browser" |
| 90 // d. VLOG(0) and lower messages to be printed from elsewhere |
| 91 // |
| 92 // The wildcarding functionality shown by (c) supports both '*' (match |
| 93 // 0 or more characters) and '?' (match any single character) wildcards. |
| 94 // |
| 95 // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as |
| 96 // |
| 97 // if (VLOG_IS_ON(2)) { |
| 98 // // do some logging preparation and logging |
| 99 // // that can't be accomplished with just VLOG(2) << ...; |
| 100 // } |
| 101 // |
| 102 // There is also a VLOG_IF "verbose level" condition macro for sample |
| 103 // cases, when some extra computation and preparation for logs is not |
| 104 // needed. |
| 105 // |
| 106 // VLOG_IF(1, (size > 1024)) |
| 107 // << "I'm printed when size is more than 1024 and when you run the " |
| 108 // "program with --v=1 or more"; |
| 109 // |
77 // We also override the standard 'assert' to use 'DLOG_ASSERT'. | 110 // We also override the standard 'assert' to use 'DLOG_ASSERT'. |
78 // | 111 // |
79 // Lastly, there is: | 112 // Lastly, there is: |
80 // | 113 // |
81 // PLOG(ERROR) << "Couldn't do foo"; | 114 // PLOG(ERROR) << "Couldn't do foo"; |
82 // DPLOG(ERROR) << "Couldn't do foo"; | 115 // DPLOG(ERROR) << "Couldn't do foo"; |
83 // PLOG_IF(ERROR, cond) << "Couldn't do foo"; | 116 // PLOG_IF(ERROR, cond) << "Couldn't do foo"; |
84 // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; | 117 // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; |
85 // PCHECK(condition) << "Couldn't do foo"; | 118 // PCHECK(condition) << "Couldn't do foo"; |
86 // DPCHECK(condition) << "Couldn't do foo"; | 119 // DPCHECK(condition) << "Couldn't do foo"; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 203 |
171 // Sets the log level. Anything at or above this level will be written to the | 204 // Sets the log level. Anything at or above this level will be written to the |
172 // log file/displayed to the user (if applicable). Anything below this level | 205 // log file/displayed to the user (if applicable). Anything below this level |
173 // will be silently ignored. The log level defaults to 0 (everything is logged) | 206 // will be silently ignored. The log level defaults to 0 (everything is logged) |
174 // if this function is not called. | 207 // if this function is not called. |
175 void SetMinLogLevel(int level); | 208 void SetMinLogLevel(int level); |
176 | 209 |
177 // Gets the current log level. | 210 // Gets the current log level. |
178 int GetMinLogLevel(); | 211 int GetMinLogLevel(); |
179 | 212 |
| 213 // Gets the current vlog level for the given file (usually taken from |
| 214 // __FILE__). |
| 215 template <size_t N> |
| 216 int GetVlogLevel(const char (&file)[N]) { |
| 217 return GetVlogLevelHelper(file, N); |
| 218 } |
| 219 // Note that |N| is the size *with* the null terminator. |
| 220 int GetVlogLevelHelper(const char* file_start, size_t N); |
| 221 |
180 // Sets the log filter prefix. Any log message below LOG_ERROR severity that | 222 // Sets the log filter prefix. Any log message below LOG_ERROR severity that |
181 // doesn't start with this prefix with be silently ignored. The filter defaults | 223 // doesn't start with this prefix with be silently ignored. The filter defaults |
182 // to NULL (everything is logged) if this function is not called. Messages | 224 // to NULL (everything is logged) if this function is not called. Messages |
183 // with severity of LOG_ERROR or higher will not be filtered. | 225 // with severity of LOG_ERROR or higher will not be filtered. |
184 void SetLogFilterPrefix(const char* filter); | 226 void SetLogFilterPrefix(const char* filter); |
185 | 227 |
186 // Sets the common items you want to be prepended to each log message. | 228 // Sets the common items you want to be prepended to each log message. |
187 // process and thread IDs default to off, the timestamp defaults to on. | 229 // process and thread IDs default to off, the timestamp defaults to on. |
188 // If this function is not called, logging defaults to writing the timestamp | 230 // If this function is not called, logging defaults to writing the timestamp |
189 // only. | 231 // only. |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR | 314 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR |
273 | 315 |
274 // We use the preprocessor's merging operator, "##", so that, e.g., | 316 // We use the preprocessor's merging operator, "##", so that, e.g., |
275 // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny | 317 // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny |
276 // subtle difference between ostream member streaming functions (e.g., | 318 // subtle difference between ostream member streaming functions (e.g., |
277 // ostream::operator<<(int) and ostream non-member streaming functions | 319 // ostream::operator<<(int) and ostream non-member streaming functions |
278 // (e.g., ::operator<<(ostream&, string&): it turns out that it's | 320 // (e.g., ::operator<<(ostream&, string&): it turns out that it's |
279 // impossible to stream something like a string directly to an unnamed | 321 // impossible to stream something like a string directly to an unnamed |
280 // ostream. We employ a neat hack by calling the stream() member | 322 // ostream. We employ a neat hack by calling the stream() member |
281 // function of LogMessage which seems to avoid the problem. | 323 // function of LogMessage which seems to avoid the problem. |
| 324 // |
| 325 // We can't do any caching tricks with VLOG_IS_ON() like the |
| 326 // google-glog version since it requires GCC extensions. This means |
| 327 // that using the v-logging functions in conjunction with --vmodule |
| 328 // may be slow. |
| 329 #define VLOG_IS_ON(verboselevel) \ |
| 330 (logging::GetVlogLevel(__FILE__) >= (verboselevel)) |
282 | 331 |
283 #define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() | 332 #define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() |
284 #define SYSLOG(severity) LOG(severity) | 333 #define SYSLOG(severity) LOG(severity) |
| 334 #define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel)) |
| 335 |
| 336 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. |
285 | 337 |
286 #define LOG_IF(severity, condition) \ | 338 #define LOG_IF(severity, condition) \ |
287 !(condition) ? (void) 0 : logging::LogMessageVoidify() & LOG(severity) | 339 !(condition) ? (void) 0 : logging::LogMessageVoidify() & LOG(severity) |
288 #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition) | 340 #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition) |
| 341 #define VLOG_IF(verboselevel, condition) \ |
| 342 LOG_IF(INFO, (condition) && VLOG_IS_ON(verboselevel)) |
289 | 343 |
290 #define LOG_ASSERT(condition) \ | 344 #define LOG_ASSERT(condition) \ |
291 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " | 345 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
292 #define SYSLOG_ASSERT(condition) \ | 346 #define SYSLOG_ASSERT(condition) \ |
293 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " | 347 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " |
294 | 348 |
295 #if defined(OS_WIN) | 349 #if defined(OS_WIN) |
296 #define LOG_GETLASTERROR(severity) \ | 350 #define LOG_GETLASTERROR(severity) \ |
297 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ | 351 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ |
298 ::logging::GetLastSystemErrorCode()).stream() | 352 ::logging::GetLastSystemErrorCode()).stream() |
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
889 #elif NOTIMPLEMENTED_POLICY == 4 | 943 #elif NOTIMPLEMENTED_POLICY == 4 |
890 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG | 944 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG |
891 #elif NOTIMPLEMENTED_POLICY == 5 | 945 #elif NOTIMPLEMENTED_POLICY == 5 |
892 #define NOTIMPLEMENTED() do {\ | 946 #define NOTIMPLEMENTED() do {\ |
893 static int count = 0;\ | 947 static int count = 0;\ |
894 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ | 948 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ |
895 } while(0) | 949 } while(0) |
896 #endif | 950 #endif |
897 | 951 |
898 #endif // BASE_LOGGING_H_ | 952 #endif // BASE_LOGGING_H_ |
OLD | NEW |