Coverage Report

Created: 2026-04-27 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/patternlayout.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/patternlayout.h>
20
#include <log4cxx/pattern/patternparser.h>
21
#include <log4cxx/pattern/loggingeventpatternconverter.h>
22
#include <log4cxx/pattern/formattinginfo.h>
23
#include <log4cxx/helpers/stringhelper.h>
24
#include <log4cxx/helpers/pool.h>
25
#include <log4cxx/helpers/optionconverter.h>
26
27
#include <log4cxx/pattern/loggerpatternconverter.h>
28
#include <log4cxx/pattern/colorendpatternconverter.h>
29
#include <log4cxx/pattern/colorstartpatternconverter.h>
30
#include <log4cxx/pattern/shortfilelocationpatternconverter.h>
31
#include <log4cxx/pattern/literalpatternconverter.h>
32
#include <log4cxx/helpers/loglog.h>
33
#include <log4cxx/pattern/classnamepatternconverter.h>
34
#include <log4cxx/pattern/datepatternconverter.h>
35
#include <log4cxx/pattern/filedatepatternconverter.h>
36
#include <log4cxx/pattern/filelocationpatternconverter.h>
37
#include <log4cxx/pattern/fulllocationpatternconverter.h>
38
#include <log4cxx/pattern/integerpatternconverter.h>
39
#include <log4cxx/pattern/linelocationpatternconverter.h>
40
#include <log4cxx/pattern/messagepatternconverter.h>
41
#include <log4cxx/pattern/lineseparatorpatternconverter.h>
42
#include <log4cxx/pattern/methodlocationpatternconverter.h>
43
#include <log4cxx/pattern/levelpatternconverter.h>
44
#include <log4cxx/pattern/relativetimepatternconverter.h>
45
#include <log4cxx/pattern/threadpatternconverter.h>
46
#include <log4cxx/pattern/mdcpatternconverter.h>
47
#include <log4cxx/pattern/ndcpatternconverter.h>
48
#include <log4cxx/pattern/propertiespatternconverter.h>
49
#include <log4cxx/pattern/throwableinformationpatternconverter.h>
50
#include <log4cxx/pattern/threadusernamepatternconverter.h>
51
52
53
using namespace LOG4CXX_NS;
54
using namespace LOG4CXX_NS::helpers;
55
using namespace LOG4CXX_NS::spi;
56
using namespace LOG4CXX_NS::pattern;
57
58
struct PatternLayout::PatternLayoutPrivate
59
{
60
  PatternLayoutPrivate()
61
2
    : expectedPatternLength(100)
62
2
    {}
63
  PatternLayoutPrivate(const LogString& pattern)
64
449
    : conversionPattern(pattern)
65
449
    , expectedPatternLength(100)
66
449
    {}
log4cxx::PatternLayout::PatternLayoutPrivate::PatternLayoutPrivate(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
64
449
    : conversionPattern(pattern)
65
449
    , expectedPatternLength(100)
66
449
    {}
Unexecuted instantiation: log4cxx::PatternLayout::PatternLayoutPrivate::PatternLayoutPrivate(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
67
68
  /**
69
   * Conversion pattern.
70
   */
71
  LogString conversionPattern;
72
73
  /**
74
   * Pattern converters.
75
   */
76
  LoggingEventPatternConverterList patternConverters;
77
78
  LogString m_fatalColor = LOG4CXX_STR("\\x1B[35m"); //magenta
79
  LogString m_errorColor = LOG4CXX_STR("\\x1B[31m"); //red
80
  LogString m_warnColor = LOG4CXX_STR("\\x1B[33m"); //yellow
81
  LogString m_infoColor = LOG4CXX_STR("\\x1B[32m"); //green
82
  LogString m_debugColor = LOG4CXX_STR("\\x1B[36m"); //cyan;
83
  LogString m_traceColor = LOG4CXX_STR("\\x1B[34m"); //blue;
84
85
  // Expected length of a formatted event excluding the message text
86
  size_t expectedPatternLength;
87
};
88
89
IMPLEMENT_LOG4CXX_OBJECT(PatternLayout)
90
91
92
PatternLayout::PatternLayout() :
93
2
  m_priv(std::make_unique<PatternLayoutPrivate>())
94
2
{
95
2
}
Unexecuted instantiation: log4cxx::PatternLayout::PatternLayout()
log4cxx::PatternLayout::PatternLayout()
Line
Count
Source
93
2
  m_priv(std::make_unique<PatternLayoutPrivate>())
94
2
{
95
2
}
96
97
/**
98
Constructs a PatternLayout using the supplied conversion pattern.
99
*/
100
PatternLayout::PatternLayout(const LogString& pattern) :
101
449
  m_priv(std::make_unique<PatternLayoutPrivate>(pattern))
102
449
{
103
449
  activateOptions();
104
449
}
Unexecuted instantiation: log4cxx::PatternLayout::PatternLayout(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
log4cxx::PatternLayout::PatternLayout(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Line
Count
Source
101
449
  m_priv(std::make_unique<PatternLayoutPrivate>(pattern))
102
449
{
103
449
  activateOptions();
104
449
}
Unexecuted instantiation: log4cxx::PatternLayout::PatternLayout(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
Unexecuted instantiation: log4cxx::PatternLayout::PatternLayout(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
105
106
451
PatternLayout::~PatternLayout() {}
107
108
void PatternLayout::setConversionPattern(const LogString& pattern)
109
0
{
110
0
  m_priv->conversionPattern = pattern;
111
0
  activateOptions();
112
0
}
Unexecuted instantiation: log4cxx::PatternLayout::setConversionPattern(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)
Unexecuted instantiation: log4cxx::PatternLayout::setConversionPattern(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > const&)
113
114
void PatternLayout::format(LogString& output,
115
  const spi::LoggingEventPtr& event,
116
  Pool& pool) const
117
99.7k
{
118
99.7k
  auto& lsMsg = event->getRenderedMessage();
119
99.7k
  output.reserve(m_priv->expectedPatternLength + lsMsg.size());
120
121
99.7k
  for (auto item : m_priv->patternConverters)
122
967k
  {
123
967k
    auto startField = output.length();
124
967k
    item->format(event, output, pool);
125
967k
    if (startField < INT_MAX)
126
967k
      item->getFormattingInfo().format(static_cast<int>(startField), output);
127
967k
  }
128
129
99.7k
}
log4cxx::PatternLayout::format(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, std::__1::shared_ptr<log4cxx::spi::LoggingEvent> const&, log4cxx::helpers::Pool&) const
Line
Count
Source
117
47.4k
{
118
47.4k
  auto& lsMsg = event->getRenderedMessage();
119
47.4k
  output.reserve(m_priv->expectedPatternLength + lsMsg.size());
120
121
47.4k
  for (auto item : m_priv->patternConverters)
122
444k
  {
123
444k
    auto startField = output.length();
124
444k
    item->format(event, output, pool);
125
444k
    if (startField < INT_MAX)
126
444k
      item->getFormattingInfo().format(static_cast<int>(startField), output);
127
444k
  }
128
129
47.4k
}
log4cxx::PatternLayout::format(std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&, std::__1::shared_ptr<log4cxx::spi::LoggingEvent> const&, log4cxx::helpers::Pool&) const
Line
Count
Source
117
52.3k
{
118
52.3k
  auto& lsMsg = event->getRenderedMessage();
119
52.3k
  output.reserve(m_priv->expectedPatternLength + lsMsg.size());
120
121
52.3k
  for (auto item : m_priv->patternConverters)
122
523k
  {
123
523k
    auto startField = output.length();
124
523k
    item->format(event, output, pool);
125
523k
    if (startField < INT_MAX)
126
523k
      item->getFormattingInfo().format(static_cast<int>(startField), output);
127
523k
  }
128
129
52.3k
}
130
131
void PatternLayout::setOption(const LogString& option, const LogString& value)
132
2
{
133
2
  if (StringHelper::equalsIgnoreCase(option,
134
2
      LOG4CXX_STR("CONVERSIONPATTERN"),
135
2
      LOG4CXX_STR("conversionpattern")))
136
2
  {
137
2
    m_priv->conversionPattern = OptionConverter::convertSpecialChars(value);
138
2
  }else if(StringHelper::equalsIgnoreCase(option,
139
0
                      LOG4CXX_STR("ERRORCOLOR"),
140
0
                      LOG4CXX_STR("errorcolor"))){
141
0
    m_priv->m_errorColor = value;
142
0
    if (LogLog::isDebugEnabled())
143
0
    {
144
0
      LogString msg(LOG4CXX_STR("Setting error color to "));
145
0
      msg += value;
146
0
      LogLog::debug(msg);
147
0
    }
148
0
  }else if(StringHelper::equalsIgnoreCase(option,
149
0
                      LOG4CXX_STR("FATALCOLOR"),
150
0
                      LOG4CXX_STR("fatalcolor"))){
151
0
    m_priv->m_fatalColor = value;
152
0
  }else if(StringHelper::equalsIgnoreCase(option,
153
0
                      LOG4CXX_STR("WARNCOLOR"),
154
0
                      LOG4CXX_STR("warncolor"))){
155
0
    m_priv->m_warnColor = value;
156
0
  }else if(StringHelper::equalsIgnoreCase(option,
157
0
                      LOG4CXX_STR("INFOCOLOR"),
158
0
                      LOG4CXX_STR("infocolor"))){
159
0
    m_priv->m_infoColor = value;
160
0
  }else if(StringHelper::equalsIgnoreCase(option,
161
0
                      LOG4CXX_STR("DEBUGCOLOR"),
162
0
                      LOG4CXX_STR("debugcolor"))){
163
0
    m_priv->m_debugColor = value;
164
0
  }else if(StringHelper::equalsIgnoreCase(option,
165
0
                      LOG4CXX_STR("TRACECOLOR"),
166
0
                      LOG4CXX_STR("tracecolor"))){
167
0
    m_priv->m_traceColor = value;
168
0
  }
169
2
}
log4cxx::PatternLayout::setOption(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
132
1
{
133
1
  if (StringHelper::equalsIgnoreCase(option,
134
1
      LOG4CXX_STR("CONVERSIONPATTERN"),
135
1
      LOG4CXX_STR("conversionpattern")))
136
1
  {
137
1
    m_priv->conversionPattern = OptionConverter::convertSpecialChars(value);
138
1
  }else if(StringHelper::equalsIgnoreCase(option,
139
0
                      LOG4CXX_STR("ERRORCOLOR"),
140
0
                      LOG4CXX_STR("errorcolor"))){
141
0
    m_priv->m_errorColor = value;
142
0
    if (LogLog::isDebugEnabled())
143
0
    {
144
0
      LogString msg(LOG4CXX_STR("Setting error color to "));
145
0
      msg += value;
146
0
      LogLog::debug(msg);
147
0
    }
148
0
  }else if(StringHelper::equalsIgnoreCase(option,
149
0
                      LOG4CXX_STR("FATALCOLOR"),
150
0
                      LOG4CXX_STR("fatalcolor"))){
151
0
    m_priv->m_fatalColor = value;
152
0
  }else if(StringHelper::equalsIgnoreCase(option,
153
0
                      LOG4CXX_STR("WARNCOLOR"),
154
0
                      LOG4CXX_STR("warncolor"))){
155
0
    m_priv->m_warnColor = value;
156
0
  }else if(StringHelper::equalsIgnoreCase(option,
157
0
                      LOG4CXX_STR("INFOCOLOR"),
158
0
                      LOG4CXX_STR("infocolor"))){
159
0
    m_priv->m_infoColor = value;
160
0
  }else if(StringHelper::equalsIgnoreCase(option,
161
0
                      LOG4CXX_STR("DEBUGCOLOR"),
162
0
                      LOG4CXX_STR("debugcolor"))){
163
0
    m_priv->m_debugColor = value;
164
0
  }else if(StringHelper::equalsIgnoreCase(option,
165
0
                      LOG4CXX_STR("TRACECOLOR"),
166
0
                      LOG4CXX_STR("tracecolor"))){
167
0
    m_priv->m_traceColor = value;
168
0
  }
169
1
}
log4cxx::PatternLayout::setOption(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
132
1
{
133
1
  if (StringHelper::equalsIgnoreCase(option,
134
1
      LOG4CXX_STR("CONVERSIONPATTERN"),
135
1
      LOG4CXX_STR("conversionpattern")))
136
1
  {
137
1
    m_priv->conversionPattern = OptionConverter::convertSpecialChars(value);
138
1
  }else if(StringHelper::equalsIgnoreCase(option,
139
0
                      LOG4CXX_STR("ERRORCOLOR"),
140
0
                      LOG4CXX_STR("errorcolor"))){
141
0
    m_priv->m_errorColor = value;
142
0
    if (LogLog::isDebugEnabled())
143
0
    {
144
0
      LogString msg(LOG4CXX_STR("Setting error color to "));
145
0
      msg += value;
146
0
      LogLog::debug(msg);
147
0
    }
148
0
  }else if(StringHelper::equalsIgnoreCase(option,
149
0
                      LOG4CXX_STR("FATALCOLOR"),
150
0
                      LOG4CXX_STR("fatalcolor"))){
151
0
    m_priv->m_fatalColor = value;
152
0
  }else if(StringHelper::equalsIgnoreCase(option,
153
0
                      LOG4CXX_STR("WARNCOLOR"),
154
0
                      LOG4CXX_STR("warncolor"))){
155
0
    m_priv->m_warnColor = value;
156
0
  }else if(StringHelper::equalsIgnoreCase(option,
157
0
                      LOG4CXX_STR("INFOCOLOR"),
158
0
                      LOG4CXX_STR("infocolor"))){
159
0
    m_priv->m_infoColor = value;
160
0
  }else if(StringHelper::equalsIgnoreCase(option,
161
0
                      LOG4CXX_STR("DEBUGCOLOR"),
162
0
                      LOG4CXX_STR("debugcolor"))){
163
0
    m_priv->m_debugColor = value;
164
0
  }else if(StringHelper::equalsIgnoreCase(option,
165
0
                      LOG4CXX_STR("TRACECOLOR"),
166
0
                      LOG4CXX_STR("tracecolor"))){
167
0
    m_priv->m_traceColor = value;
168
0
  }
169
1
}
170
171
void PatternLayout::activateOptions( LOG4CXX_ACTIVATE_OPTIONS_FORMAL_PARAMETERS )
172
451
{
173
451
  LogString pat(m_priv->conversionPattern);
174
175
451
  if (pat.empty())
176
0
  {
177
0
    pat = LOG4CXX_STR("%m%n");
178
0
  }
179
180
451
  m_priv->patternConverters.erase(m_priv->patternConverters.begin(), m_priv->patternConverters.end());
181
451
  auto converters = PatternParser::parse(pat, getFormatSpecifiers());
182
183
  //
184
  //   strip out any pattern converters that don't handle LoggingEvents
185
  //
186
  //
187
451
  for (auto const& converterItem : converters)
188
1.81k
  {
189
1.81k
    if (auto eventConverter = LOG4CXX_NS::cast<LoggingEventPatternConverter>(converterItem))
190
1.81k
    {
191
1.81k
      m_priv->patternConverters.push_back(eventConverter);
192
1.81k
    }
193
1.81k
  }
194
451
  m_priv->expectedPatternLength = getFormattedEventCharacterCount() * 2;
195
451
}
196
197
#define RULES_PUT(spec, cls) \
198
14.4k
  specs.insert(PatternMap::value_type(LogString(LOG4CXX_STR(spec)), cls ::newInstance))
199
200
201
LOG4CXX_NS::pattern::PatternMap PatternLayout::getFormatSpecifiers()
202
451
{
203
451
  PatternMap specs;
204
451
  RULES_PUT("c", LoggerPatternConverter);
205
451
  RULES_PUT("logger", LoggerPatternConverter);
206
207
451
  RULES_PUT("C", ClassNamePatternConverter);
208
451
  RULES_PUT("class", ClassNamePatternConverter);
209
210
451
  specs.insert(PatternMap::value_type(LogString(LOG4CXX_STR("Y")), std::bind(&PatternLayout::createColorStartPatternConverter, this, std::placeholders::_1)));
211
451
  RULES_PUT("y", ColorEndPatternConverter);
212
213
451
  RULES_PUT("d", DatePatternConverter);
214
451
  RULES_PUT("date", DatePatternConverter);
215
216
451
  RULES_PUT("f", ShortFileLocationPatternConverter);
217
218
451
  RULES_PUT("F", FileLocationPatternConverter);
219
451
  RULES_PUT("file", FileLocationPatternConverter);
220
221
451
  RULES_PUT("l", FullLocationPatternConverter);
222
223
451
  RULES_PUT("L", LineLocationPatternConverter);
224
451
  RULES_PUT("line", LineLocationPatternConverter);
225
226
451
  RULES_PUT("m", MessagePatternConverter);
227
451
  RULES_PUT("message", MessagePatternConverter);
228
229
451
  RULES_PUT("n", LineSeparatorPatternConverter);
230
231
451
  RULES_PUT("M", MethodLocationPatternConverter);
232
451
  RULES_PUT("method", MethodLocationPatternConverter);
233
234
451
  RULES_PUT("p", LevelPatternConverter);
235
451
  RULES_PUT("level", LevelPatternConverter);
236
237
451
  RULES_PUT("r", RelativeTimePatternConverter);
238
451
  RULES_PUT("relative", RelativeTimePatternConverter);
239
240
451
  RULES_PUT("t", ThreadPatternConverter);
241
451
  RULES_PUT("thread", ThreadPatternConverter);
242
243
451
  RULES_PUT("T", ThreadUsernamePatternConverter);
244
451
  RULES_PUT("threadname", ThreadUsernamePatternConverter);
245
246
451
  RULES_PUT("x", NDCPatternConverter);
247
451
  RULES_PUT("ndc", NDCPatternConverter);
248
249
451
  RULES_PUT("X", PropertiesPatternConverter);
250
451
  RULES_PUT("J", MDCPatternConverter);
251
451
  RULES_PUT("properties", PropertiesPatternConverter);
252
253
451
  RULES_PUT("throwable", ThrowableInformationPatternConverter);
254
451
  return specs;
255
451
}
256
257
LogString PatternLayout::getConversionPattern() const
258
0
{
259
0
  return m_priv->conversionPattern;
260
0
}
261
262
0
pattern::PatternConverterPtr PatternLayout::createColorStartPatternConverter(const std::vector<LogString>& options){
263
0
  std::shared_ptr<ColorStartPatternConverter> colorPatternConverter = std::make_shared<ColorStartPatternConverter>();
264
265
0
  colorPatternConverter->setErrorColor(m_priv->m_errorColor);
266
0
  colorPatternConverter->setFatalColor(m_priv->m_fatalColor);
267
0
  colorPatternConverter->setWarnColor(m_priv->m_warnColor);
268
0
  colorPatternConverter->setInfoColor(m_priv->m_infoColor);
269
0
  colorPatternConverter->setDebugColor(m_priv->m_debugColor);
270
0
  colorPatternConverter->setTraceColor(m_priv->m_traceColor);
271
272
0
  return colorPatternConverter;
273
0
}
Unexecuted instantiation: log4cxx::PatternLayout::createColorStartPatternConverter(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&)
Unexecuted instantiation: log4cxx::PatternLayout::createColorStartPatternConverter(std::__1::vector<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >, std::__1::allocator<std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > > > const&)
274
275
276