/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/helpers/transcoder.h> |
21 | | #include <iostream> |
22 | | #if !defined(LOG4CXX) |
23 | | #define LOG4CXX 1 |
24 | | #endif |
25 | | #include <log4cxx/private/log4cxx_private.h> |
26 | | #include <log4cxx/helpers/aprinitializer.h> |
27 | | #include <log4cxx/helpers/systemerrwriter.h> |
28 | | #include <log4cxx/helpers/optionconverter.h> |
29 | | #include <mutex> |
30 | | |
31 | | using namespace LOG4CXX_NS; |
32 | | using namespace LOG4CXX_NS::helpers; |
33 | | |
34 | | struct LogLog::LogLogPrivate { |
35 | | LogLogPrivate() : |
36 | 9 | debugEnabled(false), |
37 | 9 | quietMode(false){} |
38 | | |
39 | | ~LogLogPrivate() |
40 | 9 | { |
41 | 9 | quietMode = true; // Prevent output after deletion by onexit processing chain. |
42 | 9 | } |
43 | | |
44 | | bool debugEnabled; |
45 | | |
46 | | /** |
47 | | In quietMode not even errors generate any output. |
48 | | */ |
49 | | bool quietMode; |
50 | | std::mutex mutex; |
51 | | LogString errorPrefix; |
52 | | LogString warnPrefix; |
53 | | LogString debugPrefix; |
54 | | LogString suffix; |
55 | | void setColorEnabled(bool newValue) |
56 | 37 | { |
57 | 37 | if (newValue) |
58 | 36 | { |
59 | 36 | this->errorPrefix = LOG4CXX_STR("\x1B[31m"); //red |
60 | 36 | this->warnPrefix = LOG4CXX_STR("\x1B[33m"); //yellow |
61 | 36 | this->debugPrefix = LOG4CXX_STR("\x1B[32m"); //green |
62 | 36 | this->suffix = LOG4CXX_STR("\x1B[0m"); // none |
63 | 36 | } |
64 | 1 | else |
65 | 1 | { |
66 | 1 | this->errorPrefix.clear(); |
67 | 1 | this->warnPrefix.clear(); |
68 | 1 | this->debugPrefix.clear(); |
69 | 1 | this->suffix.clear(); |
70 | 1 | } |
71 | 37 | } |
72 | | }; |
73 | | |
74 | | LogLog::LogLog() : |
75 | 9 | m_priv(std::make_unique<LogLogPrivate>()) |
76 | 9 | { |
77 | 9 | LogString log4cxxDebug = OptionConverter::getSystemProperty(LOG4CXX_STR("LOG4CXX_DEBUG"), LOG4CXX_STR("false")); |
78 | 9 | m_priv->debugEnabled = OptionConverter::toBoolean(log4cxxDebug, false); |
79 | 9 | auto color = OptionConverter::getSystemProperty(LOG4CXX_STR("LOG4CXX_COLOR"), LOG4CXX_STR("true")); |
80 | 9 | m_priv->setColorEnabled(OptionConverter::toBoolean(color, true)); |
81 | 9 | } |
82 | | |
83 | | LogLog::~LogLog() |
84 | 9 | { m_priv.reset(); } |
85 | | |
86 | | LogLog& LogLog::getInstance() |
87 | 67.1k | { |
88 | 67.1k | static WideLife<LogLog> internalLogger; |
89 | | |
90 | 67.1k | return internalLogger; |
91 | 67.1k | } |
92 | | |
93 | | bool LogLog::isDebugEnabled() |
94 | 7.01k | { |
95 | 7.01k | auto p = getInstance().m_priv.get(); |
96 | 7.01k | return p && !p->quietMode // Not deleted by onexit processing? |
97 | 7.01k | && p->debugEnabled; |
98 | 7.01k | } |
99 | | |
100 | | void LogLog::setInternalDebugging(bool debugEnabled1) |
101 | 178 | { |
102 | 178 | auto p = getInstance().m_priv.get(); |
103 | 178 | if (p && !p->quietMode) // Not deleted by onexit processing? |
104 | 178 | p->debugEnabled = debugEnabled1; |
105 | 178 | } |
106 | | |
107 | | bool LogLog::isColorEnabled() |
108 | 0 | { |
109 | 0 | auto p = getInstance().m_priv.get(); |
110 | 0 | return p && !p->errorPrefix.empty(); |
111 | 0 | } |
112 | | |
113 | | void LogLog::setColorEnabled(bool newValue) |
114 | 28 | { |
115 | 28 | if (auto p = getInstance().m_priv.get()) |
116 | 28 | p->setColorEnabled(newValue); |
117 | 28 | } |
118 | | |
119 | | void LogLog::debug(const LogString& msg) |
120 | 4.27k | { |
121 | 4.27k | auto p = getInstance().m_priv.get(); |
122 | 4.27k | if (p && !p->quietMode) // Not deleted by onexit processing? |
123 | 4.27k | { |
124 | 4.27k | if (!p->debugEnabled) |
125 | 7 | { |
126 | 7 | return; |
127 | 7 | } |
128 | | |
129 | 4.26k | std::lock_guard<std::mutex> lock(p->mutex); |
130 | | |
131 | 4.26k | emit_log(p->debugPrefix, msg, p->suffix); |
132 | 4.26k | } |
133 | 4.27k | } log4cxx::helpers::LogLog::debug(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 120 | 2.43k | { | 121 | 2.43k | auto p = getInstance().m_priv.get(); | 122 | 2.43k | if (p && !p->quietMode) // Not deleted by onexit processing? | 123 | 2.43k | { | 124 | 2.43k | if (!p->debugEnabled) | 125 | 4 | { | 126 | 4 | return; | 127 | 4 | } | 128 | | | 129 | 2.43k | std::lock_guard<std::mutex> lock(p->mutex); | 130 | | | 131 | 2.43k | emit_log(p->debugPrefix, msg, p->suffix); | 132 | 2.43k | } | 133 | 2.43k | } |
log4cxx::helpers::LogLog::debug(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 120 | 1.83k | { | 121 | 1.83k | auto p = getInstance().m_priv.get(); | 122 | 1.83k | if (p && !p->quietMode) // Not deleted by onexit processing? | 123 | 1.83k | { | 124 | 1.83k | if (!p->debugEnabled) | 125 | 3 | { | 126 | 3 | return; | 127 | 3 | } | 128 | | | 129 | 1.83k | std::lock_guard<std::mutex> lock(p->mutex); | 130 | | | 131 | 1.83k | emit_log(p->debugPrefix, msg, p->suffix); | 132 | 1.83k | } | 133 | 1.83k | } |
|
134 | | |
135 | | void LogLog::debug(const LogString& msg, const std::exception& e) |
136 | 0 | { |
137 | 0 | auto p = getInstance().m_priv.get(); |
138 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
139 | 0 | { |
140 | 0 | if (!p->debugEnabled) |
141 | 0 | return; |
142 | | |
143 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
144 | 0 | emit_log(p->debugPrefix, msg, p->suffix); |
145 | 0 | emit_log(p->debugPrefix, e, p->suffix); |
146 | 0 | } |
147 | 0 | } Unexecuted instantiation: log4cxx::helpers::LogLog::debug(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::exception const&) Unexecuted instantiation: log4cxx::helpers::LogLog::debug(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::exception const&) |
148 | | |
149 | | |
150 | | void LogLog::error(const LogString& msg) |
151 | 38.0k | { |
152 | 38.0k | auto p = getInstance().m_priv.get(); |
153 | 38.0k | if (p && !p->quietMode) // Not deleted by onexit processing? |
154 | 38.0k | { |
155 | 38.0k | std::lock_guard<std::mutex> lock(p->mutex); |
156 | | |
157 | 38.0k | emit_log(p->errorPrefix, msg, p->suffix); |
158 | 38.0k | } |
159 | 38.0k | } log4cxx::helpers::LogLog::error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 151 | 24.4k | { | 152 | 24.4k | auto p = getInstance().m_priv.get(); | 153 | 24.4k | if (p && !p->quietMode) // Not deleted by onexit processing? | 154 | 24.4k | { | 155 | 24.4k | std::lock_guard<std::mutex> lock(p->mutex); | 156 | | | 157 | 24.4k | emit_log(p->errorPrefix, msg, p->suffix); | 158 | 24.4k | } | 159 | 24.4k | } |
log4cxx::helpers::LogLog::error(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 151 | 13.5k | { | 152 | 13.5k | auto p = getInstance().m_priv.get(); | 153 | 13.5k | if (p && !p->quietMode) // Not deleted by onexit processing? | 154 | 13.5k | { | 155 | 13.5k | std::lock_guard<std::mutex> lock(p->mutex); | 156 | | | 157 | 13.5k | emit_log(p->errorPrefix, msg, p->suffix); | 158 | 13.5k | } | 159 | 13.5k | } |
|
160 | | |
161 | | void LogLog::error(const LogString& msg, const std::exception& e) |
162 | 3.78k | { |
163 | 3.78k | auto p = getInstance().m_priv.get(); |
164 | 3.78k | if (p && !p->quietMode) // Not deleted by onexit processing? |
165 | 3.78k | { |
166 | 3.78k | std::lock_guard<std::mutex> lock(p->mutex); |
167 | 3.78k | emit_log(p->errorPrefix, msg, p->suffix); |
168 | 3.78k | emit_log(p->errorPrefix, e, p->suffix); |
169 | 3.78k | } |
170 | 3.78k | } log4cxx::helpers::LogLog::error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::exception const&) Line | Count | Source | 162 | 1.99k | { | 163 | 1.99k | auto p = getInstance().m_priv.get(); | 164 | 1.99k | if (p && !p->quietMode) // Not deleted by onexit processing? | 165 | 1.99k | { | 166 | 1.99k | std::lock_guard<std::mutex> lock(p->mutex); | 167 | 1.99k | emit_log(p->errorPrefix, msg, p->suffix); | 168 | 1.99k | emit_log(p->errorPrefix, e, p->suffix); | 169 | 1.99k | } | 170 | 1.99k | } |
log4cxx::helpers::LogLog::error(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::exception const&) Line | Count | Source | 162 | 1.79k | { | 163 | 1.79k | auto p = getInstance().m_priv.get(); | 164 | 1.79k | if (p && !p->quietMode) // Not deleted by onexit processing? | 165 | 1.79k | { | 166 | 1.79k | std::lock_guard<std::mutex> lock(p->mutex); | 167 | 1.79k | emit_log(p->errorPrefix, msg, p->suffix); | 168 | 1.79k | emit_log(p->errorPrefix, e, p->suffix); | 169 | 1.79k | } | 170 | 1.79k | } |
|
171 | | |
172 | | void LogLog::setQuietMode(bool quietMode1) |
173 | 0 | { |
174 | 0 | auto p = getInstance().m_priv.get(); |
175 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
176 | |
|
177 | 0 | p->quietMode = quietMode1; |
178 | 0 | } |
179 | | |
180 | | void LogLog::warn(const LogString& msg) |
181 | 984 | { |
182 | 984 | auto p = getInstance().m_priv.get(); |
183 | 984 | if (p && !p->quietMode) // Not deleted by onexit processing? |
184 | 984 | { |
185 | 984 | std::lock_guard<std::mutex> lock(p->mutex); |
186 | 984 | emit_log(p->warnPrefix, msg, p->suffix); |
187 | 984 | } |
188 | 984 | } log4cxx::helpers::LogLog::warn(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 181 | 627 | { | 182 | 627 | auto p = getInstance().m_priv.get(); | 183 | 627 | if (p && !p->quietMode) // Not deleted by onexit processing? | 184 | 627 | { | 185 | 627 | std::lock_guard<std::mutex> lock(p->mutex); | 186 | 627 | emit_log(p->warnPrefix, msg, p->suffix); | 187 | 627 | } | 188 | 627 | } |
log4cxx::helpers::LogLog::warn(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 181 | 357 | { | 182 | 357 | auto p = getInstance().m_priv.get(); | 183 | 357 | if (p && !p->quietMode) // Not deleted by onexit processing? | 184 | 357 | { | 185 | 357 | std::lock_guard<std::mutex> lock(p->mutex); | 186 | 357 | emit_log(p->warnPrefix, msg, p->suffix); | 187 | 357 | } | 188 | 357 | } |
|
189 | | |
190 | | void LogLog::warn(const LogString& msg, const std::exception& e) |
191 | 12.8k | { |
192 | 12.8k | auto p = getInstance().m_priv.get(); |
193 | 12.8k | if (p && !p->quietMode) // Not deleted by onexit processing? |
194 | 12.8k | { |
195 | 12.8k | std::lock_guard<std::mutex> lock(p->mutex); |
196 | 12.8k | emit_log(p->warnPrefix, msg, p->suffix); |
197 | 12.8k | emit_log(p->warnPrefix, e, p->suffix); |
198 | 12.8k | } |
199 | 12.8k | } log4cxx::helpers::LogLog::warn(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::exception const&) Line | Count | Source | 191 | 5.50k | { | 192 | 5.50k | auto p = getInstance().m_priv.get(); | 193 | 5.50k | if (p && !p->quietMode) // Not deleted by onexit processing? | 194 | 5.50k | { | 195 | 5.50k | std::lock_guard<std::mutex> lock(p->mutex); | 196 | 5.50k | emit_log(p->warnPrefix, msg, p->suffix); | 197 | 5.50k | emit_log(p->warnPrefix, e, p->suffix); | 198 | 5.50k | } | 199 | 5.50k | } |
log4cxx::helpers::LogLog::warn(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::exception const&) Line | Count | Source | 191 | 7.36k | { | 192 | 7.36k | auto p = getInstance().m_priv.get(); | 193 | 7.36k | if (p && !p->quietMode) // Not deleted by onexit processing? | 194 | 7.36k | { | 195 | 7.36k | std::lock_guard<std::mutex> lock(p->mutex); | 196 | 7.36k | emit_log(p->warnPrefix, msg, p->suffix); | 197 | 7.36k | emit_log(p->warnPrefix, e, p->suffix); | 198 | 7.36k | } | 199 | 7.36k | } |
|
200 | | |
201 | | void LogLog::emit_log(const LogString& prefix, const LogString& msg, const LogString& suffix) |
202 | 59.9k | { |
203 | 59.9k | LogString out(LOG4CXX_STR("log4cxx: ")); |
204 | 59.9k | out.append(prefix); |
205 | 59.9k | out.append(msg); |
206 | 59.9k | out.append(suffix); |
207 | 59.9k | out.append(1, (logchar) 0x0A); |
208 | | |
209 | 59.9k | SystemErrWriter::write(out); |
210 | 59.9k | } log4cxx::helpers::LogLog::emit_log(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 202 | 35.0k | { | 203 | 35.0k | LogString out(LOG4CXX_STR("log4cxx: ")); | 204 | 35.0k | out.append(prefix); | 205 | 35.0k | out.append(msg); | 206 | 35.0k | out.append(suffix); | 207 | 35.0k | out.append(1, (logchar) 0x0A); | 208 | | | 209 | 35.0k | SystemErrWriter::write(out); | 210 | 35.0k | } |
log4cxx::helpers::LogLog::emit_log(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 202 | 24.8k | { | 203 | 24.8k | LogString out(LOG4CXX_STR("log4cxx: ")); | 204 | 24.8k | out.append(prefix); | 205 | 24.8k | out.append(msg); | 206 | 24.8k | out.append(suffix); | 207 | 24.8k | out.append(1, (logchar) 0x0A); | 208 | | | 209 | 24.8k | SystemErrWriter::write(out); | 210 | 24.8k | } |
|
211 | | |
212 | | void LogLog::emit_log(const LogString& prefix, const std::exception& ex, const LogString& suffix) |
213 | 16.6k | { |
214 | 16.6k | LogString out(LOG4CXX_STR("log4cxx: ")); |
215 | 16.6k | out.append(prefix); |
216 | 16.6k | const char* raw = ex.what(); |
217 | | |
218 | 16.6k | if (raw != 0) |
219 | 16.6k | { |
220 | 16.6k | Transcoder::decode(raw, out); |
221 | 16.6k | } |
222 | 0 | else |
223 | 0 | { |
224 | 0 | out.append(LOG4CXX_STR("std::exception::what() == null")); |
225 | 0 | } |
226 | | |
227 | 16.6k | out.append(suffix); |
228 | 16.6k | out.append(1, (logchar) 0x0A); |
229 | | |
230 | 16.6k | SystemErrWriter::write(out); |
231 | 16.6k | } log4cxx::helpers::LogLog::emit_log(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::exception const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Line | Count | Source | 213 | 7.49k | { | 214 | 7.49k | LogString out(LOG4CXX_STR("log4cxx: ")); | 215 | 7.49k | out.append(prefix); | 216 | 7.49k | const char* raw = ex.what(); | 217 | | | 218 | 7.49k | if (raw != 0) | 219 | 7.49k | { | 220 | 7.49k | Transcoder::decode(raw, out); | 221 | 7.49k | } | 222 | 0 | else | 223 | 0 | { | 224 | 0 | out.append(LOG4CXX_STR("std::exception::what() == null")); | 225 | 0 | } | 226 | | | 227 | 7.49k | out.append(suffix); | 228 | 7.49k | out.append(1, (logchar) 0x0A); | 229 | | | 230 | 7.49k | SystemErrWriter::write(out); | 231 | 7.49k | } |
log4cxx::helpers::LogLog::emit_log(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&, std::exception const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&) Line | Count | Source | 213 | 9.16k | { | 214 | 9.16k | LogString out(LOG4CXX_STR("log4cxx: ")); | 215 | 9.16k | out.append(prefix); | 216 | 9.16k | const char* raw = ex.what(); | 217 | | | 218 | 9.16k | if (raw != 0) | 219 | 9.16k | { | 220 | 9.16k | Transcoder::decode(raw, out); | 221 | 9.16k | } | 222 | 0 | else | 223 | 0 | { | 224 | 0 | out.append(LOG4CXX_STR("std::exception::what() == null")); | 225 | 0 | } | 226 | | | 227 | 9.16k | out.append(suffix); | 228 | 9.16k | out.append(1, (logchar) 0x0A); | 229 | | | 230 | 9.16k | SystemErrWriter::write(out); | 231 | 9.16k | } |
|