Logging.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #if !defined(C10_NODEPRECATED)
  51. using std::string;
  52. #endif
  53. // Functions that we use for initialization.
  54. C10_API bool InitCaffeLogging(int* argc, char** argv);
  55. C10_API void UpdateLoggingLevelsFromFlags();
  56. [[noreturn]] C10_API void ThrowEnforceNotMet(
  57. const char* file,
  58. const int line,
  59. const char* condition,
  60. const std::string& msg,
  61. const void* caller = nullptr);
  62. [[noreturn]] C10_API void ThrowEnforceNotMet(
  63. const char* file,
  64. const int line,
  65. const char* condition,
  66. const char* msg,
  67. const void* caller = nullptr);
  68. [[noreturn]] inline void ThrowEnforceNotMet(
  69. const char* file,
  70. const int line,
  71. const char* condition,
  72. detail::CompileTimeEmptyString /*msg*/,
  73. const void* caller = nullptr) {
  74. ThrowEnforceNotMet(file, line, condition, "", caller);
  75. }
  76. [[noreturn]] C10_API void ThrowEnforceFiniteNotMet(
  77. const char* file,
  78. const int line,
  79. const char* condition,
  80. const std::string& msg,
  81. const void* caller = nullptr);
  82. [[noreturn]] C10_API void ThrowEnforceFiniteNotMet(
  83. const char* file,
  84. const int line,
  85. const char* condition,
  86. const char* msg,
  87. const void* caller = nullptr);
  88. [[noreturn]] inline void ThrowEnforceFiniteNotMet(
  89. const char* file,
  90. const int line,
  91. const char* condition,
  92. detail::CompileTimeEmptyString /*msg*/,
  93. const void* caller = nullptr) {
  94. ThrowEnforceFiniteNotMet(file, line, condition, "", caller);
  95. }
  96. constexpr bool IsUsingGoogleLogging() {
  97. #ifdef C10_USE_GLOG
  98. return true;
  99. #else
  100. return false;
  101. #endif
  102. }
  103. /**
  104. * A utility to allow one to show log info to stderr after the program starts.
  105. *
  106. * This is similar to calling GLOG's --logtostderr, or setting caffe2_log_level
  107. * to smaller than INFO. You are recommended to only use this in a few sparse
  108. * cases, such as when you want to write a tutorial or something. Normally, use
  109. * the commandline flags to set the log level.
  110. */
  111. C10_API void ShowLogInfoToStderr();
  112. C10_API void SetStackTraceFetcher(std::function<::c10::Backtrace()> fetcher);
  113. /**
  114. * Convenience function for non-lazy stack trace fetchers. The Backtrace
  115. * overload should be preferred when stringifying the backtrace is expensive.
  116. */
  117. C10_API void SetStackTraceFetcher(std::function<std::string()> fetcher);
  118. using EnforceNotMet = ::c10::Error;
  119. #define CAFFE_ENFORCE(condition, ...) \
  120. do { \
  121. if (C10_UNLIKELY(!(condition))) { \
  122. ::c10::ThrowEnforceNotMet( \
  123. __FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__)); \
  124. } \
  125. } while (false)
  126. #define CAFFE_ENFORCE_FINITE(condition, ...) \
  127. do { \
  128. if (C10_UNLIKELY(!(condition))) { \
  129. ::c10::ThrowEnforceFiniteNotMet( \
  130. __FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__)); \
  131. } \
  132. } while (false)
  133. #define CAFFE_ENFORCE_WITH_CALLER(condition, ...) \
  134. do { \
  135. if (C10_UNLIKELY(!(condition))) { \
  136. ::c10::ThrowEnforceNotMet( \
  137. __FILE__, __LINE__, #condition, ::c10::str(__VA_ARGS__), this); \
  138. } \
  139. } while (false)
  140. #define CAFFE_THROW(...) \
  141. ::c10::ThrowEnforceNotMet(__FILE__, __LINE__, "", ::c10::str(__VA_ARGS__))
  142. /**
  143. * Rich logging messages
  144. *
  145. * CAFFE_ENFORCE_THAT can be used with one of the "checker functions" that
  146. * capture input argument values and add it to the exception message. E.g.
  147. * `CAFFE_ENFORCE_THAT(Equals(foo(x), bar(y)), "Optional additional message")`
  148. * would evaluate both foo and bar only once and if the results are not equal -
  149. * include them in the exception message.
  150. *
  151. * Some of the basic checker functions like Equals or Greater are already
  152. * defined below. Other header might define customized checkers by adding
  153. * functions to caffe2::enforce_detail namespace. For example:
  154. *
  155. * namespace caffe2 { namespace enforce_detail {
  156. * inline EnforceFailMessage IsVector(const vector<int64_t>& shape) {
  157. * if (shape.size() == 1) { return EnforceOK(); }
  158. * return c10::str("Shape ", shape, " is not a vector");
  159. * }
  160. * }}
  161. *
  162. * With further usages like `CAFFE_ENFORCE_THAT(IsVector(Input(0).dims()))`
  163. *
  164. * Convenient wrappers for binary operations like CAFFE_ENFORCE_EQ are provided
  165. * too. Please use them instead of TORCH_CHECK_EQ and friends for failures in
  166. * user-provided input.
  167. */
  168. namespace enforce_detail {
  169. template <typename T1, typename T2>
  170. std::string enforceFailMsgImpl(const T1& x, const T2& y) {
  171. return c10::str(x, " vs ", y);
  172. }
  173. template <typename T1, typename T2, typename... Args>
  174. std::string enforceFailMsgImpl(const T1& x, const T2& y, const Args&... args) {
  175. return c10::str(x, " vs ", y, ". ", args...);
  176. }
  177. template <typename Pred, typename T1, typename T2, typename GetFailMsgFunc>
  178. void enforceThatImpl(
  179. Pred p,
  180. const T1& lhs,
  181. const T2& rhs,
  182. const char* file,
  183. int line,
  184. const char* expr,
  185. const void* caller,
  186. GetFailMsgFunc getFailMsg) {
  187. if (C10_UNLIKELY(!(p(lhs, rhs)))) {
  188. ::c10::ThrowEnforceNotMet(file, line, expr, getFailMsg(lhs, rhs), caller);
  189. }
  190. }
  191. #define CAFFE_ENFORCE_THAT_IMPL(op, lhs, rhs, expr, ...) \
  192. ::c10::enforce_detail::enforceThatImpl( \
  193. op, \
  194. (lhs), \
  195. (rhs), \
  196. __FILE__, \
  197. __LINE__, \
  198. expr, \
  199. nullptr, \
  200. [&](const auto& arg1, const auto& arg2) { \
  201. return ::c10::enforce_detail::enforceFailMsgImpl( \
  202. arg1, arg2, ##__VA_ARGS__); \
  203. })
  204. #define CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER(op, lhs, rhs, expr, ...) \
  205. ::c10::enforce_detail::enforceThatImpl( \
  206. op, \
  207. (lhs), \
  208. (rhs), \
  209. __FILE__, \
  210. __LINE__, \
  211. expr, \
  212. this, \
  213. [&](const auto& arg1, const auto& arg2) { \
  214. return ::c10::enforce_detail::enforceFailMsgImpl( \
  215. arg1, arg2, ##__VA_ARGS__); \
  216. })
  217. } // namespace enforce_detail
  218. #define CAFFE_ENFORCE_THAT(cmp, op, lhs, rhs, ...) \
  219. CAFFE_ENFORCE_THAT_IMPL(cmp, lhs, rhs, #lhs " " #op " " #rhs, ##__VA_ARGS__)
  220. #define CAFFE_ENFORCE_BINARY_OP(cmp, op, x, y, ...) \
  221. CAFFE_ENFORCE_THAT_IMPL(cmp, x, y, #x " " #op " " #y, ##__VA_ARGS__)
  222. #define CAFFE_ENFORCE_EQ(x, y, ...) \
  223. CAFFE_ENFORCE_BINARY_OP(std::equal_to<void>(), ==, x, y, ##__VA_ARGS__)
  224. #define CAFFE_ENFORCE_NE(x, y, ...) \
  225. CAFFE_ENFORCE_BINARY_OP(std::not_equal_to<void>(), !=, x, y, ##__VA_ARGS__)
  226. #define CAFFE_ENFORCE_LE(x, y, ...) \
  227. CAFFE_ENFORCE_BINARY_OP(std::less_equal<void>(), <=, x, y, ##__VA_ARGS__)
  228. #define CAFFE_ENFORCE_LT(x, y, ...) \
  229. CAFFE_ENFORCE_BINARY_OP(std::less<void>(), <, x, y, ##__VA_ARGS__)
  230. #define CAFFE_ENFORCE_GE(x, y, ...) \
  231. CAFFE_ENFORCE_BINARY_OP(std::greater_equal<void>(), >=, x, y, ##__VA_ARGS__)
  232. #define CAFFE_ENFORCE_GT(x, y, ...) \
  233. CAFFE_ENFORCE_BINARY_OP(std::greater<void>(), >, x, y, ##__VA_ARGS__)
  234. #define CAFFE_ENFORCE_BINARY_OP_WITH_CALLER(cmp, op, x, y, ...) \
  235. CAFFE_ENFORCE_THAT_IMPL_WITH_CALLER( \
  236. cmp, x, y, #x " " #op " " #y, ##__VA_ARGS__)
  237. #define CAFFE_ENFORCE_EQ_WITH_CALLER(x, y, ...) \
  238. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  239. std::equal_to<void>(), ==, x, y, ##__VA_ARGS__)
  240. #define CAFFE_ENFORCE_NE_WITH_CALLER(x, y, ...) \
  241. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  242. std::not_equal_to<void>(), !=, x, y, ##__VA_ARGS__)
  243. #define CAFFE_ENFORCE_LE_WITH_CALLER(x, y, ...) \
  244. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  245. std::less_equal<void>(), <=, x, y, ##__VA_ARGS__)
  246. #define CAFFE_ENFORCE_LT_WITH_CALLER(x, y, ...) \
  247. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER(std::less<void>(), <, x, y, ##__VA_ARGS__)
  248. #define CAFFE_ENFORCE_GE_WITH_CALLER(x, y, ...) \
  249. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  250. std::greater_equal<void>(), >=, x, y, ##__VA_ARGS__)
  251. #define CAFFE_ENFORCE_GT_WITH_CALLER(x, y, ...) \
  252. CAFFE_ENFORCE_BINARY_OP_WITH_CALLER( \
  253. std::greater<void>(), >, x, y, ##__VA_ARGS__)
  254. struct IValue;
  255. class C10_API EventSampledHandler {
  256. public:
  257. virtual void log(
  258. std::string_view model_id,
  259. const std::vector<c10::IValue>& args) = 0;
  260. virtual ~EventSampledHandler() = default;
  261. };
  262. #define C10_LOG_EVENT_SAMPLED(event, ...) \
  263. static const std::unique_ptr<::c10::EventSampledHandler>& \
  264. _##event##EventSampledHandler = ::c10::GetEventSampledHandler(#event); \
  265. if (_##event##EventSampledHandler) { \
  266. _##event##EventSampledHandler->log(__VA_ARGS__); \
  267. }
  268. // Must be called in the main thread before any other threads are spawned.
  269. C10_API void InitEventSampledHandlers(
  270. std::vector<
  271. std::pair<std::string_view, std::unique_ptr<EventSampledHandler>>>);
  272. C10_API const std::unique_ptr<EventSampledHandler>& GetEventSampledHandler(
  273. std::string_view);
  274. /**
  275. * Very lightweight logging for the first time API usage. It's beneficial for
  276. * tracking of individual functionality usage in larger applications.
  277. *
  278. * In order to ensure light-weightedness of logging, we utilize static variable
  279. * trick - LogAPIUsage will be invoked only once and further invocations will
  280. * just do an atomic check.
  281. *
  282. * Example:
  283. * // Logs caller info with an arbitrary text event, if there is a usage.
  284. * C10_LOG_API_USAGE_ONCE("my_api");
  285. */
  286. #define C10_LOG_API_USAGE_ONCE(...) \
  287. [[maybe_unused]] static bool C10_ANONYMOUS_VARIABLE(logFlag) = \
  288. ::c10::detail::LogAPIUsageFakeReturn(__VA_ARGS__);
  289. // API usage logging capabilities
  290. C10_API void SetAPIUsageLogger(std::function<void(const std::string&)> logger);
  291. C10_API void LogAPIUsage(const std::string& context);
  292. C10_API void SetAPIUsageMetadataLogger(
  293. std::function<void(
  294. const std::string&,
  295. const std::map<std::string, std::string>& metadata_map)> logger);
  296. C10_API void LogAPIUsageMetadata(
  297. const std::string& context,
  298. const std::map<std::string, std::string>& metadata_map);
  299. // PyTorch ddp usage logging capabilities
  300. // DDPLoggingData holds data that can be logged in applications
  301. // for analysis and debugging. Data structure is defined in
  302. // c10 directory so that it can be easily imported by both c10
  303. // and torch files.
  304. struct DDPLoggingData {
  305. // logging fields that are string types.
  306. std::map<std::string, std::string> strs_map;
  307. // logging fields that are int64_t types.
  308. std::map<std::string, int64_t> ints_map;
  309. };
  310. C10_API void SetPyTorchDDPUsageLogger(
  311. std::function<void(const DDPLoggingData&)> logger);
  312. C10_API void LogPyTorchDDPUsage(const DDPLoggingData& ddpData);
  313. namespace detail {
  314. // Return value is needed to do the static variable initialization trick
  315. C10_API bool LogAPIUsageFakeReturn(const std::string& context);
  316. } // namespace detail
  317. // Initializes the c10 logger.
  318. C10_API void initLogging();
  319. // Sets the rank, which will be included in log messages
  320. C10_API void SetGlobalRank(int64_t rank);
  321. } // namespace c10
  322. #endif // C10_UTIL_LOGGING_H_