logger.hpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. //! @addtogroup core_logging
  11. // This section describes OpenCV logging utilities.
  12. //
  13. //! @{
  14. namespace cv {
  15. namespace utils {
  16. namespace logging {
  17. //! Supported logging levels and their semantic
  18. enum LogLevel {
  19. LOG_LEVEL_SILENT = 0, //!< for using in setLogVevel() call
  20. LOG_LEVEL_FATAL = 1, //!< Fatal (critical) error (unrecoverable internal error)
  21. LOG_LEVEL_ERROR = 2, //!< Error message
  22. LOG_LEVEL_WARNING = 3, //!< Warning message
  23. LOG_LEVEL_INFO = 4, //!< Info message
  24. LOG_LEVEL_DEBUG = 5, //!< Debug message. Disabled in the "Release" build.
  25. LOG_LEVEL_VERBOSE = 6, //!< Verbose (trace) messages. Requires verbosity level. Disabled in the "Release" build.
  26. #ifndef CV_DOXYGEN
  27. ENUM_LOG_LEVEL_FORCE_INT = INT_MAX
  28. #endif
  29. };
  30. /** Set global logging level
  31. @return previous logging level
  32. */
  33. CV_EXPORTS LogLevel setLogLevel(LogLevel logLevel);
  34. /** Get global logging level */
  35. CV_EXPORTS LogLevel getLogLevel();
  36. namespace internal {
  37. /** Write log message */
  38. CV_EXPORTS void writeLogMessage(LogLevel logLevel, const char* message);
  39. } // namespace
  40. /**
  41. * \def CV_LOG_STRIP_LEVEL
  42. *
  43. * Define CV_LOG_STRIP_LEVEL=CV_LOG_LEVEL_[DEBUG|INFO|WARN|ERROR|FATAL|DISABLED] to compile out anything at that and before that logging level
  44. */
  45. #ifndef CV_LOG_STRIP_LEVEL
  46. # if defined NDEBUG
  47. # define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_DEBUG
  48. # else
  49. # define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_VERBOSE
  50. # endif
  51. #endif
  52. #define CV_LOG_FATAL(tag, ...) for(;;) { if (cv::utils::logging::getLogLevel() < cv::utils::logging::LOG_LEVEL_FATAL) break; std::stringstream ss; ss << __VA_ARGS__; cv::utils::logging::internal::writeLogMessage(cv::utils::logging::LOG_LEVEL_FATAL, ss.str().c_str()); break; }
  53. #define CV_LOG_ERROR(tag, ...) for(;;) { if (cv::utils::logging::getLogLevel() < cv::utils::logging::LOG_LEVEL_ERROR) break; std::stringstream ss; ss << __VA_ARGS__; cv::utils::logging::internal::writeLogMessage(cv::utils::logging::LOG_LEVEL_ERROR, ss.str().c_str()); break; }
  54. #define CV_LOG_WARNING(tag, ...) for(;;) { if (cv::utils::logging::getLogLevel() < cv::utils::logging::LOG_LEVEL_WARNING) break; std::stringstream ss; ss << __VA_ARGS__; cv::utils::logging::internal::writeLogMessage(cv::utils::logging::LOG_LEVEL_WARNING, ss.str().c_str()); break; }
  55. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
  56. #define CV_LOG_INFO(tag, ...)
  57. #else
  58. #define CV_LOG_INFO(tag, ...) for(;;) { if (cv::utils::logging::getLogLevel() < cv::utils::logging::LOG_LEVEL_INFO) break; std::stringstream ss; ss << __VA_ARGS__; cv::utils::logging::internal::writeLogMessage(cv::utils::logging::LOG_LEVEL_INFO, ss.str().c_str()); break; }
  59. #endif
  60. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
  61. #define CV_LOG_DEBUG(tag, ...)
  62. #else
  63. #define CV_LOG_DEBUG(tag, ...) for(;;) { if (cv::utils::logging::getLogLevel() < cv::utils::logging::LOG_LEVEL_DEBUG) break; std::stringstream ss; ss << __VA_ARGS__; cv::utils::logging::internal::writeLogMessage(cv::utils::logging::LOG_LEVEL_DEBUG, ss.str().c_str()); break; }
  64. #endif
  65. #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
  66. #define CV_LOG_VERBOSE(tag, v, ...)
  67. #else
  68. #define CV_LOG_VERBOSE(tag, v, ...) for(;;) { if (cv::utils::logging::getLogLevel() < cv::utils::logging::LOG_LEVEL_VERBOSE) break; std::stringstream ss; ss << "[VERB" << v << ":" << cv::utils::getThreadID() << "] " << __VA_ARGS__; cv::utils::logging::internal::writeLogMessage(cv::utils::logging::LOG_LEVEL_VERBOSE, ss.str().c_str()); break; }
  69. #endif
  70. }}} // namespace
  71. //! @}
  72. #endif // OPENCV_LOGGER_HPP