/src/logging-log4cxx/src/main/cpp/loglog.cpp
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | | * contributor license agreements. See the NOTICE file distributed with |
4 | | * this work for additional information regarding copyright ownership. |
5 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | | * (the "License"); you may not use this file except in compliance with |
7 | | * the License. You may obtain a copy of the License at |
8 | | * |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | * |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | #include <log4cxx/logstring.h> |
19 | | #include <log4cxx/helpers/loglog.h> |
20 | | #include <log4cxx/logmanager.h> |
21 | | #include <log4cxx/helpers/transcoder.h> |
22 | | #include <iostream> |
23 | | #if !defined(LOG4CXX) |
24 | | #define LOG4CXX 1 |
25 | | #endif |
26 | | #include <log4cxx/private/log4cxx_private.h> |
27 | | #include <log4cxx/helpers/aprinitializer.h> |
28 | | #include <log4cxx/helpers/date.h> |
29 | | #include <log4cxx/helpers/stringhelper.h> |
30 | | #include <log4cxx/helpers/systemerrwriter.h> |
31 | | #include <log4cxx/helpers/optionconverter.h> |
32 | | #include <mutex> |
33 | | |
34 | | using namespace LOG4CXX_NS; |
35 | | using namespace LOG4CXX_NS::helpers; |
36 | | |
37 | | struct LogLog::LogLogPrivate { |
38 | | LogLogPrivate() : |
39 | 1 | debugEnabled(false), |
40 | 1 | quietMode(false){} |
41 | | |
42 | | ~LogLogPrivate() |
43 | 1 | { |
44 | 1 | quietMode = true; // Prevent output after deletion by onexit processing chain. |
45 | 1 | } |
46 | | |
47 | | bool debugEnabled; |
48 | | |
49 | | /** |
50 | | In quietMode not even errors generate any output. |
51 | | */ |
52 | | bool quietMode; |
53 | | std::mutex mutex; |
54 | | LogString errorPrefix; |
55 | | LogString warnPrefix; |
56 | | LogString debugPrefix; |
57 | | LogString suffix; |
58 | | void setColorEnabled(bool newValue) |
59 | 1 | { |
60 | 1 | if (newValue) |
61 | 1 | { |
62 | 1 | this->errorPrefix = LOG4CXX_STR("\x1B[31m"); //red |
63 | 1 | this->warnPrefix = LOG4CXX_STR("\x1B[33m"); //yellow |
64 | 1 | this->debugPrefix = LOG4CXX_STR("\x1B[32m"); //green |
65 | 1 | this->suffix = LOG4CXX_STR("\x1B[0m"); // none |
66 | 1 | } |
67 | 0 | else |
68 | 0 | { |
69 | 0 | this->errorPrefix.clear(); |
70 | 0 | this->warnPrefix.clear(); |
71 | 0 | this->debugPrefix.clear(); |
72 | 0 | this->suffix.clear(); |
73 | 0 | } |
74 | 1 | } |
75 | | LogString elapsedMicroseconds() |
76 | 0 | { |
77 | 0 | LogString result; |
78 | 0 | auto microsecondInterval = Date::currentTime() - APRInitializer::getStartTime(); |
79 | 0 | StringHelper::toString(microsecondInterval, result); |
80 | 0 | return result; |
81 | 0 | } |
82 | | }; |
83 | | |
84 | | LogLog::LogLog() : |
85 | 1 | m_priv(std::make_unique<LogLogPrivate>()) |
86 | 1 | { |
87 | 1 | LogString log4cxxDebug = OptionConverter::getSystemProperty(LOG4CXX_STR("LOG4CXX_DEBUG"), LOG4CXX_STR("false")); |
88 | 1 | m_priv->debugEnabled = OptionConverter::toBoolean(log4cxxDebug, false); |
89 | 1 | auto color = OptionConverter::getSystemProperty(LOG4CXX_STR("LOG4CXX_COLOR"), LOG4CXX_STR("true")); |
90 | 1 | m_priv->setColorEnabled(OptionConverter::toBoolean(color, true)); |
91 | 1 | } |
92 | | |
93 | | LogLog::~LogLog() |
94 | 1 | { m_priv.reset(); } |
95 | | |
96 | | LogLog& LogLog::getInstance() |
97 | 2.30k | { |
98 | 2.30k | static WideLife<LogLog> internalLogger; |
99 | | |
100 | 2.30k | return internalLogger; |
101 | 2.30k | } |
102 | | |
103 | | bool LogLog::isDebugEnabled() |
104 | 2.30k | { |
105 | 2.30k | auto p = getInstance().m_priv.get(); |
106 | 2.30k | return p && !p->quietMode // Not deleted by onexit processing? |
107 | 2.30k | && p->debugEnabled; |
108 | 2.30k | } |
109 | | |
110 | | bool LogLog::isDebugEnabledFor(const LoggerPtr& category) |
111 | 0 | { |
112 | 0 | return isDebugEnabled() && category && category->isDebugEnabled(); |
113 | 0 | } |
114 | | |
115 | | bool LogLog::isTraceEnabledFor(const LoggerPtr& category) |
116 | 0 | { |
117 | 0 | return isDebugEnabled() && category && category->isTraceEnabled(); |
118 | 0 | } |
119 | | |
120 | | void LogLog::setInternalDebugging(bool debugEnabled1) |
121 | 0 | { |
122 | 0 | auto p = getInstance().m_priv.get(); |
123 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
124 | 0 | p->debugEnabled = debugEnabled1; |
125 | 0 | } |
126 | | |
127 | | bool LogLog::isColorEnabled() |
128 | 0 | { |
129 | 0 | auto p = getInstance().m_priv.get(); |
130 | 0 | return p && !p->errorPrefix.empty(); |
131 | 0 | } |
132 | | |
133 | | void LogLog::setColorEnabled(bool newValue) |
134 | 0 | { |
135 | 0 | if (auto p = getInstance().m_priv.get()) |
136 | 0 | p->setColorEnabled(newValue); |
137 | 0 | } |
138 | | |
139 | | void LogLog::debug(const LogString& msg) |
140 | 1 | { |
141 | 1 | auto p = getInstance().m_priv.get(); |
142 | 1 | if (p && !p->quietMode) // Not deleted by onexit processing? |
143 | 1 | { |
144 | 1 | if (!p->debugEnabled) |
145 | 1 | { |
146 | 1 | return; |
147 | 1 | } |
148 | | |
149 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
150 | 0 | emit_log(p->debugPrefix, msg, p->suffix); |
151 | 0 | } |
152 | 1 | } |
153 | | |
154 | | void LogLog::debug(const LogString& msg, const std::exception& e) |
155 | 0 | { |
156 | 0 | auto p = getInstance().m_priv.get(); |
157 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
158 | 0 | { |
159 | 0 | if (!p->debugEnabled) |
160 | 0 | return; |
161 | | |
162 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
163 | 0 | emit_log(p->debugPrefix, msg, p->suffix); |
164 | 0 | emit_log(p->debugPrefix, e, p->suffix); |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | | |
169 | | void LogLog::error(const LogString& msg) |
170 | 0 | { |
171 | 0 | auto p = getInstance().m_priv.get(); |
172 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
173 | 0 | { |
174 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
175 | |
|
176 | 0 | emit_log(p->errorPrefix, msg, p->suffix); |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | | void LogLog::error(const LogString& msg, const std::exception& e) |
181 | 0 | { |
182 | 0 | auto p = getInstance().m_priv.get(); |
183 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
184 | 0 | { |
185 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
186 | 0 | emit_log(p->errorPrefix, msg, p->suffix); |
187 | 0 | emit_log(p->errorPrefix, e, p->suffix); |
188 | 0 | } |
189 | 0 | } |
190 | | |
191 | | LoggerPtr LogLog::getLogger(const LogString& name) |
192 | 0 | { |
193 | 0 | return LogManager::getLoggerRepository()->getLogger(name); |
194 | 0 | } |
195 | | |
196 | | #if !LOG4CXX_LOGCHAR_IS_UTF8 |
197 | | void LogLog::trace(const LoggerPtr& category, const std::string& msg) |
198 | | { |
199 | | LOG4CXX_DECODE_CHAR(lsMsg, msg); |
200 | | trace(category, lsMsg); |
201 | | } |
202 | | #endif |
203 | | |
204 | | void LogLog::trace(const LoggerPtr& category, const LogString& msg) |
205 | 0 | { |
206 | 0 | auto p = getInstance().m_priv.get(); |
207 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
208 | 0 | { |
209 | 0 | if (!p->debugEnabled) |
210 | 0 | { |
211 | 0 | return; |
212 | 0 | } |
213 | | |
214 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
215 | 0 | emit_log(p->debugPrefix, p->elapsedMicroseconds() + LOG4CXX_STR(" ") + category->getName() + LOG4CXX_STR("::") + msg, p->suffix); |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | | void LogLog::setQuietMode(bool quietMode1) |
220 | 0 | { |
221 | 0 | auto p = getInstance().m_priv.get(); |
222 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
223 | |
|
224 | 0 | p->quietMode = quietMode1; |
225 | 0 | } |
226 | | |
227 | | void LogLog::warn(const LogString& msg) |
228 | 0 | { |
229 | 0 | auto p = getInstance().m_priv.get(); |
230 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
231 | 0 | { |
232 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
233 | 0 | emit_log(p->warnPrefix, msg, p->suffix); |
234 | 0 | } |
235 | 0 | } |
236 | | |
237 | | void LogLog::warn(const LogString& msg, const std::exception& e) |
238 | 0 | { |
239 | 0 | auto p = getInstance().m_priv.get(); |
240 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
241 | 0 | { |
242 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
243 | 0 | emit_log(p->warnPrefix, msg, p->suffix); |
244 | 0 | emit_log(p->warnPrefix, e, p->suffix); |
245 | 0 | } |
246 | 0 | } |
247 | | |
248 | | void LogLog::emit_log(const LogString& prefix, const LogString& msg, const LogString& suffix) |
249 | 0 | { |
250 | 0 | LogString out(LOG4CXX_STR("log4cxx: ")); |
251 | 0 | out.append(prefix); |
252 | 0 | out.append(msg); |
253 | 0 | out.append(suffix); |
254 | 0 | out.append(1, (logchar) 0x0A); |
255 | |
|
256 | 0 | SystemErrWriter().write(out); |
257 | 0 | } |
258 | | |
259 | | void LogLog::emit_log(const LogString& prefix, const std::exception& ex, const LogString& suffix) |
260 | 0 | { |
261 | 0 | LogString out(LOG4CXX_STR("log4cxx: ")); |
262 | 0 | out.append(prefix); |
263 | 0 | const char* raw = ex.what(); |
264 | |
|
265 | 0 | if (raw != 0) |
266 | 0 | { |
267 | 0 | Transcoder::decode(raw, out); |
268 | 0 | } |
269 | 0 | else |
270 | 0 | { |
271 | 0 | out.append(LOG4CXX_STR("std::exception::what() == null")); |
272 | 0 | } |
273 | |
|
274 | 0 | out.append(suffix); |
275 | 0 | out.append(1, (logchar) 0x0A); |
276 | |
|
277 | 0 | SystemErrWriter().write(out); |
278 | 0 | } |