1 #ifndef CAFFE2_CORE_LOGGING_H_ 2 #define CAFFE2_CORE_LOGGING_H_ 11 #include "caffe2/proto/caffe2.pb.h" 16 #ifndef CAFFE2_LOG_THRESHOLD 19 #define CAFFE2_LOG_THRESHOLD INT_MIN 20 #endif // CAFFE2_LOG_THRESHOLD 23 #ifdef CAFFE2_USE_GOOGLE_GLOG 24 #include "caffe2/core/logging_is_google_glog.h" 25 #else // !CAFFE2_USE_GOOGLE_GLOG 26 #include "caffe2/core/logging_is_not_google_glog.h" 27 #endif // CAFFE2_USE_GOOGLE_GLOG 29 CAFFE2_DECLARE_int(caffe2_log_level);
30 CAFFE2_DECLARE_bool(caffe2_use_fatal_for_enforce);
34 bool InitCaffeLogging(
int* argc,
char** argv);
36 constexpr
bool IsUsingGoogleLogging() {
37 #ifdef CAFFE2_USE_GOOGLE_GLOG 54 inline void MakeStringInternal(std::stringstream& ) {}
57 inline void MakeStringInternal(std::stringstream& ss,
const T& t) {
61 template <
typename T,
typename... Args>
63 MakeStringInternal(std::stringstream& ss,
const T& t,
const Args&... args) {
64 MakeStringInternal(ss, t);
65 MakeStringInternal(ss, args...);
68 template <
typename... Args>
69 string MakeString(
const Args&... args) {
71 MakeStringInternal(ss, args...);
72 return string(ss.str());
77 inline string MakeString(
const string& str) {
80 inline string MakeString(
const char* c_str) {
84 template <
class Container>
85 inline string Join(
const string& delimiter,
const Container& v) {
87 int cnt =
static_cast<int64_t
>(v.size()) - 1;
88 for (
auto i = v.begin(); i != v.end(); ++i, --cnt) {
89 s << (*i) << (cnt ? delimiter :
"");
95 string StripBasename(
const std::string& full_path);
99 size_t ReplaceAll(
string& s,
const char* from,
const char* to);
101 void SetStackTraceFetcher(std::function<
string(
void)> fetcher);
103 void SetOperatorLogger(std::function<
void(
const OperatorDef&)> tracer);
104 std::function<void(const OperatorDef&)> GetOperatorLogger();
111 const char* condition,
113 const void* caller=
nullptr);
114 void AppendMessage(
const string& msg);
116 inline const vector<string>& msg_stack()
const {
120 const char* what()
const noexcept
override;
122 const void* caller()
const noexcept;
125 vector<string> msg_stack_;
131 #define CAFFE_ENFORCE(condition, ...) \ 133 if (!(condition)) { \ 134 throw ::caffe2::EnforceNotMet( \ 135 __FILE__, __LINE__, #condition, ::caffe2::MakeString(__VA_ARGS__)); \ 139 #define CAFFE_ENFORCE_WITH_CALLER(condition, ...) \ 141 if (!(condition)) { \ 142 throw ::caffe2::EnforceNotMet( \ 143 __FILE__, __LINE__, #condition, ::caffe2::MakeString(__VA_ARGS__), this); \ 147 #define CAFFE_THROW(...) \ 148 throw ::caffe2::EnforceNotMet( \ 149 __FILE__, __LINE__, "", ::caffe2::MakeString(__VA_ARGS__)) 178 namespace enforce_detail {
190 EnforceFailMessage(EnforceFailMessage&&) =
default;
191 EnforceFailMessage(
const EnforceFailMessage&) =
delete;
192 EnforceFailMessage& operator=(EnforceFailMessage&&) =
delete;
193 EnforceFailMessage& operator=(
const EnforceFailMessage&) =
delete;
196 template <
class... Args>
197 EnforceFailMessage(Args...) {
201 sizeof...(Args) == std::numeric_limits<std::size_t>::max(),
202 "CAFFE_ENFORCE_THAT has to be used with one of special check functions " 203 "like `Equals`. Use CAFFE_ENFORCE for simple boolean checks.");
206 EnforceFailMessage(std::string&& msg) {
207 msg_ =
new std::string(std::move(msg));
209 inline bool bad()
const {
210 return msg_ !=
nullptr;
212 std::string get_message_and_free(std::string&& extra)
const {
215 r = std::move(*msg_);
217 r = ::caffe2::MakeString(std::move(*msg_),
". ", std::move(extra));
227 #define BINARY_COMP_HELPER(name, op) \ 228 template <typename T1, typename T2> \ 229 inline EnforceFailMessage name(const T1& x, const T2& y) { \ 231 return EnforceOK(); \ 233 return MakeString(x, " vs ", y); \ 235 BINARY_COMP_HELPER(Equals, ==)
236 BINARY_COMP_HELPER(NotEquals, !=)
237 BINARY_COMP_HELPER(Greater, >)
238 BINARY_COMP_HELPER(GreaterEquals, >=)
239 BINARY_COMP_HELPER(Less, <)
240 BINARY_COMP_HELPER(LessEquals, <=)
241 #undef BINARY_COMP_HELPER 243 #define CAFFE_ENFORCE_THAT_IMPL(condition, expr, ...) \ 245 using namespace ::caffe2::enforce_detail; \ 246 const EnforceFailMessage& CAFFE_ENFORCE_THAT_IMPL_r_ = (condition); \ 247 if (CAFFE_ENFORCE_THAT_IMPL_r_.bad()) { \ 248 throw ::caffe2::EnforceNotMet( \ 252 CAFFE_ENFORCE_THAT_IMPL_r_.get_message_and_free( \ 253 ::caffe2::MakeString(__VA_ARGS__))); \ 257 #define CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(condition, expr, ...) \ 259 using namespace ::caffe2::enforce_detail; \ 260 const EnforceFailMessage& CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER_r_ = \ 262 if (CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER_r_.bad()) { \ 263 throw ::caffe2::EnforceNotMet( \ 267 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER_r_.get_message_and_free( \ 268 ::caffe2::MakeString(__VA_ARGS__)), \ 274 #define CAFFE_ENFORCE_THAT(condition, ...) \ 275 CAFFE_ENFORCE_THAT_IMPL((condition), #condition, __VA_ARGS__) 277 #define CAFFE_ENFORCE_EQ(x, y, ...) \ 278 CAFFE_ENFORCE_THAT_IMPL(Equals((x), (y)), #x " == " #y, __VA_ARGS__) 279 #define CAFFE_ENFORCE_NE(x, y, ...) \ 280 CAFFE_ENFORCE_THAT_IMPL(NotEquals((x), (y)), #x " != " #y, __VA_ARGS__) 281 #define CAFFE_ENFORCE_LE(x, y, ...) \ 282 CAFFE_ENFORCE_THAT_IMPL(LessEquals((x), (y)), #x " <= " #y, __VA_ARGS__) 283 #define CAFFE_ENFORCE_LT(x, y, ...) \ 284 CAFFE_ENFORCE_THAT_IMPL(Less((x), (y)), #x " < " #y, __VA_ARGS__) 285 #define CAFFE_ENFORCE_GE(x, y, ...) \ 286 CAFFE_ENFORCE_THAT_IMPL(GreaterEquals((x), (y)), #x " >= " #y, __VA_ARGS__) 287 #define CAFFE_ENFORCE_GT(x, y, ...) \ 288 CAFFE_ENFORCE_THAT_IMPL(Greater((x), (y)), #x " > " #y, __VA_ARGS__) 289 #define CAFFE_ENFORCE_EQ_WITH_CALLER(x, y, ...) \ 290 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(Equals((x), (y)), #x " == " #y, __VA_ARGS__) 291 #define CAFFE_ENFORCE_NE_WITH_CALLER(x, y, ...) \ 292 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(NotEquals((x), (y)), #x " != " #y, __VA_ARGS__) 293 #define CAFFE_ENFORCE_LE_WITH_CALLER(x, y, ...) \ 294 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(LessEquals((x), (y)), #x " <= " #y, __VA_ARGS__) 295 #define CAFFE_ENFORCE_LT_WITH_CALLER(x, y, ...) \ 296 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(Less((x), (y)), #x " < " #y, __VA_ARGS__) 297 #define CAFFE_ENFORCE_GE_WITH_CALLER(x, y, ...) \ 298 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(GreaterEquals((x), (y)), #x " >= " #y, __VA_ARGS__) 299 #define CAFFE_ENFORCE_GT_WITH_CALLER(x, y, ...) \ 300 CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(Greater((x), (y)), #x " > " #y, __VA_ARGS__) 303 #endif // CAFFE2_CORE_LOGGING_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
void ShowLogInfoToStderr()
A utility to allow one to show log info to stderr after the program starts.
Commandline flags support for Caffe2.