00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef OR_TOOLS_BASE_LOGGING_H_
00015 #define OR_TOOLS_BASE_LOGGING_H_
00016
00017 #include <assert.h>
00018 #include <stdlib.h>
00019 #include <iostream>
00020 #include "base/commandlineflags.h"
00021 #include "base/macros.h"
00022
00023 DECLARE_int32(log_level);
00024 DECLARE_bool(log_prefix);
00025
00026
00027 #define DCHECK(condition) assert(condition)
00028 #define DCHECK_EQ(val1, val2) assert((val1) == (val2))
00029 #define DCHECK_NE(val1, val2) assert((val1) != (val2))
00030 #define DCHECK_LE(val1, val2) assert((val1) <= (val2))
00031 #define DCHECK_LT(val1, val2) assert((val1) < (val2))
00032 #define DCHECK_GE(val1, val2) assert((val1) >= (val2))
00033 #define DCHECK_GT(val1, val2) assert((val1) > (val2))
00034
00035
00036 #define CHECK(x) \
00037 if (!(x)) LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x
00038 #define CHECK_LT(x, y) CHECK((x) < (y))
00039 #define CHECK_GT(x, y) CHECK((x) > (y))
00040 #define CHECK_LE(x, y) CHECK((x) <= (y))
00041 #define CHECK_GE(x, y) CHECK((x) >= (y))
00042 #define CHECK_EQ(x, y) CHECK((x) == (y))
00043 #define CHECK_NE(x, y) CHECK((x) != (y))
00044 #define CHECK_NOTNULL(x) CHECK((x) != NULL)
00045
00046 #define LOG_INFO LogMessage(__FILE__, __LINE__)
00047 #define LOG_ERROR LOG_INFO
00048 #define LOG_WARNING LOG_INFO
00049 #define LOG_FATAL LogMessageFatal(__FILE__, __LINE__)
00050 #define LOG_QFATAL LOG_FATAL
00051
00052 #define VLOG(x) if ((x) <= FLAGS_log_level) LOG_INFO.stream()
00053
00054 #ifdef NDEBUG
00055 #define DEBUG_MODE 0
00056 #define LOG_DFATAL LOG_ERROR
00057 #else
00058 #define DEBUG_MODE 1
00059 #define LOG_DFATAL LOG_FATAL
00060 #endif
00061
00062 #define LOG(severity) LOG_ ## severity.stream()
00063 #define LG LOG_INFO.stream()
00064
00065 namespace operations_research {
00066 class DateLogger {
00067 public:
00068 DateLogger();
00069 char* const HumanDate();
00070 private:
00071 char buffer_[9];
00072 };
00073 }
00074
00075 class LogMessage {
00076 public:
00077 LogMessage(const char* file, int line) {
00078 if (FLAGS_log_prefix) {
00079 std::cerr << "[" << pretty_date_.HumanDate() << "] "
00080 << file << ":" << line << ": ";
00081 }
00082 }
00083 ~LogMessage() { std::cerr << "\n"; }
00084 std::ostream& stream() { return std::cerr; }
00085
00086 private:
00087 operations_research::DateLogger pretty_date_;
00088 DISALLOW_COPY_AND_ASSIGN(LogMessage);
00089 };
00090
00091 class LogMessageFatal : public LogMessage {
00092 public:
00093 LogMessageFatal(const char* file, int line)
00094 : LogMessage(file, line) { }
00095 ~LogMessageFatal() {
00096 std::cerr << "\n";
00097 abort();
00098 }
00099 private:
00100 DISALLOW_COPY_AND_ASSIGN(LogMessageFatal);
00101 };
00102
00103 #endif // OR_TOOLS_BASE_LOGGING_H_