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 44 inline void MakeStringInternal(std::stringstream& ss) {}
47 inline void MakeStringInternal(std::stringstream& ss,
const T& t) {
51 template <
typename T,
typename... Args>
53 MakeStringInternal(std::stringstream& ss,
const T& t,
const Args&... args) {
54 MakeStringInternal(ss, t);
55 MakeStringInternal(ss, args...);
58 template <
typename... Args>
59 string MakeString(
const Args&... args) {
61 MakeStringInternal(ss, args...);
62 return string(ss.str());
67 inline string MakeString(
const string& str) {
70 inline string MakeString(
const char* c_str) {
74 template <
class Container>
75 inline string Join(
const string& delimiter,
const Container& v) {
77 int cnt =
static_cast<int64_t
>(v.size()) - 1;
78 for (
auto i = v.begin(); i != v.end(); ++i, --cnt) {
79 s << (*i) << (cnt ? delimiter :
"");
85 string StripBasename(
const std::string& full_path);
89 size_t ReplaceAll(
string& s,
const char* from,
const char* to);
91 void SetStackTraceFetcher(std::function<
string(
void)> fetcher);
93 void SetOperatorLogger(std::function<
void(
const OperatorDef&)> tracer);
94 std::function<void(const OperatorDef&)> GetOperatorLogger();
101 const char* condition,
103 const void* caller=
nullptr);
104 void AppendMessage(
const string& msg);
106 inline const vector<string>& msg_stack()
const {
110 const char* what()
const noexcept
override;
112 const void* caller()
const noexcept;
115 vector<string> msg_stack_;
121 #define CAFFE_ENFORCE(condition, ...) \ 123 if (!(condition)) { \ 124 throw ::caffe2::EnforceNotMet( \ 125 __FILE__, __LINE__, #condition, ::caffe2::MakeString(__VA_ARGS__)); \ 129 #define CAFFE_ENFORCE_WITH_CALLER(condition, ...) \ 131 if (!(condition)) { \ 132 throw ::caffe2::EnforceNotMet( \ 133 __FILE__, __LINE__, #condition, ::caffe2::MakeString(__VA_ARGS__), this); \ 137 #define CAFFE_THROW(...) \ 138 throw ::caffe2::EnforceNotMet( \ 139 __FILE__, __LINE__, "", ::caffe2::MakeString(__VA_ARGS__)) 168 namespace enforce_detail {
180 EnforceFailMessage(EnforceFailMessage&&) =
default;
181 EnforceFailMessage(
const EnforceFailMessage&) =
delete;
182 EnforceFailMessage& operator=(EnforceFailMessage&&) =
delete;
183 EnforceFailMessage& operator=(
const EnforceFailMessage&) =
delete;
186 template <
class... Args>
187 EnforceFailMessage(Args...) {
191 sizeof...(Args) == std::numeric_limits<std::size_t>::max(),
192 "CAFFE_ENFORCE_THAT has to be used with one of special check functions " 193 "like `Equals`. Use CAFFE_ENFORCE for simple boolean checks.");
196 EnforceFailMessage(std::string&& msg) {
197 msg_ =
new std::string(std::move(msg));
199 inline bool bad()
const {
200 return msg_ !=
nullptr;
202 std::string get_message_and_free(std::string&& extra)
const {
205 r = std::move(*msg_);
207 r = ::caffe2::MakeString(std::move(*msg_),
". ", std::move(extra));
217 #define BINARY_COMP_HELPER(name, op) \ 218 template <typename T1, typename T2> \ 219 inline EnforceFailMessage name(const T1& x, const T2& y) { \ 221 return EnforceOK(); \ 223 return MakeString(x, " vs ", y); \ 225 BINARY_COMP_HELPER(Equals, ==)
226 BINARY_COMP_HELPER(NotEquals, !=)
227 BINARY_COMP_HELPER(Greater, >)
228 BINARY_COMP_HELPER(GreaterEquals, >=)
229 BINARY_COMP_HELPER(Less, <)
230 BINARY_COMP_HELPER(LessEquals, <=)
231 #undef BINARY_COMP_HELPER 233 #define CAFFE_ENFORCE_THAT_IMPL(condition, expr, ...) \ 235 using namespace ::caffe2::enforce_detail; \ 236 const EnforceFailMessage& r = (condition); \ 238 throw ::caffe2::EnforceNotMet( \ 242 r.get_message_and_free(::caffe2::MakeString(__VA_ARGS__))); \ 247 #define CAFFE_ENFORCE_THAT(condition, ...) \ 248 CAFFE_ENFORCE_THAT_IMPL((condition), #condition, __VA_ARGS__) 250 #define CAFFE_ENFORCE_EQ(x, y, ...) \ 251 CAFFE_ENFORCE_THAT_IMPL(Equals((x), (y)), #x " == " #y, __VA_ARGS__) 252 #define CAFFE_ENFORCE_NE(x, y, ...) \ 253 CAFFE_ENFORCE_THAT_IMPL(NotEquals((x), (y)), #x " != " #y, __VA_ARGS__) 254 #define CAFFE_ENFORCE_LE(x, y, ...) \ 255 CAFFE_ENFORCE_THAT_IMPL(LessEquals((x), (y)), #x " <= " #y, __VA_ARGS__) 256 #define CAFFE_ENFORCE_LT(x, y, ...) \ 257 CAFFE_ENFORCE_THAT_IMPL(Less((x), (y)), #x " < " #y, __VA_ARGS__) 258 #define CAFFE_ENFORCE_GE(x, y, ...) \ 259 CAFFE_ENFORCE_THAT_IMPL(GreaterEquals((x), (y)), #x " >= " #y, __VA_ARGS__) 260 #define CAFFE_ENFORCE_GT(x, y, ...) \ 261 CAFFE_ENFORCE_THAT_IMPL(Greater((x), (y)), #x " > " #y, __VA_ARGS__) 265 #endif // CAFFE2_CORE_LOGGING_H_
Simple registry implementation in Caffe2 that uses static variables to register object creators durin...
Commandline flags support for Caffe2.