logger.cpp
6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "precomp.hpp"
#include <opencv2/core/utils/configuration.private.hpp>
#include <opencv2/core/utils/logger.hpp>
#include "utils/logtagmanager.hpp"
#include "utils/logtagconfigparser.hpp"
#include <sstream>
#include <iostream>
#include <fstream>
#ifdef __ANDROID__
# include <android/log.h>
#endif
namespace cv {
namespace utils {
namespace logging {
namespace internal
{
// Combining several things that require static dynamic initialization in a
// well-defined order into a struct.
//
struct GlobalLoggingInitStruct
{
public:
#if defined NDEBUG
static const bool m_isDebugBuild = false;
#else
static const bool m_isDebugBuild = true;
#endif
public:
static LogLevel m_defaultUnconfiguredGlobalLevel;
public:
LogTagManager logTagManager;
GlobalLoggingInitStruct()
: logTagManager(m_defaultUnconfiguredGlobalLevel)
{
(void)getInitializationMutex(); // ensure initialization of global objects
applyConfigString();
handleMalformed();
}
private:
void applyConfigString()
{
logTagManager.setConfigString(utils::getConfigurationParameterString("OPENCV_LOG_LEVEL", ""));
}
void handleMalformed()
{
// need to print warning for malformed log tag config strings?
if (m_isDebugBuild)
{
const auto& parser = logTagManager.getConfigParser();
if (parser.hasMalformed())
{
const auto& malformedList = parser.getMalformed();
for (const auto& malformed : malformedList)
{
std::cout << "Malformed log level config: \"" << malformed << "\"\n";
}
std::cout.flush();
}
}
}
};
LogLevel GlobalLoggingInitStruct::m_defaultUnconfiguredGlobalLevel = GlobalLoggingInitStruct::m_isDebugBuild
? LOG_LEVEL_INFO
: LOG_LEVEL_WARNING;
// Static dynamic initialization guard function for the combined struct
// just defined above
//
// An initialization guard function guarantees that outside code cannot
// accidentally see not-yet-dynamically-initialized data, by routing
// all outside access request to this function, so that this function
// has a chance to run the initialization code if necessary.
//
// An initialization guard function only guarantees initialization upon
// the first call to this function.
//
static GlobalLoggingInitStruct& getGlobalLoggingInitStruct()
{
CV_SINGLETON_LAZY_INIT_REF(GlobalLoggingInitStruct, new GlobalLoggingInitStruct());
}
// To ensure that the combined struct defined above is initialized even
// if the initialization guard function wasn't called, a dummy static
// instance of a struct is defined below, which will call the
// initialization guard function.
//
struct GlobalLoggingInitCall
{
GlobalLoggingInitCall()
{
getGlobalLoggingInitStruct();
(void)getGlobalLogTag(); // complete initialization of logger structures
}
};
static GlobalLoggingInitCall globalLoggingInitCall;
static LogTagManager& getLogTagManager()
{
static LogTagManager& logTagManagerInstance = getGlobalLoggingInitStruct().logTagManager;
return logTagManagerInstance;
}
static LogLevel& getLogLevelVariable()
{
static LogLevel& refGlobalLogLevel = getGlobalLogTag()->level;
return refGlobalLogLevel;
}
LogTag* getGlobalLogTag()
{
static LogTag* globalLogTagPtr = getGlobalLoggingInitStruct().logTagManager.get("global");
return globalLogTagPtr;
}
} // namespace
void registerLogTag(LogTag* plogtag)
{
if (!plogtag || !plogtag->name)
{
return;
}
internal::getLogTagManager().assign(plogtag->name, plogtag);
}
void setLogTagLevel(const char* tag, LogLevel level)
{
if (!tag)
{
return;
}
internal::getLogTagManager().setLevelByFullName(std::string(tag), level);
}
LogLevel getLogTagLevel(const char* tag)
{
if (!tag)
{
return getLogLevel();
}
const LogTag* ptr = internal::getLogTagManager().get(std::string(tag));
if (!ptr)
{
return getLogLevel();
}
return ptr->level;
}
LogLevel setLogLevel(LogLevel logLevel)
{
// note: not thread safe, use sparingly and do not critically depend on outcome
LogLevel& refGlobalLevel = internal::getLogLevelVariable();
const LogLevel old = refGlobalLevel;
refGlobalLevel = logLevel;
return old;
}
LogLevel getLogLevel()
{
return internal::getLogLevelVariable();
}
namespace internal {
void writeLogMessage(LogLevel logLevel, const char* message)
{
const int threadID = cv::utils::getThreadID();
std::ostringstream ss;
switch (logLevel)
{
case LOG_LEVEL_FATAL: ss << "[FATAL:" << threadID << "] " << message << std::endl; break;
case LOG_LEVEL_ERROR: ss << "[ERROR:" << threadID << "] " << message << std::endl; break;
case LOG_LEVEL_WARNING: ss << "[ WARN:" << threadID << "] " << message << std::endl; break;
case LOG_LEVEL_INFO: ss << "[ INFO:" << threadID << "] " << message << std::endl; break;
case LOG_LEVEL_DEBUG: ss << "[DEBUG:" << threadID << "] " << message << std::endl; break;
case LOG_LEVEL_VERBOSE: ss << message << std::endl; break;
case LOG_LEVEL_SILENT: return; // avoid compiler warning about incomplete switch
case ENUM_LOG_LEVEL_FORCE_INT: return; // avoid compiler warning about incomplete switch
}
#ifdef __ANDROID__
int android_logLevel = ANDROID_LOG_INFO;
switch (logLevel)
{
case LOG_LEVEL_FATAL: android_logLevel = ANDROID_LOG_FATAL; break;
case LOG_LEVEL_ERROR: android_logLevel = ANDROID_LOG_ERROR; break;
case LOG_LEVEL_WARNING: android_logLevel = ANDROID_LOG_WARN; break;
case LOG_LEVEL_INFO: android_logLevel = ANDROID_LOG_INFO; break;
case LOG_LEVEL_DEBUG: android_logLevel = ANDROID_LOG_DEBUG; break;
case LOG_LEVEL_VERBOSE: android_logLevel = ANDROID_LOG_VERBOSE; break;
default:
break;
}
__android_log_print(android_logLevel, "OpenCV/" CV_VERSION, "%s", ss.str().c_str());
#endif
std::ostream* out = (logLevel <= LOG_LEVEL_WARNING) ? &std::cerr : &std::cout;
(*out) << ss.str();
if (logLevel <= LOG_LEVEL_WARNING)
(*out) << std::flush;
}
void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message)
{
std::ostringstream strm;
if (tag)
{
strm << tag << " ";
}
if (file)
{
strm << file << " ";
}
if (line > 0)
{
strm << "(" << line << ") ";
}
if (func)
{
strm << func << " ";
}
strm << message;
writeLogMessage(logLevel, strm.str().c_str());
}
} // namespace
}}} // namespace