log.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**
  2. * Copyright (c) 2022 Xiaomi Corporation (authors: Fangjun Kuang)
  3. *
  4. * See LICENSE for clarification regarding multiple authors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. // The content in this file is copied/modified from
  19. // https://github.com/k2-fsa/k2/blob/master/k2/csrc/log.h
  20. #ifndef KALDI_NATIVE_FBANK_CSRC_LOG_H_
  21. #define KALDI_NATIVE_FBANK_CSRC_LOG_H_
  22. #include <stdio.h>
  23. #include <mutex> // NOLINT
  24. #include <sstream>
  25. #include <string>
  26. namespace knf {
  27. #if KNF_ENABLE_CHECK
  28. #if defined(NDEBUG)
  29. constexpr bool kDisableDebug = true;
  30. #else
  31. constexpr bool kDisableDebug = false;
  32. #endif
  33. enum class LogLevel {
  34. kTrace = 0,
  35. kDebug = 1,
  36. kInfo = 2,
  37. kWarning = 3,
  38. kError = 4,
  39. kFatal = 5, // print message and abort the program
  40. };
  41. // They are used in KNF_LOG(xxx), so their names
  42. // do not follow the google c++ code style
  43. //
  44. // You can use them in the following way:
  45. //
  46. // KNF_LOG(TRACE) << "some message";
  47. // KNF_LOG(DEBUG) << "some message";
  48. #ifndef _MSC_VER
  49. constexpr LogLevel TRACE = LogLevel::kTrace;
  50. constexpr LogLevel DEBUG = LogLevel::kDebug;
  51. constexpr LogLevel INFO = LogLevel::kInfo;
  52. constexpr LogLevel WARNING = LogLevel::kWarning;
  53. constexpr LogLevel ERROR = LogLevel::kError;
  54. constexpr LogLevel FATAL = LogLevel::kFatal;
  55. #else
  56. #define TRACE LogLevel::kTrace
  57. #define DEBUG LogLevel::kDebug
  58. #define INFO LogLevel::kInfo
  59. #define WARNING LogLevel::kWarning
  60. #define ERROR LogLevel::kError
  61. #define FATAL LogLevel::kFatal
  62. #endif
  63. std::string GetStackTrace();
  64. /* Return the current log level.
  65. If the current log level is TRACE, then all logged messages are printed out.
  66. If the current log level is DEBUG, log messages with "TRACE" level are not
  67. shown and all other levels are printed out.
  68. Similarly, if the current log level is INFO, log message with "TRACE" and
  69. "DEBUG" are not shown and all other levels are printed out.
  70. If it is FATAL, then only FATAL messages are shown.
  71. */
  72. inline LogLevel GetCurrentLogLevel() {
  73. static LogLevel log_level = INFO;
  74. static std::once_flag init_flag;
  75. std::call_once(init_flag, []() {
  76. const char *env_log_level = std::getenv("KNF_LOG_LEVEL");
  77. if (env_log_level == nullptr) return;
  78. std::string s = env_log_level;
  79. if (s == "TRACE")
  80. log_level = TRACE;
  81. else if (s == "DEBUG")
  82. log_level = DEBUG;
  83. else if (s == "INFO")
  84. log_level = INFO;
  85. else if (s == "WARNING")
  86. log_level = WARNING;
  87. else if (s == "ERROR")
  88. log_level = ERROR;
  89. else if (s == "FATAL")
  90. log_level = FATAL;
  91. else
  92. fprintf(stderr,
  93. "Unknown KNF_LOG_LEVEL: %s"
  94. "\nSupported values are: "
  95. "TRACE, DEBUG, INFO, WARNING, ERROR, FATAL",
  96. s.c_str());
  97. });
  98. return log_level;
  99. }
  100. inline bool EnableAbort() {
  101. static std::once_flag init_flag;
  102. static bool enable_abort = false;
  103. std::call_once(init_flag, []() {
  104. enable_abort = (std::getenv("KNF_ABORT") != nullptr);
  105. });
  106. return enable_abort;
  107. }
  108. class Logger {
  109. public:
  110. Logger(const char *filename, const char *func_name, uint32_t line_num,
  111. LogLevel level)
  112. : filename_(filename),
  113. func_name_(func_name),
  114. line_num_(line_num),
  115. level_(level) {
  116. cur_level_ = GetCurrentLogLevel();
  117. fprintf(stderr, "here\n");
  118. switch (level) {
  119. case TRACE:
  120. if (cur_level_ <= TRACE) fprintf(stderr, "[T] ");
  121. break;
  122. case DEBUG:
  123. if (cur_level_ <= DEBUG) fprintf(stderr, "[D] ");
  124. break;
  125. case INFO:
  126. if (cur_level_ <= INFO) fprintf(stderr, "[I] ");
  127. break;
  128. case WARNING:
  129. if (cur_level_ <= WARNING) fprintf(stderr, "[W] ");
  130. break;
  131. case ERROR:
  132. if (cur_level_ <= ERROR) fprintf(stderr, "[E] ");
  133. break;
  134. case FATAL:
  135. if (cur_level_ <= FATAL) fprintf(stderr, "[F] ");
  136. break;
  137. }
  138. if (cur_level_ <= level_) {
  139. fprintf(stderr, "%s:%u:%s ", filename, line_num, func_name);
  140. }
  141. }
  142. ~Logger() noexcept(false) {
  143. static constexpr const char *kErrMsg = R"(
  144. Some bad things happened. Please read the above error messages and stack
  145. trace. If you are using Python, the following command may be helpful:
  146. gdb --args python /path/to/your/code.py
  147. (You can use `gdb` to debug the code. Please consider compiling
  148. a debug version of KNF.).
  149. If you are unable to fix it, please open an issue at:
  150. https://github.com/csukuangfj/kaldi-native-fbank/issues/new
  151. )";
  152. fprintf(stderr, "\n");
  153. if (level_ == FATAL) {
  154. std::string stack_trace = GetStackTrace();
  155. if (!stack_trace.empty()) {
  156. fprintf(stderr, "\n\n%s\n", stack_trace.c_str());
  157. }
  158. fflush(nullptr);
  159. #ifndef __ANDROID_API__
  160. if (EnableAbort()) {
  161. // NOTE: abort() will terminate the program immediately without
  162. // printing the Python stack backtrace.
  163. abort();
  164. }
  165. throw std::runtime_error(kErrMsg);
  166. #else
  167. abort();
  168. #endif
  169. }
  170. }
  171. const Logger &operator<<(bool b) const {
  172. if (cur_level_ <= level_) {
  173. fprintf(stderr, b ? "true" : "false");
  174. }
  175. return *this;
  176. }
  177. const Logger &operator<<(int8_t i) const {
  178. if (cur_level_ <= level_) fprintf(stderr, "%d", i);
  179. return *this;
  180. }
  181. const Logger &operator<<(const char *s) const {
  182. if (cur_level_ <= level_) fprintf(stderr, "%s", s);
  183. return *this;
  184. }
  185. const Logger &operator<<(int32_t i) const {
  186. if (cur_level_ <= level_) fprintf(stderr, "%d", i);
  187. return *this;
  188. }
  189. const Logger &operator<<(uint32_t i) const {
  190. if (cur_level_ <= level_) fprintf(stderr, "%u", i);
  191. return *this;
  192. }
  193. const Logger &operator<<(uint64_t i) const {
  194. if (cur_level_ <= level_)
  195. fprintf(stderr, "%llu", (long long unsigned int)i); // NOLINT
  196. return *this;
  197. }
  198. const Logger &operator<<(int64_t i) const {
  199. if (cur_level_ <= level_)
  200. fprintf(stderr, "%lli", (long long int)i); // NOLINT
  201. return *this;
  202. }
  203. const Logger &operator<<(float f) const {
  204. if (cur_level_ <= level_) fprintf(stderr, "%f", f);
  205. return *this;
  206. }
  207. const Logger &operator<<(double d) const {
  208. if (cur_level_ <= level_) fprintf(stderr, "%f", d);
  209. return *this;
  210. }
  211. template <typename T>
  212. const Logger &operator<<(const T &t) const {
  213. // require T overloads operator<<
  214. std::ostringstream os;
  215. os << t;
  216. return *this << os.str().c_str();
  217. }
  218. // specialization to fix compile error: `stringstream << nullptr` is ambiguous
  219. const Logger &operator<<(const std::nullptr_t &null) const {
  220. if (cur_level_ <= level_) *this << "(null)";
  221. return *this;
  222. }
  223. private:
  224. const char *filename_;
  225. const char *func_name_;
  226. uint32_t line_num_;
  227. LogLevel level_;
  228. LogLevel cur_level_;
  229. };
  230. #endif // KNF_ENABLE_CHECK
  231. class Voidifier {
  232. public:
  233. #if KNF_ENABLE_CHECK
  234. void operator&(const Logger &) const {}
  235. #endif
  236. };
  237. #if !defined(KNF_ENABLE_CHECK)
  238. template <typename T>
  239. const Voidifier &operator<<(const Voidifier &v, T &&) {
  240. return v;
  241. }
  242. #endif
  243. } // namespace knf
  244. #define KNF_STATIC_ASSERT(x) static_assert(x, "")
  245. #ifdef KNF_ENABLE_CHECK
  246. #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) || \
  247. defined(__PRETTY_FUNCTION__)
  248. // for clang and GCC
  249. #define KNF_FUNC __PRETTY_FUNCTION__
  250. #else
  251. // for other compilers
  252. #define KNF_FUNC __func__
  253. #endif
  254. #define KNF_CHECK(x) \
  255. (x) ? (void)0 \
  256. : ::knf::Voidifier() & \
  257. ::knf::Logger(__FILE__, KNF_FUNC, __LINE__, ::knf::FATAL) \
  258. << "Check failed: " << #x << " "
  259. // WARNING: x and y may be evaluated multiple times, but this happens only
  260. // when the check fails. Since the program aborts if it fails, we don't think
  261. // the extra evaluation of x and y matters.
  262. //
  263. // CAUTION: we recommend the following use case:
  264. //
  265. // auto x = Foo();
  266. // auto y = Bar();
  267. // KNF_CHECK_EQ(x, y) << "Some message";
  268. //
  269. // And please avoid
  270. //
  271. // KNF_CHECK_EQ(Foo(), Bar());
  272. //
  273. // if `Foo()` or `Bar()` causes some side effects, e.g., changing some
  274. // local static variables or global variables.
  275. #define _KNF_CHECK_OP(x, y, op) \
  276. ((x)op(y)) ? (void)0 \
  277. : ::knf::Voidifier() & \
  278. ::knf::Logger(__FILE__, KNF_FUNC, __LINE__, ::knf::FATAL) \
  279. << "Check failed: " << #x << " " << #op << " " << #y \
  280. << " (" << (x) << " vs. " << (y) << ") "
  281. #define KNF_CHECK_EQ(x, y) _KNF_CHECK_OP(x, y, ==)
  282. #define KNF_CHECK_NE(x, y) _KNF_CHECK_OP(x, y, !=)
  283. #define KNF_CHECK_LT(x, y) _KNF_CHECK_OP(x, y, <)
  284. #define KNF_CHECK_LE(x, y) _KNF_CHECK_OP(x, y, <=)
  285. #define KNF_CHECK_GT(x, y) _KNF_CHECK_OP(x, y, >)
  286. #define KNF_CHECK_GE(x, y) _KNF_CHECK_OP(x, y, >=)
  287. #define KNF_LOG(x) ::knf::Logger(__FILE__, KNF_FUNC, __LINE__, ::knf::x)
  288. // ------------------------------------------------------------
  289. // For debug check
  290. // ------------------------------------------------------------
  291. // If you define the macro "-D NDEBUG" while compiling kaldi-native-fbank,
  292. // the following macros are in fact empty and does nothing.
  293. #define KNF_DCHECK(x) ::knf::kDisableDebug ? (void)0 : KNF_CHECK(x)
  294. #define KNF_DCHECK_EQ(x, y) ::knf::kDisableDebug ? (void)0 : KNF_CHECK_EQ(x, y)
  295. #define KNF_DCHECK_NE(x, y) ::knf::kDisableDebug ? (void)0 : KNF_CHECK_NE(x, y)
  296. #define KNF_DCHECK_LT(x, y) ::knf::kDisableDebug ? (void)0 : KNF_CHECK_LT(x, y)
  297. #define KNF_DCHECK_LE(x, y) ::knf::kDisableDebug ? (void)0 : KNF_CHECK_LE(x, y)
  298. #define KNF_DCHECK_GT(x, y) ::knf::kDisableDebug ? (void)0 : KNF_CHECK_GT(x, y)
  299. #define KNF_DCHECK_GE(x, y) ::knf::kDisableDebug ? (void)0 : KNF_CHECK_GE(x, y)
  300. #define KNF_DLOG(x) \
  301. ::knf::kDisableDebug ? (void)0 : ::knf::Voidifier() & KNF_LOG(x)
  302. #else
  303. #define KNF_CHECK(x) ::knf::Voidifier()
  304. #define KNF_LOG(x) ::knf::Voidifier()
  305. #define KNF_CHECK_EQ(x, y) ::knf::Voidifier()
  306. #define KNF_CHECK_NE(x, y) ::knf::Voidifier()
  307. #define KNF_CHECK_LT(x, y) ::knf::Voidifier()
  308. #define KNF_CHECK_LE(x, y) ::knf::Voidifier()
  309. #define KNF_CHECK_GT(x, y) ::knf::Voidifier()
  310. #define KNF_CHECK_GE(x, y) ::knf::Voidifier()
  311. #define KNF_DCHECK(x) ::knf::Voidifier()
  312. #define KNF_DLOG(x) ::knf::Voidifier()
  313. #define KNF_DCHECK_EQ(x, y) ::knf::Voidifier()
  314. #define KNF_DCHECK_NE(x, y) ::knf::Voidifier()
  315. #define KNF_DCHECK_LT(x, y) ::knf::Voidifier()
  316. #define KNF_DCHECK_LE(x, y) ::knf::Voidifier()
  317. #define KNF_DCHECK_GT(x, y) ::knf::Voidifier()
  318. #define KNF_DCHECK_GE(x, y) ::knf::Voidifier()
  319. #endif // KNF_CHECK_NE
  320. #endif // KALDI_NATIVE_FBANK_CSRC_LOG_H_