logger.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #ifndef OPENCV_LOGGER_HPP
  5. #define OPENCV_LOGGER_HPP
  6. #include <iostream>
  7. #include <sstream>
  8. #include <limits.h> // INT_MAX
  9. #include "logger.defines.hpp"
  10. #include "logtag.hpp"
  11. namespace cv {
  12. namespace utils {
  13. namespace logging {
  14. //! @addtogroup core_logging
  15. //! @{
  16. /** Set global logging level
  17. @return previous logging level
  18. */
  19. CV_EXPORTS LogLevel setLogLevel(LogLevel logLevel);
  20. /** Get global logging level */
  21. CV_EXPORTS LogLevel getLogLevel();
  22. CV_EXPORTS void registerLogTag(cv::utils::logging::LogTag* plogtag);
  23. CV_EXPORTS void setLogTagLevel(const char* tag, cv::utils::logging::LogLevel level);
  24. CV_EXPORTS cv::utils::logging::LogLevel getLogTagLevel(const char* tag);
  25. namespace internal {
  26. /** Get global log tag */
  27. CV_EXPORTS cv::utils::logging::LogTag* getGlobalLogTag();
  28. /** Write log message */
  29. CV_EXPORTS void writeLogMessage(LogLevel logLevel, const char* message);
  30. /** Write log message */
  31. CV_EXPORTS void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message);
  32. } // namespace
  33. struct LogTagAuto
  34. : public LogTag
  35. {
  36. inline LogTagAuto(const char* _name, LogLevel _level)
  37. : LogTag(_name, _level)
  38. {
  39. registerLogTag(this);
  40. }
  41. };
  42. /**
  43. * \def CV_LOG_STRIP_LEVEL
  44. *
  45. * Define CV_LOG_STRIP_LEVEL=CV_LOG_LEVEL_[DEBUG|INFO|WARN|ERROR|FATAL|SILENT] to compile out anything at that and before that logging level
  46. */
  47. #ifndef CV_LOG_STRIP_LEVEL
  48. # if defined NDEBUG
  49. # define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_DEBUG
  50. # else
  51. # define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_VERBOSE
  52. # endif
  53. #endif
  54. #define CV_LOGTAG_PTR_CAST(expr) static_cast<const cv::utils::logging::LogTag*>(expr)
  55. // CV_LOGTAG_EXPAND_NAME is intended to be re-defined (undef and then define again)
  56. // to allows logging users to use a shorter name argument when calling
  57. // CV_LOG_WITH_TAG or its related macros such as CV_LOG_INFO.
  58. //
  59. // This macro is intended to modify the tag argument as a string (token), via
  60. // preprocessor token pasting or metaprogramming techniques. A typical usage
  61. // is to apply a prefix, such as
  62. // ...... #define CV_LOGTAG_EXPAND_NAME(tag) cv_logtag_##tag
  63. //
  64. // It is permitted to re-define to a hard-coded expression, ignoring the tag.
  65. // This would work identically like the CV_LOGTAG_FALLBACK macro.
  66. //
  67. // Important: When the logging macro is called with tag being NULL, a user-defined
  68. // CV_LOGTAG_EXPAND_NAME may expand it into cv_logtag_0, cv_logtag_NULL, or
  69. // cv_logtag_nullptr. Use with care. Also be mindful of C++ symbol redefinitions.
  70. //
  71. // If there is significant amount of logging code with tag being NULL, it is
  72. // recommended to use (re-define) CV_LOGTAG_FALLBACK to inject locally a default
  73. // tag at the beginning of a compilation unit, to minimize lines of code changes.
  74. //
  75. #define CV_LOGTAG_EXPAND_NAME(tag) tag
  76. // CV_LOGTAG_FALLBACK is intended to be re-defined (undef and then define again)
  77. // by any other compilation units to provide a log tag when the logging statement
  78. // does not specify one. The macro needs to expand into a C++ expression that can
  79. // be static_cast into (cv::utils::logging::LogTag*). Null (nullptr) is permitted.
  80. #define CV_LOGTAG_FALLBACK nullptr
  81. // CV_LOGTAG_GLOBAL is the tag used when a log tag is not specified in the logging
  82. // statement nor the compilation unit. The macro needs to expand into a C++
  83. // expression that can be static_cast into (cv::utils::logging::LogTag*). Must be
  84. // non-null. Do not re-define.
  85. #define CV_LOGTAG_GLOBAL cv::utils::logging::internal::getGlobalLogTag()
  86. #define CV_LOG_WITH_TAG(tag, msgLevel, extra_check0, extra_check1, ...) \
  87. for(;;) { \
  88. extra_check0; \
  89. const auto cv_temp_msglevel = (cv::utils::logging::LogLevel)(msgLevel); \
  90. if (cv_temp_msglevel >= (CV_LOG_STRIP_LEVEL)) break; \
  91. auto cv_temp_logtagptr = CV_LOGTAG_PTR_CAST(CV_LOGTAG_EXPAND_NAME(tag)); \
  92. if (!cv_temp_logtagptr) cv_temp_logtagptr = CV_LOGTAG_PTR_CAST(CV_LOGTAG_FALLBACK); \
  93. if (!cv_temp_logtagptr) cv_temp_logtagptr = CV_LOGTAG_PTR_CAST(CV_LOGTAG_GLOBAL); \
  94. if (cv_temp_logtagptr && (cv_temp_msglevel > cv_temp_logtagptr->level)) break; \
  95. extra_check1; \
  96. std::stringstream cv_temp_logstream; \
  97. cv_temp_logstream << __VA_ARGS__; \
  98. cv::utils::logging::internal::writeLogMessageEx( \
  99. cv_temp_msglevel, \
  100. (cv_temp_logtagptr ? cv_temp_logtagptr->name : nullptr), \
  101. __FILE__, \
  102. __LINE__, \
  103. CV_Func, \
  104. cv_temp_logstream.str().c_str()); \
  105. break; \
  106. }
  107. #define CV_LOG_FATAL(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_FATAL, , , __VA_ARGS__)
  108. #define CV_LOG_ERROR(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_ERROR, , , __VA_ARGS__)
  109. #define CV_LOG_WARNING(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_WARNING, , , __VA_ARGS__)
  110. #define CV_LOG_INFO(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_INFO, , , __VA_ARGS__)
  111. #define CV_LOG_DEBUG(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_DEBUG, , , __VA_ARGS__)
  112. #define CV_LOG_VERBOSE(tag, v, ...) CV_LOG_WITH_TAG(tag, (cv::utils::logging::LOG_LEVEL_VERBOSE + (int)(v)), , , __VA_ARGS__)
  113. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
  114. #undef CV_LOG_INFO
  115. #define CV_LOG_INFO(tag, ...)
  116. #endif
  117. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
  118. #undef CV_LOG_DEBUG
  119. #define CV_LOG_DEBUG(tag, ...)
  120. #endif
  121. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
  122. #undef CV_LOG_VERBOSE
  123. #define CV_LOG_VERBOSE(tag, v, ...)
  124. #endif
  125. //! @cond IGNORED
  126. #define CV__LOG_ONCE_CHECK_PRE \
  127. static bool _cv_log_once_ ## __LINE__ = false; \
  128. if (_cv_log_once_ ## __LINE__) break;
  129. #define CV__LOG_ONCE_CHECK_POST \
  130. _cv_log_once_ ## __LINE__ = true;
  131. #define CV__LOG_IF_CHECK(logging_cond) \
  132. if (!(logging_cond)) break;
  133. //! @endcond
  134. // CV_LOG_ONCE_XXX macros
  135. #define CV_LOG_ONCE_ERROR(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_ERROR, CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
  136. #define CV_LOG_ONCE_WARNING(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_WARNING, CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
  137. #define CV_LOG_ONCE_INFO(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_INFO, CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
  138. #define CV_LOG_ONCE_DEBUG(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_DEBUG, CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
  139. #define CV_LOG_ONCE_VERBOSE(tag, v, ...) CV_LOG_WITH_TAG(tag, (cv::utils::logging::LOG_LEVEL_VERBOSE + (int)(v)), CV__LOG_ONCE_CHECK_PRE, CV__LOG_ONCE_CHECK_POST, __VA_ARGS__)
  140. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
  141. #undef CV_LOG_ONCE_INFO
  142. #define CV_LOG_ONCE_INFO(tag, ...)
  143. #endif
  144. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
  145. #undef CV_LOG_ONCE_DEBUG
  146. #define CV_LOG_ONCE_DEBUG(tag, ...)
  147. #endif
  148. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
  149. #undef CV_LOG_ONCE_VERBOSE
  150. #define CV_LOG_ONCE_VERBOSE(tag, v, ...)
  151. #endif
  152. // CV_LOG_IF_XXX macros
  153. #define CV_LOG_IF_FATAL(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_FATAL, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
  154. #define CV_LOG_IF_ERROR(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_ERROR, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
  155. #define CV_LOG_IF_WARNING(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_WARNING, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
  156. #define CV_LOG_IF_INFO(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_INFO, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
  157. #define CV_LOG_IF_DEBUG(tag, logging_cond, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_DEBUG, , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
  158. #define CV_LOG_IF_VERBOSE(tag, v, logging_cond, ...) CV_LOG_WITH_TAG(tag, (cv::utils::logging::LOG_LEVEL_VERBOSE + (int)(v)), , CV__LOG_IF_CHECK(logging_cond), __VA_ARGS__)
  159. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
  160. #undef CV_LOG_IF_INFO
  161. #define CV_LOG_IF_INFO(tag, logging_cond, ...)
  162. #endif
  163. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
  164. #undef CV_LOG_IF_DEBUG
  165. #define CV_LOG_IF_DEBUG(tag, logging_cond, ...)
  166. #endif
  167. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
  168. #undef CV_LOG_IF_VERBOSE
  169. #define CV_LOG_IF_VERBOSE(tag, v, logging_cond, ...)
  170. #endif
  171. //! @}
  172. }}} // namespace
  173. #endif // OPENCV_LOGGER_HPP