/src/logging-log4cxx/src/main/cpp/loglog.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | debugEnabled(false), |
37 | 0 | quietMode(false){} |
38 | | |
39 | | ~LogLogPrivate() |
40 | 0 | { |
41 | 0 | quietMode = true; // Prevent output after deletion by onexit processing chain. |
42 | 0 | } |
43 | | |
44 | | bool debugEnabled; |
45 | | |
46 | | /** |
47 | | In quietMode not even errors generate any output. |
48 | | */ |
49 | | bool quietMode; |
50 | | std::mutex mutex; |
51 | | }; |
52 | | |
53 | | LogLog::LogLog() : |
54 | 0 | m_priv(std::make_unique<LogLogPrivate>()) |
55 | 0 | { |
56 | 0 | LogString log4cxxDebug = OptionConverter::getSystemProperty(LOG4CXX_STR("LOG4CXX_DEBUG"), LOG4CXX_STR("false")); |
57 | 0 | m_priv->debugEnabled = OptionConverter::toBoolean(log4cxxDebug, false); |
58 | 0 | } |
59 | | |
60 | 0 | LogLog::~LogLog(){} |
61 | | |
62 | | LogLog& LogLog::getInstance() |
63 | 0 | { |
64 | 0 | static WideLife<LogLog> internalLogger; |
65 | |
|
66 | 0 | return internalLogger; |
67 | 0 | } |
68 | | |
69 | | bool LogLog::isDebugEnabled() |
70 | 0 | { |
71 | 0 | auto p = getInstance().m_priv.get(); |
72 | 0 | return p && !p->quietMode // Not deleted by onexit processing? |
73 | 0 | && p->debugEnabled; |
74 | 0 | } |
75 | | |
76 | | void LogLog::setInternalDebugging(bool debugEnabled1) |
77 | 0 | { |
78 | 0 | auto p = getInstance().m_priv.get(); |
79 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
80 | 0 | p->debugEnabled = debugEnabled1; |
81 | 0 | } |
82 | | |
83 | | void LogLog::debug(const LogString& msg) |
84 | 0 | { |
85 | 0 | auto p = getInstance().m_priv.get(); |
86 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
87 | 0 | { |
88 | 0 | if (!p->debugEnabled) |
89 | 0 | { |
90 | 0 | return; |
91 | 0 | } |
92 | | |
93 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
94 | |
|
95 | 0 | emit_log(msg); |
96 | 0 | } |
97 | 0 | } |
98 | | |
99 | | void LogLog::debug(const LogString& msg, const std::exception& e) |
100 | 0 | { |
101 | 0 | auto p = getInstance().m_priv.get(); |
102 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
103 | 0 | { |
104 | 0 | if (!p->debugEnabled) |
105 | 0 | return; |
106 | | |
107 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
108 | 0 | emit_log(msg); |
109 | 0 | emit_log(e); |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | | |
114 | | void LogLog::error(const LogString& msg) |
115 | 0 | { |
116 | 0 | auto p = getInstance().m_priv.get(); |
117 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
118 | 0 | { |
119 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
120 | |
|
121 | 0 | emit_log(msg); |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | | void LogLog::error(const LogString& msg, const std::exception& e) |
126 | 0 | { |
127 | 0 | auto p = getInstance().m_priv.get(); |
128 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
129 | 0 | { |
130 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
131 | 0 | emit_log(msg); |
132 | 0 | emit_log(e); |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | | void LogLog::setQuietMode(bool quietMode1) |
137 | 0 | { |
138 | 0 | auto p = getInstance().m_priv.get(); |
139 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
140 | |
|
141 | 0 | p->quietMode = quietMode1; |
142 | 0 | } |
143 | | |
144 | | void LogLog::warn(const LogString& msg) |
145 | 0 | { |
146 | 0 | auto p = getInstance().m_priv.get(); |
147 | 0 | if (p && !p->quietMode) // Not deleted by onexit processing? |
148 | 0 | { |
149 | 0 | std::lock_guard<std::mutex> lock(p->mutex); |
150 | 0 | emit_log(msg); |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | void LogLog::warn(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 | std::lock_guard<std::mutex> lock(p->mutex); |
160 | 0 | emit_log(msg); |
161 | 0 | emit_log(e); |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | | void LogLog::emit_log(const LogString& msg) |
166 | 0 | { |
167 | 0 | LogString out(LOG4CXX_STR("log4cxx: ")); |
168 | |
|
169 | 0 | out.append(msg); |
170 | 0 | out.append(1, (logchar) 0x0A); |
171 | |
|
172 | 0 | SystemErrWriter::write(out); |
173 | 0 | } |
174 | | |
175 | | void LogLog::emit_log(const std::exception& ex) |
176 | 0 | { |
177 | 0 | LogString out(LOG4CXX_STR("log4cxx: ")); |
178 | 0 | const char* raw = ex.what(); |
179 | |
|
180 | 0 | if (raw != 0) |
181 | 0 | { |
182 | 0 | Transcoder::decode(raw, out); |
183 | 0 | } |
184 | 0 | else |
185 | 0 | { |
186 | 0 | out.append(LOG4CXX_STR("std::exception::what() == null")); |
187 | 0 | } |
188 | |
|
189 | 0 | out.append(1, (logchar) 0x0A); |
190 | |
|
191 | 0 | SystemErrWriter::write(out); |
192 | 0 | } |