|
#ifndef C10_UTIL_LOGGING_H_ |
|
#define C10_UTIL_LOGGING_H_ |
|
|
|
#include <climits> |
|
#include <exception> |
|
#include <functional> |
|
#include <limits> |
|
#include <sstream> |
|
|
|
#include <c10/macros/Macros.h> |
|
#include <c10/util/Exception.h> |
|
#include <c10/util/Flags.h> |
|
#include <c10/util/StringUtil.h> |
|
|
|
|
|
|
|
|
|
#ifndef CAFFE2_LOG_THRESHOLD |
|
|
|
|
|
#define CAFFE2_LOG_THRESHOLD INT_MIN |
|
#endif |
|
|
|
|
|
#ifdef C10_USE_GLOG |
|
#include <c10/util/logging_is_google_glog.h> |
|
#else |
|
#include <c10/util/logging_is_not_google_glog.h> |
|
#endif |
|
|
|
C10_DECLARE_int(caffe2_log_level); |
|
C10_DECLARE_bool(caffe2_use_fatal_for_enforce); |
|
|
|
|
|
|
|
|
|
#ifdef LOG_EVERY_MS |
|
#define C10_LOG_EVERY_MS(severity, ms) LOG_EVERY_MS(severity, ms) |
|
#else |
|
#define C10_LOG_EVERY_MS(severity, ms) LOG(severity) |
|
#endif |
|
|
|
|
|
#ifdef LOG_FIRST_N |
|
#define C10_LOG_FIRST_N(severity, n) LOG_FIRST_N(severity, n) |
|
#else |
|
#define C10_LOG_FIRST_N(severity, n) LOG(severity) |
|
#endif |
|
|
|
|
|
#ifdef LOG_EVERY_N |
|
#define C10_LOG_EVERY_N(severity, n) LOG_EVERY_N(severity, n) |
|
#else |
|
#define C10_LOG_EVERY_N(severity, n) LOG(severity) |
|
#endif |
|
|
|
namespace c10 { |
|
|
|
using std::string; |
|
|
|
|
|
C10_API bool InitCaffeLogging(int* argc, char** argv); |
|
C10_API void UpdateLoggingLevelsFromFlags(); |
|
|
|
[[noreturn]] C10_API void ThrowEnforceNotMet( |
|
const char* file, |
|
const int line, |
|
const char* condition, |
|
const std::string& msg, |
|
const void* caller = nullptr); |
|
|
|
[[noreturn]] C10_API void ThrowEnforceNotMet( |
|
const char* file, |
|
const int line, |
|
const char* condition, |
|
const char* msg, |
|
const void* caller = nullptr); |
|
|
|
[[noreturn]] C10_API inline void ThrowEnforceNotMet( |
|
const char* file, |
|
const int line, |
|
const char* condition, |
|
detail::CompileTimeEmptyString , |
|
const void* caller = nullptr) { |
|
ThrowEnforceNotMet(file, line, condition, "", caller); |
|
} |
|
|
|
[[noreturn]] C10_API void ThrowEnforceFiniteNotMet( |
|
const char* file, |
|
const int line, |
|
const char* condition, |
|
const std::string& msg, |
|
const void* caller = nullptr); |
|
|
|
[[noreturn]] C10_API void ThrowEnforceFiniteNotMet( |
|
const char* file, |
|
const int line, |
|
const char* condition, |
|
const char* msg, |
|
const void* caller = nullptr); |
|
|
|
[[noreturn]] C10_API inline void ThrowEnforceFiniteNotMet( |
|
const char* file, |
|
const int line, |
|
const char* condition, |
|
detail::CompileTimeEmptyString , |
|
const void* caller = nullptr) { |
|
ThrowEnforceFiniteNotMet(file, line, condition, "", caller); |
|
} |
|
|
|
constexpr bool IsUsingGoogleLogging() { |
|
#ifdef C10_USE_GLOG |
|
return true; |
|
#else |
|
return false; |
|
#endif |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C10_API void ShowLogInfoToStderr(); |
|
|
|
C10_API void SetStackTraceFetcher(std::function<string(void)> fetcher); |
|
|
|
using EnforceNotMet = ::c10::Error; |
|
|
|
#define CAFFE_ENFORCE(condition, ...) \ |
|
do { \ |
|
if (C10_UNLIKELY(!(condition))) { \ |
|
::c10::ThrowEnforceNotMet( \ |
|
__FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__)); \ |
|
} \ |
|
} while (false) |
|
|
|
#define CAFFE_ENFORCE_FINITE(condition, ...) \ |
|
do { \ |
|
if (C10_UNLIKELY(!(condition))) { \ |
|
::c10::ThrowEnforceFiniteNotMet( \ |
|
__FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__)); \ |
|
} \ |
|
} while (false) |
|
|
|
#define CAFFE_ENFORCE_WITH_CALLER(condition, ...) \ |
|
do { \ |
|
if (C10_UNLIKELY(!(condition))) { \ |
|
::c10::ThrowEnforceNotMet( \ |
|
__FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__), this); \ |
|
} \ |
|
} while (false) |
|
|
|
#define CAFFE_THROW(...) \ |
|
::c10::ThrowEnforceNotMet(__FILE__, __LINE__, "", ::c10::str(__VA_ARGS__)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace enforce_detail { |
|
|
|
template <typename T1, typename T2> |
|
std::string enforceFailMsgImpl(const T1& x, const T2& y) { |
|
return c10::str(x, " vs ", y); |
|
} |
|
|
|
template <typename T1, typename T2, typename... Args> |
|
std::string enforceFailMsgImpl(const T1& x, const T2& y, const Args&... args) { |
|
return c10::str(x, " vs ", y, ". ", args...); |
|
} |
|
|
|
template <typename Pred, typename T1, typename T2, typename... Args> |
|
void enforceThatImpl( |
|
Pred p, |
|
const T1& lhs, |
|
const T2& rhs, |
|
const char* file, |
|
int line, |
|
const char* expr, |
|
const void* caller, |
|
const Args&... args) { |
|
if (C10_UNLIKELY(!(p(lhs, rhs)))) { |
|
::c10::ThrowEnforceNotMet( |
|
file, |
|
line, |
|
expr, |
|
::c10::enforce_detail::enforceFailMsgImpl(lhs, rhs, args...), |
|
caller); |
|
} |
|
} |
|
#define CAFFE_ENFORCE_THAT_IMPL(op, lhs, rhs, expr, ...) \ |
|
::c10::enforce_detail::enforceThatImpl( \ |
|
op, lhs, rhs, __FILE__, __LINE__, expr, nullptr, ##__VA_ARGS__) |
|
|
|
#define CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(op, lhs, rhs, expr, ...) \ |
|
::c10::enforce_detail::enforceThatImpl( \ |
|
op, (lhs), (rhs), __FILE__, __LINE__, expr, this, ##__VA_ARGS__) |
|
|
|
} |
|
|
|
#define CAFFE_ENFORCE_THAT(cmp, op, lhs, rhs, ...) \ |
|
CAFFE_ENFORCE_THAT_IMPL(cmp, lhs, rhs, #lhs " " #op " " #rhs, ##__VA_ARGS__) |
|
|
|
#define CAFFE_ENFORCE_BINARY_OP(cmp, op, x, y, ...) \ |
|
CAFFE_ENFORCE_THAT_IMPL(cmp, x, y, #x " " #op " " #y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_EQ(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP(std::equal_to<void>(), ==, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_NE(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP(std::not_equal_to<void>(), !=, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_LE(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP(std::less_equal<void>(), <=, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_LT(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP(std::less<void>(), <, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_GE(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP(std::greater_equal<void>(), >=, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_GT(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP(std::greater<void>(), >, x, y, ##__VA_ARGS__) |
|
|
|
#define CAFFE_ENFORCE_BINARY_OP_WITH_CALLER(cmp, op, x, y, ...) \ |
|
CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER( \ |
|
cmp, x, y, #x " " #op " " #y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_EQ_WITH_CALLER(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \ |
|
std::equal_to<void>(), ==, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_NE_WITH_CALLER(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \ |
|
std::not_equal_to<void>(), !=, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_LE_WITH_CALLER(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \ |
|
std::less_equal<void>(), <=, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_LT_WITH_CALLER(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP_WITH_CALLER(std::less<void>(), <, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_GE_WITH_CALLER(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \ |
|
std::greater_equal<void>(), >=, x, y, ##__VA_ARGS__) |
|
#define CAFFE_ENFORCE_GT_WITH_CALLER(x, y, ...) \ |
|
CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \ |
|
std::greater<void>(), >, x, y, ##__VA_ARGS__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define C10_LOG_API_USAGE_ONCE(...) \ |
|
C10_UNUSED static bool C10_ANONYMOUS_VARIABLE(logFlag) = \ |
|
::c10::detail::LogAPIUsageFakeReturn(__VA_ARGS__); |
|
|
|
|
|
C10_API void SetAPIUsageLogger(std::function<void(const std::string&)> logger); |
|
C10_API void LogAPIUsage(const std::string& context); |
|
|
|
|
|
|
|
|
|
|
|
|
|
struct DDPLoggingData { |
|
|
|
std::map<std::string, std::string> strs_map; |
|
|
|
std::map<std::string, int64_t> ints_map; |
|
}; |
|
|
|
C10_API void SetPyTorchDDPUsageLogger( |
|
std::function<void(const DDPLoggingData&)> logger); |
|
C10_API void LogPyTorchDDPUsage(const DDPLoggingData& ddpData); |
|
|
|
namespace detail { |
|
|
|
C10_API bool LogAPIUsageFakeReturn(const std::string& context); |
|
} |
|
|
|
|
|
C10_API void initLogging(); |
|
|
|
} |
|
|
|
#endif |
|
|