EstervQrCode 1.1.1
Library for qr code manipulation
logger.hpp
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 
5 #ifndef OPENCV_LOGGER_HPP
6 #define OPENCV_LOGGER_HPP
7 
8 #include <iostream>
9 #include <sstream>
10 #include <limits.h> // INT_MAX
11 
12 #include "logger.defines.hpp"
13 #include "logtag.hpp"
14 
15 namespace cv {
16 namespace utils {
17 namespace logging {
18 
21 
28 
30 
32 
34 
35 namespace internal {
36 
39 
41 CV_EXPORTS void writeLogMessage(LogLevel logLevel, const char* message);
42 
44 CV_EXPORTS void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message);
45 
46 } // namespace
47 
48 struct LogTagAuto
49  : public LogTag
50 {
51  inline LogTagAuto(const char* _name, LogLevel _level)
52  : LogTag(_name, _level)
53  {
54  registerLogTag(this);
55  }
56 };
57 
63 #ifndef CV_LOG_STRIP_LEVEL
64 # if defined NDEBUG
65 # define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_DEBUG
66 # else
67 # define CV_LOG_STRIP_LEVEL CV_LOG_LEVEL_VERBOSE
68 # endif
69 #endif
70 
71 #define CV_LOGTAG_PTR_CAST(expr) static_cast<const cv::utils::logging::LogTag*>(expr)
72 
73 // CV_LOGTAG_EXPAND_NAME is intended to be re-defined (undef and then define again)
74 // to allows logging users to use a shorter name argument when calling
75 // CV_LOG_WITH_TAG or its related macros such as CV_LOG_INFO.
76 //
77 // This macro is intended to modify the tag argument as a string (token), via
78 // preprocessor token pasting or metaprogramming techniques. A typical usage
79 // is to apply a prefix, such as
80 // ...... #define CV_LOGTAG_EXPAND_NAME(tag) cv_logtag_##tag
81 //
82 // It is permitted to re-define to a hard-coded expression, ignoring the tag.
83 // This would work identically like the CV_LOGTAG_FALLBACK macro.
84 //
85 // Important: When the logging macro is called with tag being NULL, a user-defined
86 // CV_LOGTAG_EXPAND_NAME may expand it into cv_logtag_0, cv_logtag_NULL, or
87 // cv_logtag_nullptr. Use with care. Also be mindful of C++ symbol redefinitions.
88 //
89 // If there is significant amount of logging code with tag being NULL, it is
90 // recommended to use (re-define) CV_LOGTAG_FALLBACK to inject locally a default
91 // tag at the beginning of a compilation unit, to minimize lines of code changes.
92 //
93 #define CV_LOGTAG_EXPAND_NAME(tag) tag
94 
95 // CV_LOGTAG_FALLBACK is intended to be re-defined (undef and then define again)
96 // by any other compilation units to provide a log tag when the logging statement
97 // does not specify one. The macro needs to expand into a C++ expression that can
98 // be static_cast into (cv::utils::logging::LogTag*). Null (nullptr) is permitted.
99 #define CV_LOGTAG_FALLBACK nullptr
100 
101 // CV_LOGTAG_GLOBAL is the tag used when a log tag is not specified in the logging
102 // statement nor the compilation unit. The macro needs to expand into a C++
103 // expression that can be static_cast into (cv::utils::logging::LogTag*). Must be
104 // non-null. Do not re-define.
105 #define CV_LOGTAG_GLOBAL cv::utils::logging::internal::getGlobalLogTag()
106 
107 #define CV_LOG_WITH_TAG(tag, msgLevel, extra_check0, extra_check1, ...) \
108  for(;;) { \
109  extra_check0; \
110  const auto cv_temp_msglevel = (cv::utils::logging::LogLevel)(msgLevel); \
111  if (cv_temp_msglevel >= (CV_LOG_STRIP_LEVEL)) break; \
112  auto cv_temp_logtagptr = CV_LOGTAG_PTR_CAST(CV_LOGTAG_EXPAND_NAME(tag)); \
113  if (!cv_temp_logtagptr) cv_temp_logtagptr = CV_LOGTAG_PTR_CAST(CV_LOGTAG_FALLBACK); \
114  if (!cv_temp_logtagptr) cv_temp_logtagptr = CV_LOGTAG_PTR_CAST(CV_LOGTAG_GLOBAL); \
115  if (cv_temp_logtagptr && (cv_temp_msglevel > cv_temp_logtagptr->level)) break; \
116  extra_check1; \
117  std::stringstream cv_temp_logstream; \
118  cv_temp_logstream << __VA_ARGS__; \
119  cv::utils::logging::internal::writeLogMessageEx( \
120  cv_temp_msglevel, \
121  (cv_temp_logtagptr ? cv_temp_logtagptr->name : nullptr), \
122  __FILE__, \
123  __LINE__, \
124  CV_Func, \
125  cv_temp_logstream.str().c_str()); \
126  break; \
127  }
128 
129 #define CV_LOG_FATAL(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_FATAL, , , __VA_ARGS__)
130 #define CV_LOG_ERROR(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_ERROR, , , __VA_ARGS__)
131 #define CV_LOG_WARNING(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_WARNING, , , __VA_ARGS__)
132 #define CV_LOG_INFO(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_INFO, , , __VA_ARGS__)
133 #define CV_LOG_DEBUG(tag, ...) CV_LOG_WITH_TAG(tag, cv::utils::logging::LOG_LEVEL_DEBUG, , , __VA_ARGS__)
134 #define CV_LOG_VERBOSE(tag, v, ...) CV_LOG_WITH_TAG(tag, (cv::utils::logging::LOG_LEVEL_VERBOSE + (int)(v)), , , __VA_ARGS__)
135 
136 #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
137 #undef CV_LOG_INFO
138 #define CV_LOG_INFO(tag, ...)
139 #endif
140 
141 #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
142 #undef CV_LOG_DEBUG
143 #define CV_LOG_DEBUG(tag, ...)
144 #endif
145 
146 #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
147 #undef CV_LOG_VERBOSE
148 #define CV_LOG_VERBOSE(tag, v, ...)
149 #endif
150 
152 #define CV__LOG_ONCE_CHECK_PRE \
153  static bool _cv_log_once_ ## __LINE__ = false; \
154  if (_cv_log_once_ ## __LINE__) break;
155 
156 #define CV__LOG_ONCE_CHECK_POST \
157  _cv_log_once_ ## __LINE__ = true;
158 
159 #define CV__LOG_IF_CHECK(logging_cond) \
160  if (!(logging_cond)) break;
161 
163 
164 
165 // CV_LOG_ONCE_XXX macros
166 
167 #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__)
168 #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__)
169 #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__)
170 #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__)
171 #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__)
172 
173 #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
174 #undef CV_LOG_ONCE_INFO
175 #define CV_LOG_ONCE_INFO(tag, ...)
176 #endif
177 
178 #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
179 #undef CV_LOG_ONCE_DEBUG
180 #define CV_LOG_ONCE_DEBUG(tag, ...)
181 #endif
182 
183 #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
184 #undef CV_LOG_ONCE_VERBOSE
185 #define CV_LOG_ONCE_VERBOSE(tag, v, ...)
186 #endif
187 
188 
189 // CV_LOG_IF_XXX macros
190 
191 #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__)
192 #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__)
193 #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__)
194 #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__)
195 #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__)
196 #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__)
197 
198 #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_INFO
199 #undef CV_LOG_IF_INFO
200 #define CV_LOG_IF_INFO(tag, logging_cond, ...)
201 #endif
202 
203 #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_DEBUG
204 #undef CV_LOG_IF_DEBUG
205 #define CV_LOG_IF_DEBUG(tag, logging_cond, ...)
206 #endif
207 
208 #if CV_LOG_STRIP_LEVEL <= CV_LOG_LEVEL_VERBOSE
209 #undef CV_LOG_IF_VERBOSE
210 #define CV_LOG_IF_VERBOSE(tag, v, logging_cond, ...)
211 #endif
212 
213 
215 
216 }}} // namespace
217 
218 #endif // OPENCV_LOGGER_HPP
CvCmpFunc func
Definition: core_c.h:1712
CV_EXPORTS LogLevel getLogLevel()
CV_EXPORTS void registerLogTag(cv::utils::logging::LogTag *plogtag)
CV_EXPORTS void setLogTagLevel(const char *tag, cv::utils::logging::LogLevel level)
CV_EXPORTS cv::utils::logging::LogLevel getLogTagLevel(const char *tag)
CV_EXPORTS LogLevel setLogLevel(LogLevel logLevel)
#define CV_EXPORTS
Definition: cvdef.h:435
CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
CV_EXPORTS cv::utils::logging::LogTag * getGlobalLogTag()
CV_EXPORTS void writeLogMessage(LogLevel logLevel, const char *message)
CV_EXPORTS void writeLogMessageEx(LogLevel logLevel, const char *tag, const char *file, int line, const char *func, const char *message)
LogLevel
Supported logging levels and their semantic.
Definition: logger.defines.hpp:25
"black box" representation of the file storage associated with a file on disk.
Definition: calib3d.hpp:441
Definition: logger.hpp:50
LogTagAuto(const char *_name, LogLevel _level)
Definition: logger.hpp:51
Definition: logtag.hpp:16