Logging.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #ifndef C10_UTIL_LOGGING_H_
  2. #define C10_UTIL_LOGGING_H_
  3. #include <climits>
  4. #include <exception>
  5. #include <functional>
  6. #include <limits>
  7. #include <sstream>
  8. #include <c10/macros/Macros.h>
  9. #include <c10/util/Backtrace.h>
  10. #include <c10/util/Exception.h>
  11. #include <c10/util/Flags.h>
  12. #include <c10/util/StringUtil.h>
  13. // CAFFE2_LOG_THRESHOLD is a compile time flag that would allow us to turn off
  14. // logging at compile time so no logging message below that level is produced
  15. // at all. The value should be between INT_MIN and CAFFE_FATAL.
  16. #ifndef CAFFE2_LOG_THRESHOLD
  17. // If we have not defined the compile time log threshold, we keep all the
  18. // log cases.
  19. #define CAFFE2_LOG_THRESHOLD INT_MIN
  20. #endif // CAFFE2_LOG_THRESHOLD
  21. // Below are different implementations for glog and non-glog cases.
  22. #ifdef C10_USE_GLOG
  23. #include <c10/util/logging_is_google_glog.h>
  24. #else // !C10_USE_GLOG
  25. #include <c10/util/logging_is_not_google_glog.h>
  26. #endif // C10_USE_GLOG
  27. C10_DECLARE_int(caffe2_log_level);
  28. C10_DECLARE_bool(caffe2_use_fatal_for_enforce);
  29. // Some versions of GLOG support less-spammy version of LOG_EVERY_MS. If it's
  30. // not available - just short-circuit to the always working one one.
  31. // We define the C10_ name to avoid confusing other files
  32. #ifdef LOG_EVERY_MS
  33. #define C10_LOG_EVERY_MS(severity, ms) LOG_EVERY_MS(severity, ms)
  34. #else
  35. #define C10_LOG_EVERY_MS(severity, ms) LOG(severity)
  36. #endif
  37. // Same for LOG_FIRST_N
  38. #ifdef LOG_FIRST_N
  39. #define C10_LOG_FIRST_N(severity, n) LOG_FIRST_N(severity, n)
  40. #else
  41. #define C10_LOG_FIRST_N(severity, n) LOG(severity)
  42. #endif
  43. // Same for LOG_EVERY_N
  44. #ifdef LOG_EVERY_N
  45. #define C10_LOG_EVERY_N(severity, n) LOG_EVERY_N(severity, n)
  46. #else
  47. #define C10_LOG_EVERY_N(severity, n) LOG(severity)
  48. #endif
  49. namespace c10 {
  50. using std::string;
  51. // Functions that we use for initialization.
  52. C10_API bool InitCaffeLogging(int* argc, char** argv);
  53. C10_API void UpdateLoggingLevelsFromFlags();
  54. [[noreturn]] C10_API void ThrowEnforceNotMet(
  55. const char* file,
  56. const int line,
  57. const char* condition,
  58. const std::string& msg,
  59. const void* caller = nullptr);
  60. [[noreturn]] C10_API void ThrowEnforceNotMet(
  61. const char* file,
  62. const int line,
  63. const char* condition,
  64. const char* msg,
  65. const void* caller = nullptr);
  66. [[noreturn]] C10_API inline void ThrowEnforceNotMet(
  67. const char* file,
  68. const int line,
  69. const char* condition,
  70. detail::CompileTimeEmptyString /*msg*/,
  71. const void* caller = nullptr) {
  72. ThrowEnforceNotMet(file, line, condition, "", caller);
  73. }
  74. [[noreturn]] C10_API void ThrowEnforceFiniteNotMet(
  75. const char* file,
  76. const int line,
  77. const char* condition,
  78. const std::string& msg,
  79. const void* caller = nullptr);
  80. [[noreturn]] C10_API void ThrowEnforceFiniteNotMet(
  81. const char* file,
  82. const int line,
  83. const char* condition,
  84. const char* msg,
  85. const void* caller = nullptr);
  86. [[noreturn]] C10_API inline void ThrowEnforceFiniteNotMet(
  87. const char* file,
  88. const int line,
  89. const char* condition,
  90. detail::CompileTimeEmptyString /*msg*/,
  91. const void* caller = nullptr) {
  92. ThrowEnforceFiniteNotMet(file, line, condition, "", caller);
  93. }
  94. constexpr bool IsUsingGoogleLogging() {
  95. #ifdef C10_USE_GLOG
  96. return true;
  97. #else
  98. return false;
  99. #endif
  100. }
  101. /**
  102. * A utility to allow one to show log info to stderr after the program starts.
  103. *
  104. * This is similar to calling GLOG's --logtostderr, or setting caffe2_log_level
  105. * to smaller than INFO. You are recommended to only use this in a few sparse
  106. * cases, such as when you want to write a tutorial or something. Normally, use
  107. * the commandline flags to set the log level.
  108. */
  109. C10_API void ShowLogInfoToStderr();
  110. C10_API void SetStackTraceFetcher(std::function<::c10::Backtrace()> fetcher);
  111. /**
  112. * Convenience function for non-lazy stack trace fetchers. The Backtrace
  113. * overload should be preferred when stringifying the backtrace is expensive.
  114. */
  115. C10_API void SetStackTraceFetcher(std::function<std::string()> fetcher);
  116. using EnforceNotMet = ::c10::Error;
  117. #define CAFFE_ENFORCE(condition, ...) \
  118. do { \
  119. if (C10_UNLIKELY(!(condition))) { \
  120. ::c10::ThrowEnforceNotMet( \
  121. __FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__)); \
  122. } \
  123. } while (false)
  124. #define CAFFE_ENFORCE_FINITE(condition, ...) \
  125. do { \
  126. if (C10_UNLIKELY(!(condition))) { \
  127. ::c10::ThrowEnforceFiniteNotMet( \
  128. __FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__)); \
  129. } \
  130. } while (false)
  131. #define CAFFE_ENFORCE_WITH_CALLER(condition, ...) \
  132. do { \
  133. if (C10_UNLIKELY(!(condition))) { \
  134. ::c10::ThrowEnforceNotMet( \
  135. __FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__), this); \
  136. } \
  137. } while (false)
  138. #define CAFFE_THROW(...) \
  139. ::c10::ThrowEnforceNotMet(__FILE__, __LINE__, "", ::c10::str(__VA_ARGS__))
  140. /**
  141. * Rich logging messages
  142. *
  143. * CAFFE_ENFORCE_THAT can be used with one of the "checker functions" that
  144. * capture input argument values and add it to the exception message. E.g.
  145. * `CAFFE_ENFORCE_THAT(Equals(foo(x), bar(y)), "Optional additional message")`
  146. * would evaluate both foo and bar only once and if the results are not equal -
  147. * include them in the exception message.
  148. *
  149. * Some of the basic checker functions like Equals or Greater are already
  150. * defined below. Other header might define customized checkers by adding
  151. * functions to caffe2::enforce_detail namespace. For example:
  152. *
  153. * namespace caffe2 { namespace enforce_detail {
  154. * inline EnforceFailMessage IsVector(const vector<int64_t>& shape) {
  155. * if (shape.size() == 1) { return EnforceOK(); }
  156. * return c10::str("Shape ", shape, " is not a vector");
  157. * }
  158. * }}
  159. *
  160. * With further usages like `CAFFE_ENFORCE_THAT(IsVector(Input(0).dims()))`
  161. *
  162. * Convenient wrappers for binary operations like CAFFE_ENFORCE_EQ are provided
  163. * too. Please use them instead of TORCH_CHECK_EQ and friends for failures in
  164. * user-provided input.
  165. */
  166. namespace enforce_detail {
  167. template <typename T1, typename T2>
  168. std::string enforceFailMsgImpl(const T1& x, const T2& y) {
  169. return c10::str(x, " vs ", y);
  170. }
  171. template <typename T1, typename T2, typename... Args>
  172. std::string enforceFailMsgImpl(const T1& x, const T2& y, const Args&... args) {
  173. return c10::str(x, " vs ", y, ". ", args...);
  174. }
  175. template <typename Pred, typename T1, typename T2, typename GetFailMsgFunc>
  176. void enforceThatImpl(
  177. Pred p,
  178. const T1& lhs,
  179. const T2& rhs,
  180. const char* file,
  181. int line,
  182. const char* expr,
  183. const void* caller,
  184. GetFailMsgFunc getFailMsg) {
  185. if (C10_UNLIKELY(!(p(lhs, rhs)))) {
  186. ::c10::ThrowEnforceNotMet(file, line, expr, getFailMsg(lhs, rhs), caller);
  187. }
  188. }
  189. #define CAFFE_ENFORCE_THAT_IMPL(op, lhs, rhs, expr, ...) \
  190. ::c10::enforce_detail::enforceThatImpl( \
  191. op, \
  192. (lhs), \
  193. (rhs), \
  194. __FILE__, \
  195. __LINE__, \
  196. expr, \
  197. nullptr, \
  198. [&](const auto& arg1, const auto& arg2) { \
  199. return ::c10::enforce_detail::enforceFailMsgImpl( \
  200. arg1, arg2, ##__VA_ARGS__); \
  201. })
  202. #define CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(op, lhs, rhs, expr, ...) \
  203. ::c10::enforce_detail::enforceThatImpl( \
  204. op, \
  205. (lhs), \
  206. (rhs), \
  207. __FILE__, \
  208. __LINE__, \
  209. expr, \
  210. this, \
  211. [&](const auto& arg1, const auto& arg2) { \
  212. return ::c10::enforce_detail::enforceFailMsgImpl( \
  213. arg1, arg2, ##__VA_ARGS__); \
  214. })
  215. } // namespace enforce_detail
  216. #define CAFFE_ENFORCE_THAT(cmp, op, lhs, rhs, ...) \
  217. CAFFE_ENFORCE_THAT_IMPL(cmp, lhs, rhs, #lhs " " #op " " #rhs, ##__VA_ARGS__)
  218. #define CAFFE_ENFORCE_BINARY_OP(cmp, op, x, y, ...) \
  219. CAFFE_ENFORCE_THAT_IMPL(cmp, x, y, #x " " #op " " #y, ##__VA_ARGS__)
  220. #define CAFFE_ENFORCE_EQ(x, y, ...) \
  221. CAFFE_ENFORCE_BINARY_OP(std::equal_to<void>(), ==, x, y, ##__VA_ARGS__)
  222. #define CAFFE_ENFORCE_NE(x, y, ...) \
  223. CAFFE_ENFORCE_BINARY_OP(std::not_equal_to<void>(), !=, x, y, ##__VA_ARGS__)
  224. #define CAFFE_ENFORCE_LE(x, y, ...) \
  225. CAFFE_ENFORCE_BINARY_OP(std::less_equal<void>(), <=, x, y, ##__VA_ARGS__)
  226. #define CAFFE_ENFORCE_LT(x, y, ...) \
  227. CAFFE_ENFORCE_BINARY_OP(std::less<void>(), <, x, y, ##__VA_ARGS__)
  228. #define CAFFE_ENFORCE_GE(x, y, ...) \
  229. CAFFE_ENFORCE_BINARY_OP(std::greater_equal<void>(), >=, x, y, ##__VA_ARGS__)
  230. #define CAFFE_ENFORCE_GT(x, y, ...) \
  231. CAFFE_ENFORCE_BINARY_OP(std::greater<void>(), >, x, y, ##__VA_ARGS__)
  232. #define CAFFE_ENFORCE_BINARY_OP_WITH_CALLER(cmp, op, x, y, ...) \
  233. CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER( \
  234. cmp, x, y, #x " " #op " " #y, ##__VA_ARGS__)
  235. #define CAFFE_ENFORCE_EQ_WITH_CALLER(x, y, ...) \
  236. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  237. std::equal_to<void>(), ==, x, y, ##__VA_ARGS__)
  238. #define CAFFE_ENFORCE_NE_WITH_CALLER(x, y, ...) \
  239. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  240. std::not_equal_to<void>(), !=, x, y, ##__VA_ARGS__)
  241. #define CAFFE_ENFORCE_LE_WITH_CALLER(x, y, ...) \
  242. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  243. std::less_equal<void>(), <=, x, y, ##__VA_ARGS__)
  244. #define CAFFE_ENFORCE_LT_WITH_CALLER(x, y, ...) \
  245. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER(std::less<void>(), <, x, y, ##__VA_ARGS__)
  246. #define CAFFE_ENFORCE_GE_WITH_CALLER(x, y, ...) \
  247. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  248. std::greater_equal<void>(), >=, x, y, ##__VA_ARGS__)
  249. #define CAFFE_ENFORCE_GT_WITH_CALLER(x, y, ...) \
  250. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  251. std::greater<void>(), >, x, y, ##__VA_ARGS__)
  252. /**
  253. * Very lightweight logging for the first time API usage. It's beneficial for
  254. * tracking of individual functionality usage in larger applications.
  255. *
  256. * In order to ensure light-weightedness of logging, we utilize static variable
  257. * trick - LogAPIUsage will be invoked only once and further invocations will
  258. * just do an atomic check.
  259. *
  260. * Example:
  261. * // Logs caller info with an arbitrary text event, if there is a usage.
  262. * C10_LOG_API_USAGE_ONCE("my_api");
  263. */
  264. #define C10_LOG_API_USAGE_ONCE(...) \
  265. C10_UNUSED static bool C10_ANONYMOUS_VARIABLE(logFlag) = \
  266. ::c10::detail::LogAPIUsageFakeReturn(__VA_ARGS__);
  267. // API usage logging capabilities
  268. C10_API void SetAPIUsageLogger(std::function<void(const std::string&)> logger);
  269. C10_API void LogAPIUsage(const std::string& context);
  270. C10_API void SetAPIUsageMetadataLogger(
  271. std::function<void(
  272. const std::string&,
  273. const std::map<std::string, std::string>& metadata_map)> logger);
  274. C10_API void LogAPIUsageMetadata(
  275. const std::string& context,
  276. const std::map<std::string, std::string>& metadata_map);
  277. // PyTorch ddp usage logging capabilities
  278. // DDPLoggingData holds data that can be logged in applications
  279. // for analysis and debugging. Data structure is defined in
  280. // c10 directory so that it can be easily imported by both c10
  281. // and torch files.
  282. struct DDPLoggingData {
  283. // logging fields that are string types.
  284. std::map<std::string, std::string> strs_map;
  285. // logging fields that are int64_t types.
  286. std::map<std::string, int64_t> ints_map;
  287. };
  288. C10_API void SetPyTorchDDPUsageLogger(
  289. std::function<void(const DDPLoggingData&)> logger);
  290. C10_API void LogPyTorchDDPUsage(const DDPLoggingData& ddpData);
  291. namespace detail {
  292. // Return value is needed to do the static variable initialization trick
  293. C10_API bool LogAPIUsageFakeReturn(const std::string& context);
  294. } // namespace detail
  295. // Initializes the c10 logger.
  296. C10_API void initLogging();
  297. // Sets the rank, which will be included in log messages
  298. C10_API void SetGlobalRank(int64_t rank);
  299. } // namespace c10
  300. #endif // C10_UTIL_LOGGING_H_