PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/thirdparty/breakpad/third_party/glog/src/glog/logging.h.in

http://github.com/tomahawk-player/tomahawk
Autoconf | 1506 lines | 1058 code | 214 blank | 234 comment | 132 complexity | 536ad159c6712c6b5336a87db9159ae1 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Copyright (c) 1999, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: Ray Sidney
  31. //
  32. // This file contains #include information about logging-related stuff.
  33. // Pretty much everybody needs to #include this file so that they can
  34. // log various happenings.
  35. //
  36. #ifndef _LOGGING_H_
  37. #define _LOGGING_H_
  38. #include <errno.h>
  39. #include <string.h>
  40. #include <time.h>
  41. #include <string>
  42. #if @ac_cv_have_unistd_h@
  43. # include <unistd.h>
  44. #endif
  45. #ifdef __DEPRECATED
  46. // Make GCC quiet.
  47. # undef __DEPRECATED
  48. # include <strstream>
  49. # define __DEPRECATED
  50. #else
  51. # include <strstream>
  52. #endif
  53. #include <vector>
  54. // Annoying stuff for windows -- makes sure clients can import these functions
  55. #ifndef GOOGLE_GLOG_DLL_DECL
  56. # if defined(_WIN32) && !defined(__CYGWIN__)
  57. # define GOOGLE_GLOG_DLL_DECL __declspec(dllimport)
  58. # else
  59. # define GOOGLE_GLOG_DLL_DECL
  60. # endif
  61. #endif
  62. // We care a lot about number of bits things take up. Unfortunately,
  63. // systems define their bit-specific ints in a lot of different ways.
  64. // We use our own way, and have a typedef to get there.
  65. // Note: these commands below may look like "#if 1" or "#if 0", but
  66. // that's because they were constructed that way at ./configure time.
  67. // Look at logging.h.in to see how they're calculated (based on your config).
  68. #if @ac_cv_have_stdint_h@
  69. #include <stdint.h> // the normal place uint16_t is defined
  70. #endif
  71. #if @ac_cv_have_systypes_h@
  72. #include <sys/types.h> // the normal place u_int16_t is defined
  73. #endif
  74. #if @ac_cv_have_inttypes_h@
  75. #include <inttypes.h> // a third place for uint16_t or u_int16_t
  76. #endif
  77. #if @ac_cv_have_libgflags@
  78. #include <gflags/gflags.h>
  79. #endif
  80. @ac_google_start_namespace@
  81. #if @ac_cv_have_uint16_t@ // the C99 format
  82. typedef int32_t int32;
  83. typedef uint32_t uint32;
  84. typedef int64_t int64;
  85. typedef uint64_t uint64;
  86. #elif @ac_cv_have_u_int16_t@ // the BSD format
  87. typedef int32_t int32;
  88. typedef u_int32_t uint32;
  89. typedef int64_t int64;
  90. typedef u_int64_t uint64;
  91. #elif @ac_cv_have___uint16@ // the windows (vc7) format
  92. typedef __int32 int32;
  93. typedef unsigned __int32 uint32;
  94. typedef __int64 int64;
  95. typedef unsigned __int64 uint64;
  96. #else
  97. #error Do not know how to define a 32-bit integer quantity on your system
  98. #endif
  99. @ac_google_end_namespace@
  100. // The global value of GOOGLE_STRIP_LOG. All the messages logged to
  101. // LOG(XXX) with severity less than GOOGLE_STRIP_LOG will not be displayed.
  102. // If it can be determined at compile time that the message will not be
  103. // printed, the statement will be compiled out.
  104. //
  105. // Example: to strip out all INFO and WARNING messages, use the value
  106. // of 2 below. To make an exception for WARNING messages from a single
  107. // file, add "#define GOOGLE_STRIP_LOG 1" to that file _before_ including
  108. // base/logging.h
  109. #ifndef GOOGLE_STRIP_LOG
  110. #define GOOGLE_STRIP_LOG 0
  111. #endif
  112. // GCC can be told that a certain branch is not likely to be taken (for
  113. // instance, a CHECK failure), and use that information in static analysis.
  114. // Giving it this information can help it optimize for the common case in
  115. // the absence of better information (ie. -fprofile-arcs).
  116. //
  117. #ifndef GOOGLE_PREDICT_BRANCH_NOT_TAKEN
  118. #if @ac_cv_have___builtin_expect@
  119. #define GOOGLE_PREDICT_BRANCH_NOT_TAKEN(x) (__builtin_expect(x, 0))
  120. #else
  121. #define GOOGLE_PREDICT_BRANCH_NOT_TAKEN(x) x
  122. #endif
  123. #endif
  124. // Make a bunch of macros for logging. The way to log things is to stream
  125. // things to LOG(<a particular severity level>). E.g.,
  126. //
  127. // LOG(INFO) << "Found " << num_cookies << " cookies";
  128. //
  129. // You can capture log messages in a string, rather than reporting them
  130. // immediately:
  131. //
  132. // vector<string> errors;
  133. // LOG_STRING(ERROR, &errors) << "Couldn't parse cookie #" << cookie_num;
  134. //
  135. // This pushes back the new error onto 'errors'; if given a NULL pointer,
  136. // it reports the error via LOG(ERROR).
  137. //
  138. // You can also do conditional logging:
  139. //
  140. // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
  141. //
  142. // You can also do occasional logging (log every n'th occurrence of an
  143. // event):
  144. //
  145. // LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
  146. //
  147. // The above will cause log messages to be output on the 1st, 11th, 21st, ...
  148. // times it is executed. Note that the special google::COUNTER value is used
  149. // to identify which repetition is happening.
  150. //
  151. // You can also do occasional conditional logging (log every n'th
  152. // occurrence of an event, when condition is satisfied):
  153. //
  154. // LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER
  155. // << "th big cookie";
  156. //
  157. // You can log messages the first N times your code executes a line. E.g.
  158. //
  159. // LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";
  160. //
  161. // Outputs log messages for the first 20 times it is executed.
  162. //
  163. // Analogous SYSLOG, SYSLOG_IF, and SYSLOG_EVERY_N macros are available.
  164. // These log to syslog as well as to the normal logs. If you use these at
  165. // all, you need to be aware that syslog can drastically reduce performance,
  166. // especially if it is configured for remote logging! Don't use these
  167. // unless you fully understand this and have a concrete need to use them.
  168. // Even then, try to minimize your use of them.
  169. //
  170. // There are also "debug mode" logging macros like the ones above:
  171. //
  172. // DLOG(INFO) << "Found cookies";
  173. //
  174. // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
  175. //
  176. // DLOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
  177. //
  178. // All "debug mode" logging is compiled away to nothing for non-debug mode
  179. // compiles.
  180. //
  181. // We also have
  182. //
  183. // LOG_ASSERT(assertion);
  184. // DLOG_ASSERT(assertion);
  185. //
  186. // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
  187. //
  188. // There are "verbose level" logging macros. They look like
  189. //
  190. // VLOG(1) << "I'm printed when you run the program with --v=1 or more";
  191. // VLOG(2) << "I'm printed when you run the program with --v=2 or more";
  192. //
  193. // These always log at the INFO log level (when they log at all).
  194. // The verbose logging can also be turned on module-by-module. For instance,
  195. // --vmodule=mapreduce=2,file=1,gfs*=3 --v=0
  196. // will cause:
  197. // a. VLOG(2) and lower messages to be printed from mapreduce.{h,cc}
  198. // b. VLOG(1) and lower messages to be printed from file.{h,cc}
  199. // c. VLOG(3) and lower messages to be printed from files prefixed with "gfs"
  200. // d. VLOG(0) and lower messages to be printed from elsewhere
  201. //
  202. // The wildcarding functionality shown by (c) supports both '*' (match
  203. // 0 or more characters) and '?' (match any single character) wildcards.
  204. //
  205. // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
  206. //
  207. // if (VLOG_IS_ON(2)) {
  208. // // do some logging preparation and logging
  209. // // that can't be accomplished with just VLOG(2) << ...;
  210. // }
  211. //
  212. // There are also VLOG_IF, VLOG_EVERY_N and VLOG_IF_EVERY_N "verbose level"
  213. // condition macros for sample cases, when some extra computation and
  214. // preparation for logs is not needed.
  215. // VLOG_IF(1, (size > 1024))
  216. // << "I'm printed when size is more than 1024 and when you run the "
  217. // "program with --v=1 or more";
  218. // VLOG_EVERY_N(1, 10)
  219. // << "I'm printed every 10th occurrence, and when you run the program "
  220. // "with --v=1 or more. Present occurence is " << google::COUNTER;
  221. // VLOG_IF_EVERY_N(1, (size > 1024), 10)
  222. // << "I'm printed on every 10th occurence of case when size is more "
  223. // " than 1024, when you run the program with --v=1 or more. ";
  224. // "Present occurence is " << google::COUNTER;
  225. //
  226. // The supported severity levels for macros that allow you to specify one
  227. // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
  228. // Note that messages of a given severity are logged not only in the
  229. // logfile for that severity, but also in all logfiles of lower severity.
  230. // E.g., a message of severity FATAL will be logged to the logfiles of
  231. // severity FATAL, ERROR, WARNING, and INFO.
  232. //
  233. // There is also the special severity of DFATAL, which logs FATAL in
  234. // debug mode, ERROR in normal mode.
  235. //
  236. // Very important: logging a message at the FATAL severity level causes
  237. // the program to terminate (after the message is logged).
  238. //
  239. // Unless otherwise specified, logs will be written to the filename
  240. // "<program name>.<hostname>.<user name>.log.<severity level>.", followed
  241. // by the date, time, and pid (you can't prevent the date, time, and pid
  242. // from being in the filename).
  243. //
  244. // The logging code takes two flags:
  245. // --v=# set the verbose level
  246. // --logtostderr log all the messages to stderr instead of to logfiles
  247. // LOG LINE PREFIX FORMAT
  248. //
  249. // Log lines have this form:
  250. //
  251. // Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
  252. //
  253. // where the fields are defined as follows:
  254. //
  255. // L A single character, representing the log level
  256. // (eg 'I' for INFO)
  257. // mm The month (zero padded; ie May is '05')
  258. // dd The day (zero padded)
  259. // hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds
  260. // threadid The space-padded thread ID as returned by GetTID()
  261. // (this matches the PID on Linux)
  262. // file The file name
  263. // line The line number
  264. // msg The user-supplied message
  265. //
  266. // Example:
  267. //
  268. // I1103 11:57:31.739339 24395 google.cc:2341] Command line: ./some_prog
  269. // I1103 11:57:31.739403 24395 google.cc:2342] Process id 24395
  270. //
  271. // NOTE: although the microseconds are useful for comparing events on
  272. // a single machine, clocks on different machines may not be well
  273. // synchronized. Hence, use caution when comparing the low bits of
  274. // timestamps from different machines.
  275. #ifndef DECLARE_VARIABLE
  276. #define MUST_UNDEF_GFLAGS_DECLARE_MACROS
  277. #define DECLARE_VARIABLE(type, name, tn) \
  278. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##tn##_instead { \
  279. extern GOOGLE_GLOG_DLL_DECL type FLAGS_##name; \
  280. } \
  281. using FLAG__namespace_do_not_use_directly_use_DECLARE_##tn##_instead::FLAGS_##name
  282. // bool specialization
  283. #define DECLARE_bool(name) \
  284. DECLARE_VARIABLE(bool, name, bool)
  285. // int32 specialization
  286. #define DECLARE_int32(name) \
  287. DECLARE_VARIABLE(@ac_google_namespace@::int32, name, int32)
  288. // Special case for string, because we have to specify the namespace
  289. // std::string, which doesn't play nicely with our FLAG__namespace hackery.
  290. #define DECLARE_string(name) \
  291. namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \
  292. extern GOOGLE_GLOG_DLL_DECL std::string FLAGS_##name; \
  293. } \
  294. using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
  295. #endif
  296. // Set whether log messages go to stderr instead of logfiles
  297. DECLARE_bool(logtostderr);
  298. // Set whether log messages go to stderr in addition to logfiles.
  299. DECLARE_bool(alsologtostderr);
  300. // Log messages at a level >= this flag are automatically sent to
  301. // stderr in addition to log files.
  302. DECLARE_int32(stderrthreshold);
  303. // Set whether the log prefix should be prepended to each line of output.
  304. DECLARE_bool(log_prefix);
  305. // Log messages at a level <= this flag are buffered.
  306. // Log messages at a higher level are flushed immediately.
  307. DECLARE_int32(logbuflevel);
  308. // Sets the maximum number of seconds which logs may be buffered for.
  309. DECLARE_int32(logbufsecs);
  310. // Log suppression level: messages logged at a lower level than this
  311. // are suppressed.
  312. DECLARE_int32(minloglevel);
  313. // If specified, logfiles are written into this directory instead of the
  314. // default logging directory.
  315. DECLARE_string(log_dir);
  316. // Sets the path of the directory into which to put additional links
  317. // to the log files.
  318. DECLARE_string(log_link);
  319. DECLARE_int32(v); // in vlog_is_on.cc
  320. // Sets the maximum log file size (in MB).
  321. DECLARE_int32(max_log_size);
  322. // Sets whether to avoid logging to the disk if the disk is full.
  323. DECLARE_bool(stop_logging_if_full_disk);
  324. #ifdef MUST_UNDEF_GFLAGS_DECLARE_MACROS
  325. #undef MUST_UNDEF_GFLAGS_DECLARE_MACROS
  326. #undef DECLARE_VARIABLE
  327. #undef DECLARE_bool
  328. #undef DECLARE_int32
  329. #undef DECLARE_string
  330. #endif
  331. // Log messages below the GOOGLE_STRIP_LOG level will be compiled away for
  332. // security reasons. See LOG(severtiy) below.
  333. // A few definitions of macros that don't generate much code. Since
  334. // LOG(INFO) and its ilk are used all over our code, it's
  335. // better to have compact code for these operations.
  336. #if GOOGLE_STRIP_LOG == 0
  337. #define COMPACT_GOOGLE_LOG_INFO @ac_google_namespace@::LogMessage( \
  338. __FILE__, __LINE__)
  339. #define LOG_TO_STRING_INFO(message) @ac_google_namespace@::LogMessage( \
  340. __FILE__, __LINE__, @ac_google_namespace@::INFO, message)
  341. #else
  342. #define COMPACT_GOOGLE_LOG_INFO @ac_google_namespace@::NullStream()
  343. #define LOG_TO_STRING_INFO(message) @ac_google_namespace@::NullStream()
  344. #endif
  345. #if GOOGLE_STRIP_LOG <= 1
  346. #define COMPACT_GOOGLE_LOG_WARNING @ac_google_namespace@::LogMessage( \
  347. __FILE__, __LINE__, @ac_google_namespace@::WARNING)
  348. #define LOG_TO_STRING_WARNING(message) @ac_google_namespace@::LogMessage( \
  349. __FILE__, __LINE__, @ac_google_namespace@::WARNING, message)
  350. #else
  351. #define COMPACT_GOOGLE_LOG_WARNING @ac_google_namespace@::NullStream()
  352. #define LOG_TO_STRING_WARNING(message) @ac_google_namespace@::NullStream()
  353. #endif
  354. #if GOOGLE_STRIP_LOG <= 2
  355. #define COMPACT_GOOGLE_LOG_ERROR @ac_google_namespace@::LogMessage( \
  356. __FILE__, __LINE__, @ac_google_namespace@::ERROR)
  357. #define LOG_TO_STRING_ERROR(message) @ac_google_namespace@::LogMessage( \
  358. __FILE__, __LINE__, @ac_google_namespace@::ERROR, message)
  359. #else
  360. #define COMPACT_GOOGLE_LOG_ERROR @ac_google_namespace@::NullStream()
  361. #define LOG_TO_STRING_ERROR(message) @ac_google_namespace@::NullStream()
  362. #endif
  363. #if GOOGLE_STRIP_LOG <= 3
  364. #define COMPACT_GOOGLE_LOG_FATAL @ac_google_namespace@::LogMessageFatal( \
  365. __FILE__, __LINE__)
  366. #define LOG_TO_STRING_FATAL(message) @ac_google_namespace@::LogMessage( \
  367. __FILE__, __LINE__, @ac_google_namespace@::FATAL, message)
  368. #else
  369. #define COMPACT_GOOGLE_LOG_FATAL @ac_google_namespace@::NullStreamFatal()
  370. #define LOG_TO_STRING_FATAL(message) @ac_google_namespace@::NullStreamFatal()
  371. #endif
  372. // For DFATAL, we want to use LogMessage (as opposed to
  373. // LogMessageFatal), to be consistent with the original behavior.
  374. #ifdef NDEBUG
  375. #define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_ERROR
  376. #elif GOOGLE_STRIP_LOG <= 3
  377. #define COMPACT_GOOGLE_LOG_DFATAL @ac_google_namespace@::LogMessage( \
  378. __FILE__, __LINE__, @ac_google_namespace@::FATAL)
  379. #else
  380. #define COMPACT_GOOGLE_LOG_DFATAL @ac_google_namespace@::NullStreamFatal()
  381. #endif
  382. #define GOOGLE_LOG_INFO(counter) @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::INFO, counter, &@ac_google_namespace@::LogMessage::SendToLog)
  383. #define SYSLOG_INFO(counter) \
  384. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::INFO, counter, \
  385. &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
  386. #define GOOGLE_LOG_WARNING(counter) \
  387. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::WARNING, counter, \
  388. &@ac_google_namespace@::LogMessage::SendToLog)
  389. #define SYSLOG_WARNING(counter) \
  390. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::WARNING, counter, \
  391. &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
  392. #define GOOGLE_LOG_ERROR(counter) \
  393. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::ERROR, counter, \
  394. &@ac_google_namespace@::LogMessage::SendToLog)
  395. #define SYSLOG_ERROR(counter) \
  396. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::ERROR, counter, \
  397. &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
  398. #define GOOGLE_LOG_FATAL(counter) \
  399. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::FATAL, counter, \
  400. &@ac_google_namespace@::LogMessage::SendToLog)
  401. #define SYSLOG_FATAL(counter) \
  402. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::FATAL, counter, \
  403. &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
  404. #define GOOGLE_LOG_DFATAL(counter) \
  405. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::DFATAL_LEVEL, counter, \
  406. &@ac_google_namespace@::LogMessage::SendToLog)
  407. #define SYSLOG_DFATAL(counter) \
  408. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::DFATAL_LEVEL, counter, \
  409. &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
  410. #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
  411. // A very useful logging macro to log windows errors:
  412. #define LOG_SYSRESULT(result) \
  413. if (FAILED(result)) { \
  414. LPTSTR message = NULL; \
  415. LPTSTR msg = reinterpret_cast<LPTSTR>(&message); \
  416. DWORD message_length = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | \
  417. FORMAT_MESSAGE_FROM_SYSTEM, \
  418. 0, result, 0, msg, 100, NULL); \
  419. if (message_length > 0) { \
  420. @ac_google_namespace@::LogMessage(__FILE__, __LINE__, ERROR, 0, \
  421. &@ac_google_namespace@::LogMessage::SendToLog).stream() << message; \
  422. LocalFree(message); \
  423. } \
  424. }
  425. #endif
  426. // We use the preprocessor's merging operator, "##", so that, e.g.,
  427. // LOG(INFO) becomes the token GOOGLE_LOG_INFO. There's some funny
  428. // subtle difference between ostream member streaming functions (e.g.,
  429. // ostream::operator<<(int) and ostream non-member streaming functions
  430. // (e.g., ::operator<<(ostream&, string&): it turns out that it's
  431. // impossible to stream something like a string directly to an unnamed
  432. // ostream. We employ a neat hack by calling the stream() member
  433. // function of LogMessage which seems to avoid the problem.
  434. #define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
  435. #define SYSLOG(severity) SYSLOG_ ## severity(0).stream()
  436. @ac_google_start_namespace@
  437. // They need the definitions of integer types.
  438. #include "glog/log_severity.h"
  439. #include "glog/vlog_is_on.h"
  440. // Initialize google's logging library. You will see the program name
  441. // specified by argv0 in log outputs.
  442. GOOGLE_GLOG_DLL_DECL void InitGoogleLogging(const char* argv0);
  443. // Shutdown google's logging library.
  444. GOOGLE_GLOG_DLL_DECL void ShutdownGoogleLogging();
  445. // Install a function which will be called after LOG(FATAL).
  446. GOOGLE_GLOG_DLL_DECL void InstallFailureFunction(void (*fail_func)());
  447. class LogSink; // defined below
  448. // If a non-NULL sink pointer is given, we push this message to that sink.
  449. // For LOG_TO_SINK we then do normal LOG(severity) logging as well.
  450. // This is useful for capturing messages and passing/storing them
  451. // somewhere more specific than the global log of the process.
  452. // Argument types:
  453. // LogSink* sink;
  454. // LogSeverity severity;
  455. // The cast is to disambiguate NULL arguments.
  456. #define LOG_TO_SINK(sink, severity) \
  457. @ac_google_namespace@::LogMessage( \
  458. __FILE__, __LINE__, \
  459. @ac_google_namespace@::severity, \
  460. static_cast<@ac_google_namespace@::LogSink*>(sink), true).stream()
  461. #define LOG_TO_SINK_BUT_NOT_TO_LOGFILE(sink, severity) \
  462. @ac_google_namespace@::LogMessage( \
  463. __FILE__, __LINE__, \
  464. @ac_google_namespace@::severity, \
  465. static_cast<@ac_google_namespace@::LogSink*>(sink), false).stream()
  466. // If a non-NULL string pointer is given, we write this message to that string.
  467. // We then do normal LOG(severity) logging as well.
  468. // This is useful for capturing messages and storing them somewhere more
  469. // specific than the global log of the process.
  470. // Argument types:
  471. // string* message;
  472. // LogSeverity severity;
  473. // The cast is to disambiguate NULL arguments.
  474. // NOTE: LOG(severity) expands to LogMessage().stream() for the specified
  475. // severity.
  476. #define LOG_TO_STRING(severity, message) \
  477. LOG_TO_STRING_##severity(static_cast<string*>(message)).stream()
  478. // If a non-NULL pointer is given, we push the message onto the end
  479. // of a vector of strings; otherwise, we report it with LOG(severity).
  480. // This is handy for capturing messages and perhaps passing them back
  481. // to the caller, rather than reporting them immediately.
  482. // Argument types:
  483. // LogSeverity severity;
  484. // vector<string> *outvec;
  485. // The cast is to disambiguate NULL arguments.
  486. #define LOG_STRING(severity, outvec) \
  487. LOG_TO_STRING_##severity(static_cast<vector<string>*>(outvec)).stream()
  488. #define LOG_IF(severity, condition) \
  489. !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
  490. #define SYSLOG_IF(severity, condition) \
  491. !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & SYSLOG(severity)
  492. #define LOG_ASSERT(condition) \
  493. LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
  494. #define SYSLOG_ASSERT(condition) \
  495. SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
  496. // CHECK dies with a fatal error if condition is not true. It is *not*
  497. // controlled by NDEBUG, so the check will be executed regardless of
  498. // compilation mode. Therefore, it is safe to do things like:
  499. // CHECK(fp->Write(x) == 4)
  500. #define CHECK(condition) \
  501. LOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN(!(condition))) \
  502. << "Check failed: " #condition " "
  503. // A container for a string pointer which can be evaluated to a bool -
  504. // true iff the pointer is NULL.
  505. struct CheckOpString {
  506. CheckOpString(std::string* str) : str_(str) { }
  507. // No destructor: if str_ is non-NULL, we're about to LOG(FATAL),
  508. // so there's no point in cleaning up str_.
  509. operator bool() const {
  510. return GOOGLE_PREDICT_BRANCH_NOT_TAKEN(str_ != NULL);
  511. }
  512. std::string* str_;
  513. };
  514. // Function is overloaded for integral types to allow static const
  515. // integrals declared in classes and not defined to be used as arguments to
  516. // CHECK* macros. It's not encouraged though.
  517. template <class T>
  518. inline const T& GetReferenceableValue(const T& t) { return t; }
  519. inline char GetReferenceableValue(char t) { return t; }
  520. inline unsigned char GetReferenceableValue(unsigned char t) { return t; }
  521. inline signed char GetReferenceableValue(signed char t) { return t; }
  522. inline short GetReferenceableValue(short t) { return t; }
  523. inline unsigned short GetReferenceableValue(unsigned short t) { return t; }
  524. inline int GetReferenceableValue(int t) { return t; }
  525. inline unsigned int GetReferenceableValue(unsigned int t) { return t; }
  526. inline long GetReferenceableValue(long t) { return t; }
  527. inline unsigned long GetReferenceableValue(unsigned long t) { return t; }
  528. inline long long GetReferenceableValue(long long t) { return t; }
  529. inline unsigned long long GetReferenceableValue(unsigned long long t) {
  530. return t;
  531. }
  532. // This is a dummy class to define the following operator.
  533. struct DummyClassToDefineOperator {};
  534. @ac_google_end_namespace@
  535. // Define global operator<< to declare using ::operator<<.
  536. // This declaration will allow use to use CHECK macros for user
  537. // defined classes which have operator<< (e.g., stl_logging.h).
  538. inline std::ostream& operator<<(
  539. std::ostream& out, const google::DummyClassToDefineOperator&) {
  540. return out;
  541. }
  542. @ac_google_start_namespace@
  543. // Build the error message string.
  544. template<class t1, class t2>
  545. std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
  546. // It means that we cannot use stl_logging if compiler doesn't
  547. // support using expression for operator.
  548. // TODO(hamaji): Figure out a way to fix.
  549. #if @ac_cv_cxx_using_operator@
  550. using ::operator<<;
  551. #endif
  552. std::strstream ss;
  553. ss << names << " (" << v1 << " vs. " << v2 << ")";
  554. return new std::string(ss.str(), ss.pcount());
  555. }
  556. // Helper functions for CHECK_OP macro.
  557. // The (int, int) specialization works around the issue that the compiler
  558. // will not instantiate the template version of the function on values of
  559. // unnamed enum type - see comment below.
  560. #define DEFINE_CHECK_OP_IMPL(name, op) \
  561. template <class t1, class t2> \
  562. inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
  563. const char* names) { \
  564. if (v1 op v2) return NULL; \
  565. else return MakeCheckOpString(v1, v2, names); \
  566. } \
  567. inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
  568. return Check##name##Impl<int, int>(v1, v2, names); \
  569. }
  570. // Use _EQ, _NE, _LE, etc. in case the file including base/logging.h
  571. // provides its own #defines for the simpler names EQ, NE, LE, etc.
  572. // This happens if, for example, those are used as token names in a
  573. // yacc grammar.
  574. DEFINE_CHECK_OP_IMPL(_EQ, ==)
  575. DEFINE_CHECK_OP_IMPL(_NE, !=)
  576. DEFINE_CHECK_OP_IMPL(_LE, <=)
  577. DEFINE_CHECK_OP_IMPL(_LT, < )
  578. DEFINE_CHECK_OP_IMPL(_GE, >=)
  579. DEFINE_CHECK_OP_IMPL(_GT, > )
  580. #undef DEFINE_CHECK_OP_IMPL
  581. // Helper macro for binary operators.
  582. // Don't use this macro directly in your code, use CHECK_EQ et al below.
  583. #if defined(STATIC_ANALYSIS)
  584. // Only for static analysis tool to know that it is equivalent to assert
  585. #define CHECK_OP_LOG(name, op, val1, val2, log) CHECK((val1) op (val2))
  586. #elif !defined(NDEBUG)
  587. // In debug mode, avoid constructing CheckOpStrings if possible,
  588. // to reduce the overhead of CHECK statments by 2x.
  589. // Real DCHECK-heavy tests have seen 1.5x speedups.
  590. // The meaning of "string" might be different between now and
  591. // when this macro gets invoked (e.g., if someone is experimenting
  592. // with other string implementations that get defined after this
  593. // file is included). Save the current meaning now and use it
  594. // in the macro.
  595. typedef std::string _Check_string;
  596. #define CHECK_OP_LOG(name, op, val1, val2, log) \
  597. while (@ac_google_namespace@::_Check_string* _result = \
  598. @ac_google_namespace@::Check##name##Impl( \
  599. @ac_google_namespace@::GetReferenceableValue(val1), \
  600. @ac_google_namespace@::GetReferenceableValue(val2), \
  601. #val1 " " #op " " #val2)) \
  602. log(__FILE__, __LINE__, \
  603. @ac_google_namespace@::CheckOpString(_result)).stream()
  604. #else
  605. // In optimized mode, use CheckOpString to hint to compiler that
  606. // the while condition is unlikely.
  607. #define CHECK_OP_LOG(name, op, val1, val2, log) \
  608. while (@ac_google_namespace@::CheckOpString _result = \
  609. @ac_google_namespace@::Check##name##Impl( \
  610. @ac_google_namespace@::GetReferenceableValue(val1), \
  611. @ac_google_namespace@::GetReferenceableValue(val2), \
  612. #val1 " " #op " " #val2)) \
  613. log(__FILE__, __LINE__, _result).stream()
  614. #endif // STATIC_ANALYSIS, !NDEBUG
  615. #if GOOGLE_STRIP_LOG <= 3
  616. #define CHECK_OP(name, op, val1, val2) \
  617. CHECK_OP_LOG(name, op, val1, val2, @ac_google_namespace@::LogMessageFatal)
  618. #else
  619. #define CHECK_OP(name, op, val1, val2) \
  620. CHECK_OP_LOG(name, op, val1, val2, @ac_google_namespace@::NullStreamFatal)
  621. #endif // STRIP_LOG <= 3
  622. // Equality/Inequality checks - compare two values, and log a FATAL message
  623. // including the two values when the result is not as expected. The values
  624. // must have operator<<(ostream, ...) defined.
  625. //
  626. // You may append to the error message like so:
  627. // CHECK_NE(1, 2) << ": The world must be ending!";
  628. //
  629. // We are very careful to ensure that each argument is evaluated exactly
  630. // once, and that anything which is legal to pass as a function argument is
  631. // legal here. In particular, the arguments may be temporary expressions
  632. // which will end up being destroyed at the end of the apparent statement,
  633. // for example:
  634. // CHECK_EQ(string("abc")[1], 'b');
  635. //
  636. // WARNING: These don't compile correctly if one of the arguments is a pointer
  637. // and the other is NULL. To work around this, simply static_cast NULL to the
  638. // type of the desired pointer.
  639. #define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2)
  640. #define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2)
  641. #define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2)
  642. #define CHECK_LT(val1, val2) CHECK_OP(_LT, < , val1, val2)
  643. #define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2)
  644. #define CHECK_GT(val1, val2) CHECK_OP(_GT, > , val1, val2)
  645. // Check that the input is non NULL. This very useful in constructor
  646. // initializer lists.
  647. #define CHECK_NOTNULL(val) \
  648. @ac_google_namespace@::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
  649. // Helper functions for string comparisons.
  650. // To avoid bloat, the definitions are in logging.cc.
  651. #define DECLARE_CHECK_STROP_IMPL(func, expected) \
  652. GOOGLE_GLOG_DLL_DECL std::string* Check##func##expected##Impl( \
  653. const char* s1, const char* s2, const char* names);
  654. DECLARE_CHECK_STROP_IMPL(strcmp, true)
  655. DECLARE_CHECK_STROP_IMPL(strcmp, false)
  656. DECLARE_CHECK_STROP_IMPL(strcasecmp, true)
  657. DECLARE_CHECK_STROP_IMPL(strcasecmp, false)
  658. #undef DECLARE_CHECK_STROP_IMPL
  659. // Helper macro for string comparisons.
  660. // Don't use this macro directly in your code, use CHECK_STREQ et al below.
  661. #define CHECK_STROP(func, op, expected, s1, s2) \
  662. while (@ac_google_namespace@::CheckOpString _result = \
  663. @ac_google_namespace@::Check##func##expected##Impl((s1), (s2), \
  664. #s1 " " #op " " #s2)) \
  665. LOG(FATAL) << *_result.str_
  666. // String (char*) equality/inequality checks.
  667. // CASE versions are case-insensitive.
  668. //
  669. // Note that "s1" and "s2" may be temporary strings which are destroyed
  670. // by the compiler at the end of the current "full expression"
  671. // (e.g. CHECK_STREQ(Foo().c_str(), Bar().c_str())).
  672. #define CHECK_STREQ(s1, s2) CHECK_STROP(strcmp, ==, true, s1, s2)
  673. #define CHECK_STRNE(s1, s2) CHECK_STROP(strcmp, !=, false, s1, s2)
  674. #define CHECK_STRCASEEQ(s1, s2) CHECK_STROP(strcasecmp, ==, true, s1, s2)
  675. #define CHECK_STRCASENE(s1, s2) CHECK_STROP(strcasecmp, !=, false, s1, s2)
  676. #define CHECK_INDEX(I,A) CHECK(I < (sizeof(A)/sizeof(A[0])))
  677. #define CHECK_BOUND(B,A) CHECK(B <= (sizeof(A)/sizeof(A[0])))
  678. #define CHECK_DOUBLE_EQ(val1, val2) \
  679. do { \
  680. CHECK_LE((val1), (val2)+0.000000000000001L); \
  681. CHECK_GE((val1), (val2)-0.000000000000001L); \
  682. } while (0)
  683. #define CHECK_NEAR(val1, val2, margin) \
  684. do { \
  685. CHECK_LE((val1), (val2)+(margin)); \
  686. CHECK_GE((val1), (val2)-(margin)); \
  687. } while (0)
  688. // perror()..googly style!
  689. //
  690. // PLOG() and PLOG_IF() and PCHECK() behave exactly like their LOG* and
  691. // CHECK equivalents with the addition that they postpend a description
  692. // of the current state of errno to their output lines.
  693. #define PLOG(severity) GOOGLE_PLOG(severity, 0).stream()
  694. #define GOOGLE_PLOG(severity, counter) \
  695. @ac_google_namespace@::ErrnoLogMessage( \
  696. __FILE__, __LINE__, @ac_google_namespace@::severity, counter, \
  697. &@ac_google_namespace@::LogMessage::SendToLog)
  698. #define PLOG_IF(severity, condition) \
  699. !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & PLOG(severity)
  700. // A CHECK() macro that postpends errno if the condition is false. E.g.
  701. //
  702. // if (poll(fds, nfds, timeout) == -1) { PCHECK(errno == EINTR); ... }
  703. #define PCHECK(condition) \
  704. PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN(!(condition))) \
  705. << "Check failed: " #condition " "
  706. // A CHECK() macro that lets you assert the success of a function that
  707. // returns -1 and sets errno in case of an error. E.g.
  708. //
  709. // CHECK_ERR(mkdir(path, 0700));
  710. //
  711. // or
  712. //
  713. // int fd = open(filename, flags); CHECK_ERR(fd) << ": open " << filename;
  714. #define CHECK_ERR(invocation) \
  715. PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
  716. << #invocation
  717. // Use macro expansion to create, for each use of LOG_EVERY_N(), static
  718. // variables with the __LINE__ expansion as part of the variable name.
  719. #define LOG_EVERY_N_VARNAME(base, line) LOG_EVERY_N_VARNAME_CONCAT(base, line)
  720. #define LOG_EVERY_N_VARNAME_CONCAT(base, line) base ## line
  721. #define LOG_OCCURRENCES LOG_EVERY_N_VARNAME(occurrences_, __LINE__)
  722. #define LOG_OCCURRENCES_MOD_N LOG_EVERY_N_VARNAME(occurrences_mod_n_, __LINE__)
  723. #define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
  724. static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
  725. ++LOG_OCCURRENCES; \
  726. if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
  727. if (LOG_OCCURRENCES_MOD_N == 1) \
  728. @ac_google_namespace@::LogMessage( \
  729. __FILE__, __LINE__, @ac_google_namespace@::severity, LOG_OCCURRENCES, \
  730. &what_to_do).stream()
  731. #define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
  732. static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
  733. ++LOG_OCCURRENCES; \
  734. if (condition && \
  735. ((LOG_OCCURRENCES_MOD_N=(LOG_OCCURRENCES_MOD_N + 1) % n) == (1 % n))) \
  736. @ac_google_namespace@::LogMessage( \
  737. __FILE__, __LINE__, @ac_google_namespace@::severity, LOG_OCCURRENCES, \
  738. &what_to_do).stream()
  739. #define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
  740. static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
  741. ++LOG_OCCURRENCES; \
  742. if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
  743. if (LOG_OCCURRENCES_MOD_N == 1) \
  744. @ac_google_namespace@::ErrnoLogMessage( \
  745. __FILE__, __LINE__, @ac_google_namespace@::severity, LOG_OCCURRENCES, \
  746. &what_to_do).stream()
  747. #define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
  748. static int LOG_OCCURRENCES = 0; \
  749. if (LOG_OCCURRENCES <= n) \
  750. ++LOG_OCCURRENCES; \
  751. if (LOG_OCCURRENCES <= n) \
  752. @ac_google_namespace@::LogMessage( \
  753. __FILE__, __LINE__, @ac_google_namespace@::severity, LOG_OCCURRENCES, \
  754. &what_to_do).stream()
  755. namespace glog_internal_namespace_ {
  756. template <bool>
  757. struct CompileAssert {
  758. };
  759. struct CrashReason;
  760. } // namespace glog_internal_namespace_
  761. #define GOOGLE_GLOG_COMPILE_ASSERT(expr, msg) \
  762. typedef @ac_google_namespace@::glog_internal_namespace_::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
  763. #define LOG_EVERY_N(severity, n) \
  764. GOOGLE_GLOG_COMPILE_ASSERT(@ac_google_namespace@::severity < \
  765. @ac_google_namespace@::NUM_SEVERITIES, \
  766. INVALID_REQUESTED_LOG_SEVERITY); \
  767. SOME_KIND_OF_LOG_EVERY_N(severity, (n), @ac_google_namespace@::LogMessage::SendToLog)
  768. #define SYSLOG_EVERY_N(severity, n) \
  769. SOME_KIND_OF_LOG_EVERY_N(severity, (n), @ac_google_namespace@::LogMessage::SendToSyslogAndLog)
  770. #define PLOG_EVERY_N(severity, n) \
  771. SOME_KIND_OF_PLOG_EVERY_N(severity, (n), @ac_google_namespace@::LogMessage::SendToLog)
  772. #define LOG_FIRST_N(severity, n) \
  773. SOME_KIND_OF_LOG_FIRST_N(severity, (n), @ac_google_namespace@::LogMessage::SendToLog)
  774. #define LOG_IF_EVERY_N(severity, condition, n) \
  775. SOME_KIND_OF_LOG_IF_EVERY_N(severity, (condition), (n), @ac_google_namespace@::LogMessage::SendToLog)
  776. // We want the special COUNTER value available for LOG_EVERY_X()'ed messages
  777. enum PRIVATE_Counter {COUNTER};
  778. // Plus some debug-logging macros that get compiled to nothing for production
  779. #ifndef NDEBUG
  780. #define DLOG(severity) LOG(severity)
  781. #define DVLOG(verboselevel) VLOG(verboselevel)
  782. #define DLOG_IF(severity, condition) LOG_IF(severity, condition)
  783. #define DLOG_EVERY_N(severity, n) LOG_EVERY_N(severity, n)
  784. #define DLOG_IF_EVERY_N(severity, condition, n) \
  785. LOG_IF_EVERY_N(severity, condition, n)
  786. #define DLOG_ASSERT(condition) LOG_ASSERT(condition)
  787. // debug-only checking. not executed in NDEBUG mode.
  788. #define DCHECK(condition) CHECK(condition)
  789. #define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
  790. #define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
  791. #define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
  792. #define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
  793. #define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
  794. #define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
  795. #define DCHECK_NOTNULL(val) CHECK_NOTNULL(val)
  796. #define DCHECK_STREQ(str1, str2) CHECK_STREQ(str1, str2)
  797. #define DCHECK_STRCASEEQ(str1, str2) CHECK_STRCASEEQ(str1, str2)
  798. #define DCHECK_STRNE(str1, str2) CHECK_STRNE(str1, str2)
  799. #define DCHECK_STRCASENE(str1, str2) CHECK_STRCASENE(str1, str2)
  800. #else // NDEBUG
  801. #define DLOG(severity) \
  802. true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
  803. #define DVLOG(verboselevel) \
  804. (true || !VLOG_IS_ON(verboselevel)) ?\
  805. (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(INFO)
  806. #define DLOG_IF(severity, condition) \
  807. (true || !(condition)) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
  808. #define DLOG_EVERY_N(severity, n) \
  809. true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
  810. #define DLOG_IF_EVERY_N(severity, condition, n) \
  811. (true || !(condition))? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
  812. #define DLOG_ASSERT(condition) \
  813. true ? (void) 0 : LOG_ASSERT(condition)
  814. #define DCHECK(condition) \
  815. while (false) \
  816. CHECK(condition)
  817. #define DCHECK_EQ(val1, val2) \
  818. while (false) \
  819. CHECK_EQ(val1, val2)
  820. #define DCHECK_NE(val1, val2) \
  821. while (false) \
  822. CHECK_NE(val1, val2)
  823. #define DCHECK_LE(val1, val2) \
  824. while (false) \
  825. CHECK_LE(val1, val2)
  826. #define DCHECK_LT(val1, val2) \
  827. while (false) \
  828. CHECK_LT(val1, val2)
  829. #define DCHECK_GE(val1, val2) \
  830. while (false) \
  831. CHECK_GE(val1, val2)
  832. #define DCHECK_GT(val1, val2) \
  833. while (false) \
  834. CHECK_GT(val1, val2)
  835. #define DCHECK_NOTNULL(val) (val)
  836. #define DCHECK_STREQ(str1, str2) \
  837. while (false) \
  838. CHECK_STREQ(str1, str2)
  839. #define DCHECK_STRCASEEQ(str1, str2) \
  840. while (false) \
  841. CHECK_STRCASEEQ(str1, str2)
  842. #define DCHECK_STRNE(str1, str2) \
  843. while (false) \
  844. CHECK_STRNE(str1, str2)
  845. #define DCHECK_STRCASENE(str1, str2) \
  846. while (false) \
  847. CHECK_STRCASENE(str1, str2)
  848. #endif // NDEBUG
  849. // Log only in verbose mode.
  850. #define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel))
  851. #define VLOG_IF(verboselevel, condition) \
  852. LOG_IF(INFO, (condition) && VLOG_IS_ON(verboselevel))
  853. #define VLOG_EVERY_N(verboselevel, n) \
  854. LOG_IF_EVERY_N(INFO, VLOG_IS_ON(verboselevel), n)
  855. #define VLOG_IF_EVERY_N(verboselevel, condition, n) \
  856. LOG_IF_EVERY_N(INFO, (condition) && VLOG_IS_ON(verboselevel), n)
  857. //
  858. // This class more or less represents a particular log message. You
  859. // create an instance of LogMessage and then stream stuff to it.
  860. // When you finish streaming to it, ~LogMessage is called and the
  861. // full message gets streamed to the appropriate destination.
  862. //
  863. // You shouldn't actually use LogMessage's constructor to log things,
  864. // though. You should use the LOG() macro (and variants thereof)
  865. // above.
  866. class GOOGLE_GLOG_DLL_DECL LogMessage {
  867. public:
  868. enum {
  869. // Passing kNoLogPrefix for the line number disables the
  870. // log-message prefix. Useful for using the LogMessage
  871. // infrastructure as a printing utility. See also the --log_prefix
  872. // flag for controlling the log-message prefix on an
  873. // application-wide basis.
  874. kNoLogPrefix = -1
  875. };
  876. // LogStream inherit from non-DLL-exported class (std::ostrstream)
  877. // and VC++ produces a warning for this situation.
  878. // However, MSDN says "C4275 can be ignored in Microsoft Visual C++
  879. // 2005 if you are deriving from a type in the Standard C++ Library"
  880. // http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
  881. // Let's just ignore the warning.
  882. #ifdef _MSC_VER
  883. # pragma warning(disable: 4275)
  884. #endif
  885. class GOOGLE_GLOG_DLL_DECL LogStream : public std::ostrstream {
  886. #ifdef _MSC_VER
  887. # pragma warning(default: 4275)
  888. #endif
  889. public:
  890. LogStream(char *buf, int len, int ctr)
  891. : ostrstream(buf, len),
  892. ctr_(ctr) {
  893. self_ = this;
  894. }
  895. int ctr() const { return ctr_; }
  896. void set_ctr(int ctr) { ctr_ = ctr; }
  897. LogStream* self() const { return self_; }
  898. private:
  899. int ctr_; // Counter hack (for the LOG_EVERY_X() macro)
  900. LogStream *self_; // Consistency check hack
  901. };
  902. public:
  903. // icc 8 requires this typedef to avoid an internal compiler error.
  904. typedef void (LogMessage::*SendMethod)();
  905. LogMessage(const char* file, int line, LogSeverity severity, int ctr,
  906. SendMethod send_method);
  907. // Two special constructors that generate reduced amounts of code at
  908. // LOG call sites for common cases.
  909. // Used for LOG(INFO): Implied are:
  910. // severity = INFO, ctr = 0, send_method = &LogMessage::SendToLog.
  911. //
  912. // Using this constructor instead of the more complex constructor above
  913. // saves 19 bytes per call site.
  914. LogMessage(const char* file, int line);
  915. // Used for LOG(severity) where severity != INFO. Implied
  916. // are: ctr = 0, send_method = &LogMessage::SendToLog
  917. //
  918. // Using this constructor instead of the more complex constructor above
  919. // saves 17 bytes per call site.
  920. LogMessage(const char* file, int line, LogSeverity severity);
  921. // Constructor to log this message to a specified sink (if not NULL).
  922. // Implied are: ctr = 0, send_method = &LogMessage::SendToSinkAndLog if
  923. // also_send_to_log is true, send_method = &LogMessage::SendToSink otherwise.
  924. LogMessage(const char* file, int line, LogSeverity severity, LogSink* sink,
  925. bool also_send_to_log);
  926. // Constructor where we also give a vector<string> pointer
  927. // for storing the messages (if the pointer is not NULL).
  928. // Implied are: ctr = 0, send_method = &LogMessage::SaveOrSendToLog.
  929. LogMessage(const char* file, int line, LogSeverity severity,
  930. std::vector<std::string>* outvec);
  931. // Constructor where we also give a string pointer for storing the
  932. // message (if the pointer is not NULL). Implied are: ctr = 0,
  933. // send_method = &LogMessage::WriteToStringAndLog.
  934. LogMessage(const char* file, int line, LogSeverity severity,
  935. std::string* message);
  936. // A special constructor used for check failures
  937. LogMessage(const char* file, int line, const CheckOpString& result);
  938. ~LogMessage();
  939. // Flush a buffered message to the sink set in the constructor. Always
  940. // called by the destructor, it may also be called from elsewhere if
  941. // needed. Only the first call is actioned; any later ones are ignored.
  942. void Flush();
  943. // An arbitrary limit on the length of a single log message. This
  944. // is so that streaming can be done more efficiently.
  945. static const size_t kMaxLogMessageLen;
  946. // Theses should not be called directly outside of logging.*,
  947. // only passed as SendMethod arguments to other LogMessage methods:
  948. void SendToLog(); // Actually dispatch to the logs
  949. void SendToSyslogAndLog(); // Actually dispatch to syslog and the logs
  950. // Call abort() or similar to perform LOG(FATAL) crash.
  951. static void Fail() @ac_cv___attribute___noreturn@;
  952. std::ostream& stream() { return *(data_->stream_); }
  953. int preserved_errno() const { return data_->preserved_errno_; }
  954. // Must be called without the log_mutex held. (L < log_mutex)
  955. static int64 num_messages(int severity);
  956. private:
  957. // Fully internal SendMethod cases:
  958. void SendToSinkAndLog(); // Send to sink if provided and dispatch to the logs
  959. void SendToSink(); // Send to sink if provided, do nothing otherwise.
  960. // Write to string if provided and dispatch to the logs.
  961. void WriteToStringAndLog();
  962. void SaveOrSendToLog(); // Save to stringvec if provided, else to logs
  963. void Init(const char* file, int line, LogSeverity severity,
  964. void (LogMessage::*send_method)());
  965. // Used to fill in crash information during LOG(FATAL) failures.
  966. void RecordCrashReason(glog_internal_namespace_::CrashReason* reason);
  967. // Counts of messages sent at each priority:
  968. static int64 num_messages_[NUM_SEVERITIES]; // under log_mutex
  969. // We keep the data in a separate struct so that each instance of
  970. // LogMessage uses less stack space.
  971. struct GOOGLE_GLOG_DLL_DECL LogMessageData {
  972. LogMessageData() {};
  973. int preserved_errno_; // preserved errno
  974. char* buf_;
  975. char* message_text_; // Complete message text (points to selected buffer)
  976. LogStream* stream_alloc_;
  977. LogStream* stream_;
  978. char severity_; // What level is this LogMessage logged at?
  979. int line_; // line number where logging call is.
  980. void (LogMessage::*send_method_)(); // Call this in destructor to send
  981. union { // At most one of these is used: union to keep the size low.
  982. LogSink* sink_; // NULL or sink to send message to
  983. std::vector<std::string>* outvec_; // NULL or vector to push message onto
  984. std::string* message_; // NULL or string to write message into
  985. };
  986. time_t timestamp_; // Time of creation of LogMessage
  987. struct ::tm tm_time_; // Time of creation of LogMessage
  988. size_t num_prefix_chars_; // # of chars of prefix in this message
  989. size_t num_chars_to_log_; // # of chars of msg to send to log
  990. size_t num_chars_to_syslog_; // # of chars of msg to send to syslog
  991. const char* basename_; // basename of file that called LOG
  992. const char* fullname_; // fullname of file that called LOG
  993. bool has_been_flushed_; // false => data has not been flushed
  994. bool first_fatal_; // true => this was first fatal msg
  995. ~LogMessageData();
  996. private:
  997. LogMessageData(const LogMessageData&);
  998. void operator=(const LogMessageData&);
  999. };
  1000. static LogMessageData fatal_msg_data_exclusive_;
  1001. static LogMessageData fatal_msg_data_shared_;
  1002. LogMessageData* allocated_;
  1003. LogMessageData* data_;
  1004. friend class LogDestination;
  1005. LogMessage(const LogMessage&);
  1006. void operator=(const LogMessage&);
  1007. };
  1008. // This class happens to be thread-hostile because all instances share
  1009. // a single data buffer, but since it can only be created just before
  1010. // the process dies, we don't worry so much.
  1011. class GOOGLE_GLOG_DLL_DECL LogMessageFatal : public LogMessage {
  1012. public:
  1013. LogMessageFatal(const char* file, int line);
  1014. LogMessageFatal(const char* file, int line, const CheckOpString& result);
  1015. ~LogMessageFatal() @ac_cv___attribute___noreturn@;
  1016. };
  1017. // A non-macro interface to the log facility; (useful
  1018. // when the logging level is not a compile-time constant).
  1019. inline void LogAtLevel(int const severity, std::string const &msg) {
  1020. LogMessage(__FILE__, __LINE__, severity).stream() << msg;
  1021. }
  1022. // A macro alternative of LogAtLevel. New code may want to use this
  1023. // version since there are two advantages: 1. this version outputs the
  1024. // file name and the line number where this macro is put like other
  1025. // LOG macros, 2. this macro can be used as C++ stream.
  1026. #define LOG_AT_LEVEL(severity) @ac_google_namespace@::LogMessage(__FILE__, __LINE__, severity).stream()
  1027. // A small helper for CHECK_NOTNULL().
  1028. template <typename T>
  1029. T* CheckNotNull(const char *file, int line, const char *names, T* t) {
  1030. if (t == NULL) {
  1031. LogMessageFatal(file, line, new std::string(names));
  1032. }
  1033. return t;
  1034. }
  1035. // Allow folks to put a counter in the LOG_EVERY_X()'ed messages. This
  1036. // only works if ostream is a LogStream. If the ostream is not a
  1037. // LogStream you'll get an assert saying as much at runtime.
  1038. GOOGLE_GLOG_DLL_DECL std::ostream& operator<<(std::ostream &os,
  1039. const PRIVATE_Counter&);
  1040. // Derived class for PLOG*() above.
  1041. class GOOGLE_GLOG_DLL_DECL ErrnoLogMessage : public LogMessage {
  1042. public:
  1043. ErrnoLogMessage(const char* file, int line, LogSeverity severity, int ctr,
  1044. void (LogMessage::*send_method)());
  1045. // Postpends ": strerror(errno) [errno]".
  1046. ~ErrnoLogMessage();
  1047. private:
  1048. ErrnoLogMessage(const ErrnoLogMessage&);
  1049. void operator=(const ErrnoLogMessage&);
  1050. };
  1051. // This class is used to explicitly ignore values in the conditional
  1052. // logging macros. This avoids compiler warnings like "value computed
  1053. // is not used" and "statement has no effect".
  1054. class GOOGLE_GLOG_DLL_DECL LogMessageVoidify {
  1055. public:
  1056. LogMessageVoidify() { }
  1057. // This has to be an operator with a precedence lower than << but
  1058. // higher than ?:
  1059. void operator&(std::ostream&) { }
  1060. };
  1061. // Flushes all log files that contains messages that are at least of
  1062. // the specified severity level. Thread-safe.
  1063. GOOGLE_GLOG_DLL_DECL void FlushLogFiles(LogSeverity min_severity);
  1064. // Flushes all log files that contains messages that are at least of
  1065. // the specified severity level. Thread-hostile because it ignores
  1066. // locking -- used for catastrophic failures.
  1067. GOOGLE_GLOG_DLL_DECL void FlushLogFilesUnsafe(LogSeverity min_severity);
  1068. //
  1069. // Set the destination to which a particular severity level of log
  1070. // messages is sent. If base_filename is "", it means "don't log this
  1071. // severity". Thread-safe.
  1072. //
  1073. GOOGLE_GLOG_DLL_DECL void SetLogDestination(LogSeverity severity,
  1074. const char* base_filename);
  1075. //
  1076. // Set the basename of the symlink to the latest log file at a given
  1077. // severity. If symlink_basename is empty, do not make a symlink. If
  1078. // you don't call this function, the symlink basename is the
  1079. // invocation name of the program. Thread-safe.
  1080. //
  1081. GOOGLE_GLOG_DLL_DECL void SetLogSymlink(LogSeverity severity,
  1082. const char* symlink_basename);
  1083. //
  1084. // Used to send logs to some other kind of destination
  1085. // Users should subclass LogSink and override send to do whatever they want.
  1086. // Implementations must be thread-safe because a shared instance will
  1087. // be called from whichever thread ran the LOG(XXX) line.
  1088. class GOOGLE_GLOG_DLL_DECL LogSink {
  1089. public:
  1090. virtual ~LogSink();
  1091. // Sink's logging logic (message_len is such as to exclude '\n' at the end).
  1092. // This method can't use LOG() or CHECK() as logging system mutex(s) are held
  1093. // during this call.
  1094. virtual void send(LogSeverity severity, const char* full_filename,
  1095. const char* base_filename, int line,
  1096. const struct ::tm* tm_time,
  1097. const char* message, size_t message_len) = 0;
  1098. // Redefine this to implement waiting for
  1099. // the sink's logging logic to complete.
  1100. // It will be called after each send() returns,
  1101. // but before that LogMessage exits or crashes.
  1102. // By default this function does nothing.
  1103. // Using this function one can implement complex logic for send()
  1104. // that itself involves logging; and do all this w/o causing deadlocks and
  1105. // inconsistent rearrangement of log messages.
  1106. // E.g. if a LogSink has thread-specific actions, the send() method
  1107. // can simply add the message to a queue and wake up another thread that
  1108. // handles real logging while itself making some LOG() calls;
  1109. // WaitTillSent() can be implemented to wait for that logic to complete.
  1110. // See our unittest for an example.
  1111. virtual void WaitTillSent();
  1112. // Returns the normal text output of the log message.
  1113. // Can be useful to implement send().
  1114. static std::string ToString(LogSeverity severity, const char* file, int line,
  1115. const struct ::tm* tm_time,
  1116. const char* message, size_t message_len);
  1117. };
  1118. // Add or remove a LogSink as a consumer of logging data. Thread-safe.
  1119. GOOGLE_GLOG_DLL_DECL void AddLogSink(LogSink *destination);
  1120. GOOGLE_GLOG_DLL_DECL void RemoveLogSink(LogSink *destination);
  1121. //
  1122. // Specify an "extension" added to the filename specified via
  1123. // SetLogDestination. This applies to all severity levels. It's
  1124. // often used to append the port we're listening on to the logfile
  1125. // name. Thread-safe.
  1126. //
  1127. GOOGLE_GLOG_DLL_DECL void SetLogFilenameExtension(
  1128. const char* filename_extension);
  1129. //
  1130. // Make it so that all log messages of at least a particular severity
  1131. // are logged to stderr (in addition to logging to the usual log
  1132. // file(s)). Thread-safe.
  1133. //
  1134. GOOGLE_GLOG_DLL_DECL void SetStderrLogging(LogSeverity min_severity);
  1135. //
  1136. // Make it so that all log messages go only to stderr. Thread-safe.
  1137. //
  1138. GOOGLE_GLOG_DLL_DECL void LogToStderr();
  1139. //
  1140. // Make it so that all log messages of at least a particular severity are
  1141. // logged via email to a list of addresses (in addition to logging to the
  1142. // usual log file(s)). The list of addresses is just a string containing
  1143. // the email addresses to send to (separated by spaces, say). Thread-safe.
  1144. //
  1145. GOOGLE_GLOG_DLL_DECL void SetEmailLogging(LogSeverity min_severity,
  1146. const char* addresses);
  1147. // A simple function that sends email. dest is a commma-separated
  1148. // list of addressess. Thread-safe.
  1149. GOOGLE_GLOG_DLL_DECL bool SendEmail(const char *dest,
  1150. const char *subject, const char *body);
  1151. GOOGLE_GLOG_DLL_DECL const std::vector<std::string>& GetLoggingDirectories();
  1152. // For tests only: Clear the internal [cached] list of logging directories to
  1153. // force a refresh the next time GetLoggingDirectories is called.
  1154. // Thread-hostile.
  1155. void TestOnly_ClearLoggingDirectoriesList();
  1156. // Returns a set of existing temporary directories, which will be a
  1157. // subset of the directories returned by GetLogginDirectories().
  1158. // Thread-safe.
  1159. GOOGLE_GLOG_DLL_DECL void GetExistingTempDirectories(
  1160. std::vector<std::string>* list);
  1161. // Print any fatal message again -- useful to call from signal handler
  1162. // so that the last thing in the output is the fatal message.
  1163. // Thread-hostile, but a race is unlikely.
  1164. GOOGLE_GLOG_DLL_DECL void ReprintFatalMessage();
  1165. // Truncate a log file that may be the append-only output of multiple
  1166. // processes and hence can't simply be renamed/reopened (typically a
  1167. // stdout/stderr). If the file "path" is > "limit" bytes, copy the
  1168. // last "keep" bytes to offset 0 and truncate the rest. Since we could
  1169. // be racing with other writers, this approach has the potential to
  1170. // lose very small amounts of data. For security, only follow symlinks
  1171. // if the path is /proc/self/fd/*
  1172. GOOGLE_GLOG_DLL_DECL void TruncateLogFile(const char *path,
  1173. int64 limit, int64 keep);
  1174. // Truncate stdout and stderr if they are over the value specified by
  1175. // --max_log_size; keep the final 1MB. This function has the same
  1176. // race condition as TruncateLogFile.
  1177. GOOGLE_GLOG_DLL_DECL void TruncateStdoutStderr();
  1178. // Return the string representation of the provided LogSeverity level.
  1179. // Thread-safe.
  1180. GOOGLE_GLOG_DLL_DECL const char* GetLogSeverityName(LogSeverity severity);
  1181. // ---------------------------------------------------------------------
  1182. // Implementation details that are not useful to most clients
  1183. // ---------------------------------------------------------------------
  1184. // A Logger is the interface used by logging modules to emit entries
  1185. // to a log. A typical implementation will dump formatted data to a
  1186. // sequence of files. We also provide interfaces that will forward
  1187. // the data to another thread so that the invoker never blocks.
  1188. // Implementations should be thread-safe since the logging system
  1189. // will write to them from multiple threads.
  1190. namespace base {
  1191. class GOOGLE_GLOG_DLL_DECL Logger {
  1192. public:
  1193. virtual ~Logger();
  1194. // Writes "message[0,message_len-1]" corresponding to an event that
  1195. // occurred at "timestamp". If "force_flush" is true, the log file
  1196. // is flushed immediately.
  1197. //
  1198. // The input message has already been formatted as deemed
  1199. // appropriate by the higher level logging facility. For example,
  1200. // textual log messages already contain timestamps, and the
  1201. // file:linenumber header.
  1202. virtual void Write(bool force_flush,
  1203. time_t timestamp,
  1204. const char* message,
  1205. int message_len) = 0;
  1206. // Flush any buffered messages
  1207. virtual void Flush() = 0;
  1208. // Get the current LOG file size.
  1209. // The returned value is approximate since some
  1210. // logged data may not have been flushed to disk yet.
  1211. virtual uint32 LogSize() = 0;
  1212. };
  1213. // Get the logger for the specified severity level. The logger
  1214. // remains the property of the logging module and should not be
  1215. // deleted by the caller. Thread-safe.
  1216. extern GOOGLE_GLOG_DLL_DECL Logger* GetLogger(LogSeverity level);
  1217. // Set the logger for the specified severity level. The logger
  1218. // becomes the property of the logging module and should not
  1219. // be deleted by the caller. Thread-safe.
  1220. extern GOOGLE_GLOG_DLL_DECL void SetLogger(LogSeverity level, Logger* logger);
  1221. }
  1222. // glibc has traditionally implemented two incompatible versions of
  1223. // strerror_r(). There is a poorly defined convention for picking the
  1224. // version that we want, but it is not clear whether it even works with
  1225. // all versions of glibc.
  1226. // So, instead, we provide this wrapper that automatically detects the
  1227. // version that is in use, and then implements POSIX semantics.
  1228. // N.B. In addition to what POSIX says, we also guarantee that "buf" will
  1229. // be set to an empty string, if this function failed. This means, in most
  1230. // cases, you do not need to check the error code and you can directly
  1231. // use the value of "buf". It will never have an undefined value.
  1232. GOOGLE_GLOG_DLL_DECL int posix_strerror_r(int err, char *buf, size_t len);
  1233. // A class for which we define operator<<, which does nothing.
  1234. class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream {
  1235. public:
  1236. // Initialize the LogStream so the messages can be written somewhere
  1237. // (they'll never be actually displayed). This will be needed if a
  1238. // NullStream& is implicitly converted to LogStream&, in which case
  1239. // the overloaded NullStream::operator<< will not be invoked.
  1240. NullStream() : LogMessage::LogStream(message_buffer_, 1, 0) { }
  1241. NullStream(const char* /*file*/, int /*line*/,
  1242. const CheckOpString& /*result*/) :
  1243. LogMessage::LogStream(message_buffer_, 1, 0) { }
  1244. NullStream &stream() { return *this; }
  1245. private:
  1246. // A very short buffer for messages (which we discard anyway). This
  1247. // will be needed if NullStream& converted to LogStream& (e.g. as a
  1248. // result of a conditional expression).
  1249. char message_buffer_[2];
  1250. };
  1251. // Do nothing. This operator is inline, allowing the message to be
  1252. // compiled away. The message will not be compiled away if we do
  1253. // something like (flag ? LOG(INFO) : LOG(ERROR)) << message; when
  1254. // SKIP_LOG=WARNING. In those cases, NullStream will be implicitly
  1255. // converted to LogStream and the message will be computed and then
  1256. // quietly discarded.
  1257. template<class T>
  1258. inline NullStream& operator<<(NullStream &str, const T &value) { return str; }
  1259. // Similar to NullStream, but aborts the program (without stack
  1260. // trace), like LogMessageFatal.
  1261. class GOOGLE_GLOG_DLL_DECL NullStreamFatal : public NullStream {
  1262. public:
  1263. NullStreamFatal() { }
  1264. NullStreamFatal(const char* file, int line, const CheckOpString& result) :
  1265. NullStream(file, line, result) { }
  1266. @ac_cv___attribute___noreturn@ ~NullStreamFatal() { _exit(1); }
  1267. };
  1268. // Install a signal handler that will dump signal information and a stack
  1269. // trace when the program crashes on certain signals. We'll install the
  1270. // signal handler for the following signals.
  1271. //
  1272. // SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGBUS, and SIGTERM.
  1273. //
  1274. // By default, the signal handler will write the failure dump to the
  1275. // standard error. You can customize the destination by installing your
  1276. // own writer function by InstallFailureWriter() below.
  1277. //
  1278. // Note on threading:
  1279. //
  1280. // The function should be called before threads are created, if you want
  1281. // to use the failure signal handler for all threads. The stack trace
  1282. // will be shown only for the thread that receives the signal. In other
  1283. // words, stack traces of other threads won't be shown.
  1284. GOOGLE_GLOG_DLL_DECL void InstallFailureSignalHandler();
  1285. // Installs a function that is used for writing the failure dump. "data"
  1286. // is the pointer to the beginning of a message to be written, and "size"
  1287. // is the size of the message. You should not expect the data is
  1288. // terminated with '\0'.
  1289. GOOGLE_GLOG_DLL_DECL void InstallFailureWriter(
  1290. void (*writer)(const char* data, int size));
  1291. @ac_google_end_namespace@
  1292. #endif // _LOGGING_H_