Caffe2 - C++ API
A deep learning, cross platform ML framework
logging_is_not_google_glog.h
1 #ifndef CAFFE2_CORE_LOGGING_IS_NOT_GOOGLE_GLOG_H_
2 #define CAFFE2_CORE_LOGGING_IS_NOT_GOOGLE_GLOG_H_
3 
4 #include <chrono>
5 #include <climits>
6 #include <ctime>
7 #include <iomanip>
8 #include <string>
9 #include <fstream>
10 #include <set>
11 #include <sstream>
12 #include <vector>
13 
14 #include "caffe2/core/flags.h"
15 
16 // Log severity level constants.
17 const int FATAL = 3;
18 #if !defined(_MSC_VER) || !defined(ERROR)
19 // Windows defines the ERROR macro already, and as a result we will
20 // simply use that one. The downside is that one will now mix LOG(INFO)
21 // and LOG(ERROR) because ERROR is defined to be zero. Anyway, the
22 // recommended way is to use glog so fixing this is a low-pri item.
23 const int ERROR = 2;
24 #endif
25 const int WARNING = 1;
26 const int INFO = 0;
27 const char CAFFE2_SEVERITY_PREFIX[] = "FEWIV";
28 
29 namespace caffe2 {
31  public:
32  MessageLogger(const char *file, int line, int severity);
33  ~MessageLogger();
34  // Return the stream associated with the logger object.
35  std::stringstream &stream() { return stream_; }
36 
37  private:
38  // When there is a fatal log, we simply abort.
39  void DealWithFatal() { abort(); }
40 
41  const char* tag_;
42  std::stringstream stream_;
43  int severity_;
44 };
45 
46 // This class is used to explicitly ignore values in the conditional
47 // logging macros. This avoids compiler warnings like "value computed
48 // is not used" and "statement has no effect".
50  public:
51  LoggerVoidify() { }
52  // This has to be an operator with a precedence lower than << but
53  // higher than ?:
54  void operator&(const std::ostream &s) { }
55 };
56 
57 // Log a message and terminate.
58 template<class T>
59 void LogMessageFatal(const char *file, int line, const T &message) {
60  MessageLogger(file, line, FATAL).stream() << message;
61 }
62 
63 // Helpers for CHECK_NOTNULL(). Two are necessary to support both raw pointers
64 // and smart pointers.
65 template <typename T>
66 T& CheckNotNullCommon(const char *file, int line, const char *names, T& t) {
67  if (t == nullptr) {
68  LogMessageFatal(file, line, std::string(names));
69  }
70  return t;
71 }
72 
73 template <typename T>
74 T* CheckNotNull(const char *file, int line, const char *names, T* t) {
75  return CheckNotNullCommon(file, line, names, t);
76 }
77 
78 template <typename T>
79 T& CheckNotNull(const char *file, int line, const char *names, T& t) {
80  return CheckNotNullCommon(file, line, names, t);
81 }
82 } // namespace caffe2
83 
84 // ---------------------- Logging Macro definitions --------------------------
85 
86 
87 static_assert(CAFFE2_LOG_THRESHOLD <= FATAL,
88  "CAFFE2_LOG_THRESHOLD should at most be FATAL.");
89 // If n is under the compile time caffe log threshold, The _CAFFE_LOG(n)
90 // should not generate anything in optimized code.
91 #define LOG(n) \
92  if (n >= CAFFE2_LOG_THRESHOLD) \
93  ::caffe2::MessageLogger((char*)__FILE__, __LINE__, n).stream()
94 #define VLOG(n) LOG((-n))
95 
96 #define LOG_IF(n, condition) \
97  if (n >= CAFFE2_LOG_THRESHOLD && (condition)) \
98  ::caffe2::MessageLogger((char*)__FILE__, __LINE__, n).stream()
99 #define VLOG_IF(n, condition) LOG_IF((-n), (condition))
100 
101 // Log only if condition is met. Otherwise evaluates to void.
102 #define FATAL_IF(condition) \
103  condition ? (void) 0 : ::caffe2::LoggerVoidify() & \
104  ::caffe2::MessageLogger((char*)__FILE__, __LINE__, FATAL).stream()
105 
106 // Check for a given boolean condition.
107 #define CHECK(condition) FATAL_IF(condition) \
108  << "Check failed: " #condition " "
109 
110 #ifndef NDEBUG
111 // Debug only version of CHECK
112 #define DCHECK(condition) FATAL_IF(condition) \
113  << "Check failed: " #condition " "
114 #else
115 // Optimized version - generates no code.
116 #define DCHECK(condition) if(false) CHECK(condition)
117 #endif // NDEBUG
118 
119 #define CHECK_OP(val1, val2, op) FATAL_IF((val1 op val2)) \
120  << "Check failed: " #val1 " " #op " " #val2 " "
121 
122 // Check_op macro definitions
123 #define CHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
124 #define CHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
125 #define CHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
126 #define CHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
127 #define CHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
128 #define CHECK_GT(val1, val2) CHECK_OP(val1, val2, >)
129 
130 #ifndef NDEBUG
131 // Debug only versions of CHECK_OP macros.
132 #define DCHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
133 #define DCHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
134 #define DCHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
135 #define DCHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
136 #define DCHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
137 #define DCHECK_GT(val1, val2) CHECK_OP(val1, val2, >)
138 #else // !NDEBUG
139 // These versions generate no code in optimized mode.
140 #define DCHECK_EQ(val1, val2) if(false) CHECK_OP(val1, val2, ==)
141 #define DCHECK_NE(val1, val2) if(false) CHECK_OP(val1, val2, !=)
142 #define DCHECK_LE(val1, val2) if(false) CHECK_OP(val1, val2, <=)
143 #define DCHECK_LT(val1, val2) if(false) CHECK_OP(val1, val2, <)
144 #define DCHECK_GE(val1, val2) if(false) CHECK_OP(val1, val2, >=)
145 #define DCHECK_GT(val1, val2) if(false) CHECK_OP(val1, val2, >)
146 #endif // NDEBUG
147 
148 // Check that a pointer is not null.
149 #define CHECK_NOTNULL(val) \
150  ::caffe2::CheckNotNull( \
151  __FILE__, __LINE__, "Check failed: '" #val "' Must be non NULL", (val))
152 
153 #ifndef NDEBUG
154 // Debug only version of CHECK_NOTNULL
155 #define DCHECK_NOTNULL(val) \
156  ::caffe2::CheckNotNull( \
157  __FILE__, __LINE__, "Check failed: '" #val "' Must be non NULL", (val))
158 #else // !NDEBUG
159 // Optimized version - generates no code.
160 #define DCHECK_NOTNULL(val) if (false) CHECK_NOTNULL(val)
161 #endif // NDEBUG
162 
163 // ---------------------- Support for std objects --------------------------
164 // These are adapted from glog to support a limited set of logging capability
165 // for STL objects.
166 
167 namespace caffe2 {
168 // Forward declare these two, and define them after all the container streams
169 // operators so that we can recurse from pair -> container -> container -> pair
170 // properly.
171 template<class First, class Second>
172 std::ostream& operator<<(
173  std::ostream& out, const std::pair<First, Second>& p);
174 template <class Iter>
175 void PrintSequence(std::ostream& ss, Iter begin, Iter end);
176 
177 #define INSTANTIATE_FOR_CONTAINER(container) \
178 template <class... Types> \
179 std::ostream& operator<<( \
180  std::ostream& out, const container<Types...>& seq) { \
181  PrintSequence(out, seq.begin(), seq.end()); \
182  return out; \
183 }
184 
185 INSTANTIATE_FOR_CONTAINER(std::vector)
186 INSTANTIATE_FOR_CONTAINER(std::map)
187 INSTANTIATE_FOR_CONTAINER(std::set)
188 #undef INSTANTIATE_FOR_CONTAINER
189 
190 template<class First, class Second>
191 inline std::ostream& operator<<(
192  std::ostream& out, const std::pair<First, Second>& p) {
193  out << '(' << p.first << ", " << p.second << ')';
194  return out;
195 }
196 
197 template <class Iter>
198 inline void PrintSequence(std::ostream& out, Iter begin, Iter end) {
199  // Output at most 100 elements -- appropriate if used for logging.
200  for (int i = 0; begin != end && i < 100; ++i, ++begin) {
201  if (i > 0) out << ' ';
202  out << *begin;
203  }
204  if (begin != end) {
205  out << " ...";
206  }
207 }
208 } // namespace caffe2
209 
210 #endif // CAFFE2_CORE_LOGGING_IS_NOT_GOOGLE_GLOG_H_
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
Commandline flags support for Caffe2.