Coverage Report

Created: 2026-08-31 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
8
    debugEnabled(false),
40
8
    quietMode(false){}
41
42
  ~LogLogPrivate()
43
8
  {
44
8
    quietMode = true; // Prevent output after deletion by onexit processing chain.
45
8
  }
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
122
  {
60
122
    if (newValue)
61
118
    {
62
118
      this->errorPrefix = LOG4CXX_STR("\x1B[31m"); //red
63
118
      this->warnPrefix = LOG4CXX_STR("\x1B[33m"); //yellow
64
118
      this->debugPrefix = LOG4CXX_STR("\x1B[32m"); //green
65
118
      this->suffix = LOG4CXX_STR("\x1B[0m"); // none
66
118
    }
67
4
    else
68
4
    {
69
4
      this->errorPrefix.clear();
70
4
      this->warnPrefix.clear();
71
4
      this->debugPrefix.clear();
72
4
      this->suffix.clear();
73
4
    }
74
122
  }
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
8
  m_priv(std::make_unique<LogLogPrivate>())
86
8
{
87
8
  LogString log4cxxDebug = OptionConverter::getSystemProperty(LOG4CXX_STR("LOG4CXX_DEBUG"), LOG4CXX_STR("false"));
88
8
  m_priv->debugEnabled = OptionConverter::toBoolean(log4cxxDebug, false);
89
8
  auto color = OptionConverter::getSystemProperty(LOG4CXX_STR("LOG4CXX_COLOR"), LOG4CXX_STR("true"));
90
8
  m_priv->setColorEnabled(OptionConverter::toBoolean(color, true));
91
8
}
92
93
LogLog::~LogLog()
94
8
{ m_priv.reset(); }
95
96
LogLog& LogLog::getInstance()
97
142k
{
98
142k
  static WideLife<LogLog> internalLogger;
99
100
142k
  return internalLogger;
101
142k
}
102
103
bool LogLog::isDebugEnabled()
104
43.3k
{
105
43.3k
  auto p = getInstance().m_priv.get();
106
43.3k
  return p && !p->quietMode // Not deleted by onexit processing?
107
43.3k
       && p->debugEnabled;
108
43.3k
}
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
470
{
122
470
  auto p = getInstance().m_priv.get();
123
470
  if (p && !p->quietMode) // Not deleted by onexit processing?
124
470
    p->debugEnabled = debugEnabled1;
125
470
}
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
114
{
135
114
  if (auto p = getInstance().m_priv.get())
136
114
    p->setColorEnabled(newValue);
137
114
}
138
139
void LogLog::debug(const LogString& msg)
140
36.0k
{
141
36.0k
  auto p = getInstance().m_priv.get();
142
36.0k
  if (p && !p->quietMode) // Not deleted by onexit processing?
143
36.0k
  {
144
36.0k
    if (!p->debugEnabled)
145
8
    {
146
8
      return;
147
8
    }
148
149
36.0k
    std::lock_guard<std::mutex> lock(p->mutex);
150
36.0k
    emit_log(p->debugPrefix, msg, p->suffix);
151
36.0k
  }
152
36.0k
}
log4cxx::helpers::LogLog::debug(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
140
33.6k
{
141
33.6k
  auto p = getInstance().m_priv.get();
142
33.6k
  if (p && !p->quietMode) // Not deleted by onexit processing?
143
33.6k
  {
144
33.6k
    if (!p->debugEnabled)
145
4
    {
146
4
      return;
147
4
    }
148
149
33.6k
    std::lock_guard<std::mutex> lock(p->mutex);
150
33.6k
    emit_log(p->debugPrefix, msg, p->suffix);
151
33.6k
  }
152
33.6k
}
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
140
2.41k
{
141
2.41k
  auto p = getInstance().m_priv.get();
142
2.41k
  if (p && !p->quietMode) // Not deleted by onexit processing?
143
2.41k
  {
144
2.41k
    if (!p->debugEnabled)
145
4
    {
146
4
      return;
147
4
    }
148
149
2.40k
    std::lock_guard<std::mutex> lock(p->mutex);
150
2.40k
    emit_log(p->debugPrefix, msg, p->suffix);
151
2.40k
  }
152
2.41k
}
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
}
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&)
167
168
169
void LogLog::error(const LogString& msg)
170
38.0k
{
171
38.0k
  auto p = getInstance().m_priv.get();
172
38.0k
  if (p && !p->quietMode) // Not deleted by onexit processing?
173
38.0k
  {
174
38.0k
    std::lock_guard<std::mutex> lock(p->mutex);
175
176
38.0k
    emit_log(p->errorPrefix, msg, p->suffix);
177
38.0k
  }
178
38.0k
}
log4cxx::helpers::LogLog::error(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
170
21.3k
{
171
21.3k
  auto p = getInstance().m_priv.get();
172
21.3k
  if (p && !p->quietMode) // Not deleted by onexit processing?
173
21.3k
  {
174
21.3k
    std::lock_guard<std::mutex> lock(p->mutex);
175
176
21.3k
    emit_log(p->errorPrefix, msg, p->suffix);
177
21.3k
  }
178
21.3k
}
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
170
16.6k
{
171
16.6k
  auto p = getInstance().m_priv.get();
172
16.6k
  if (p && !p->quietMode) // Not deleted by onexit processing?
173
16.6k
  {
174
16.6k
    std::lock_guard<std::mutex> lock(p->mutex);
175
176
16.6k
    emit_log(p->errorPrefix, msg, p->suffix);
177
16.6k
  }
178
16.6k
}
179
180
void LogLog::error(const LogString& msg, const std::exception& e)
181
3.21k
{
182
3.21k
  auto p = getInstance().m_priv.get();
183
3.21k
  if (p && !p->quietMode) // Not deleted by onexit processing?
184
3.21k
  {
185
3.21k
    std::lock_guard<std::mutex> lock(p->mutex);
186
3.21k
    emit_log(p->errorPrefix, msg, p->suffix);
187
3.21k
    emit_log(p->errorPrefix, e, p->suffix);
188
3.21k
  }
189
3.21k
}
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
181
1.73k
{
182
1.73k
  auto p = getInstance().m_priv.get();
183
1.73k
  if (p && !p->quietMode) // Not deleted by onexit processing?
184
1.73k
  {
185
1.73k
    std::lock_guard<std::mutex> lock(p->mutex);
186
1.73k
    emit_log(p->errorPrefix, msg, p->suffix);
187
1.73k
    emit_log(p->errorPrefix, e, p->suffix);
188
1.73k
  }
189
1.73k
}
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
181
1.47k
{
182
1.47k
  auto p = getInstance().m_priv.get();
183
1.47k
  if (p && !p->quietMode) // Not deleted by onexit processing?
184
1.47k
  {
185
1.47k
    std::lock_guard<std::mutex> lock(p->mutex);
186
1.47k
    emit_log(p->errorPrefix, msg, p->suffix);
187
1.47k
    emit_log(p->errorPrefix, e, p->suffix);
188
1.47k
  }
189
1.47k
}
190
191
LoggerPtr LogLog::getLogger(const LogString& name)
192
0
{
193
0
  return LogManager::getLoggerRepository()->getLogger(name);
194
0
}
Unexecuted instantiation: log4cxx::helpers::LogLog::getLogger(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: log4cxx::helpers::LogLog::getLogger(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
195
196
#if !LOG4CXX_LOGCHAR_IS_UTF8
197
void LogLog::trace(const LoggerPtr& category, const std::string& msg)
198
0
{
199
0
  LOG4CXX_DECODE_CHAR(lsMsg, msg);
200
0
  trace(category, lsMsg);
201
0
}
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
}
Unexecuted instantiation: log4cxx::helpers::LogLog::trace(std::__1::shared_ptr<log4cxx::Logger> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: log4cxx::helpers::LogLog::trace(std::__1::shared_ptr<log4cxx::Logger> const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
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
3.03k
{
229
3.03k
  auto p = getInstance().m_priv.get();
230
3.03k
  if (p && !p->quietMode) // Not deleted by onexit processing?
231
3.03k
  {
232
3.03k
    std::lock_guard<std::mutex> lock(p->mutex);
233
3.03k
    emit_log(p->warnPrefix, msg, p->suffix);
234
3.03k
  }
235
3.03k
}
log4cxx::helpers::LogLog::warn(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
228
2.20k
{
229
2.20k
  auto p = getInstance().m_priv.get();
230
2.20k
  if (p && !p->quietMode) // Not deleted by onexit processing?
231
2.20k
  {
232
2.20k
    std::lock_guard<std::mutex> lock(p->mutex);
233
2.20k
    emit_log(p->warnPrefix, msg, p->suffix);
234
2.20k
  }
235
2.20k
}
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
228
828
{
229
828
  auto p = getInstance().m_priv.get();
230
828
  if (p && !p->quietMode) // Not deleted by onexit processing?
231
828
  {
232
828
    std::lock_guard<std::mutex> lock(p->mutex);
233
828
    emit_log(p->warnPrefix, msg, p->suffix);
234
828
  }
235
828
}
236
237
void LogLog::warn(const LogString& msg, const std::exception& e)
238
18.1k
{
239
18.1k
  auto p = getInstance().m_priv.get();
240
18.1k
  if (p && !p->quietMode) // Not deleted by onexit processing?
241
18.1k
  {
242
18.1k
    std::lock_guard<std::mutex> lock(p->mutex);
243
18.1k
    emit_log(p->warnPrefix, msg, p->suffix);
244
18.1k
    emit_log(p->warnPrefix, e, p->suffix);
245
18.1k
  }
246
18.1k
}
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
238
10.4k
{
239
10.4k
  auto p = getInstance().m_priv.get();
240
10.4k
  if (p && !p->quietMode) // Not deleted by onexit processing?
241
10.4k
  {
242
10.4k
    std::lock_guard<std::mutex> lock(p->mutex);
243
10.4k
    emit_log(p->warnPrefix, msg, p->suffix);
244
10.4k
    emit_log(p->warnPrefix, e, p->suffix);
245
10.4k
  }
246
10.4k
}
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
238
7.63k
{
239
7.63k
  auto p = getInstance().m_priv.get();
240
7.63k
  if (p && !p->quietMode) // Not deleted by onexit processing?
241
7.63k
  {
242
7.63k
    std::lock_guard<std::mutex> lock(p->mutex);
243
7.63k
    emit_log(p->warnPrefix, msg, p->suffix);
244
7.63k
    emit_log(p->warnPrefix, e, p->suffix);
245
7.63k
  }
246
7.63k
}
247
248
void LogLog::emit_log(const LogString& prefix, const LogString& msg, const LogString& suffix)
249
98.4k
{
250
98.4k
  LogString out(LOG4CXX_STR("log4cxx: "));
251
98.4k
  out.append(prefix);
252
98.4k
  out.append(msg);
253
98.4k
  out.append(suffix);
254
98.4k
  out.append(1, (logchar) 0x0A);
255
256
98.4k
  SystemErrWriter().write(out);
257
98.4k
}
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
249
69.4k
{
250
69.4k
  LogString out(LOG4CXX_STR("log4cxx: "));
251
69.4k
  out.append(prefix);
252
69.4k
  out.append(msg);
253
69.4k
  out.append(suffix);
254
69.4k
  out.append(1, (logchar) 0x0A);
255
256
69.4k
  SystemErrWriter().write(out);
257
69.4k
}
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
249
29.0k
{
250
29.0k
  LogString out(LOG4CXX_STR("log4cxx: "));
251
29.0k
  out.append(prefix);
252
29.0k
  out.append(msg);
253
29.0k
  out.append(suffix);
254
29.0k
  out.append(1, (logchar) 0x0A);
255
256
29.0k
  SystemErrWriter().write(out);
257
29.0k
}
258
259
void LogLog::emit_log(const LogString& prefix, const std::exception& ex, const LogString& suffix)
260
21.3k
{
261
21.3k
  LogString out(LOG4CXX_STR("log4cxx: "));
262
21.3k
  out.append(prefix);
263
21.3k
  const char* raw = ex.what();
264
265
21.3k
  if (raw != 0)
266
21.3k
  {
267
21.3k
    Transcoder::decode(raw, out);
268
21.3k
  }
269
0
  else
270
0
  {
271
0
    out.append(LOG4CXX_STR("std::exception::what() == null"));
272
0
  }
273
274
21.3k
  out.append(suffix);
275
21.3k
  out.append(1, (logchar) 0x0A);
276
277
21.3k
  SystemErrWriter().write(out);
278
21.3k
}
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
260
12.2k
{
261
12.2k
  LogString out(LOG4CXX_STR("log4cxx: "));
262
12.2k
  out.append(prefix);
263
12.2k
  const char* raw = ex.what();
264
265
12.2k
  if (raw != 0)
266
12.2k
  {
267
12.2k
    Transcoder::decode(raw, out);
268
12.2k
  }
269
0
  else
270
0
  {
271
0
    out.append(LOG4CXX_STR("std::exception::what() == null"));
272
0
  }
273
274
12.2k
  out.append(suffix);
275
12.2k
  out.append(1, (logchar) 0x0A);
276
277
12.2k
  SystemErrWriter().write(out);
278
12.2k
}
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
260
9.10k
{
261
9.10k
  LogString out(LOG4CXX_STR("log4cxx: "));
262
9.10k
  out.append(prefix);
263
9.10k
  const char* raw = ex.what();
264
265
9.10k
  if (raw != 0)
266
9.10k
  {
267
9.10k
    Transcoder::decode(raw, out);
268
9.10k
  }
269
0
  else
270
0
  {
271
0
    out.append(LOG4CXX_STR("std::exception::what() == null"));
272
0
  }
273
274
9.10k
  out.append(suffix);
275
9.10k
  out.append(1, (logchar) 0x0A);
276
277
9.10k
  SystemErrWriter().write(out);
278
9.10k
}