Coverage Report

Created: 2026-12-01 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/levelpatternconverter.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/pattern/levelpatternconverter.h>
20
#include <log4cxx/spi/loggingevent.h>
21
#include <log4cxx/spi/location/locationinfo.h>
22
#include <log4cxx/level.h>
23
#include <log4cxx/helpers/loglog.h>
24
#include <log4cxx/helpers/optionconverter.h>
25
#include <log4cxx/helpers/stringhelper.h>
26
#include <log4cxx/helpers/stringtokenizer.h>
27
#include <map>
28
29
30
using namespace LOG4CXX_NS;
31
using namespace LOG4CXX_NS::pattern;
32
using namespace LOG4CXX_NS::spi;
33
using namespace LOG4CXX_NS::helpers;
34
35
IMPLEMENT_LOG4CXX_OBJECT(LevelPatternConverter)
36
37
namespace {
38
/**
39
 * Formats the level of a logging event using the labels,
40
 * maximum length and letter case requested in the conversion pattern
41
 */
42
class LevelLabelPatternConverter : public LevelPatternConverter
43
{
44
  public:
45
    /// The label to output for a level value
46
    using LabelMap = std::map<int, LogString>;
47
48
  private:
49
    const LabelMap m_labels;
50
    const size_t m_maxLength;
51
    const bool m_lowerCase;
52
53
  public:
54
    LevelLabelPatternConverter(const LabelMap& labels, size_t maxLength, bool lowerCase)
55
2.02k
      : m_labels(labels)
56
2.02k
      , m_maxLength(maxLength)
57
2.02k
      , m_lowerCase(lowerCase)
58
2.02k
      {}
levelpatternconverter.cpp:(anonymous namespace)::LevelLabelPatternConverter::LevelLabelPatternConverter(std::__1::map<int, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> > > > > const&, unsigned long, bool)
Line
Count
Source
55
2.02k
      : m_labels(labels)
56
2.02k
      , m_maxLength(maxLength)
57
2.02k
      , m_lowerCase(lowerCase)
58
2.02k
      {}
levelpatternconverter.cpp:(anonymous namespace)::LevelLabelPatternConverter::LevelLabelPatternConverter(std::__1::map<int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<int>, std::__1::allocator<std::__1::pair<int const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > > const&, unsigned long, bool)
Line
Count
Source
55
3
      : m_labels(labels)
56
3
      , m_maxLength(maxLength)
57
3
      , m_lowerCase(lowerCase)
58
3
      {}
59
60
    using LevelPatternConverter::format;
61
62
    // Output the label provided for the event's level, otherwise the (shortened) level name
63
    void format( const spi::LoggingEventPtr& event, LogString& toAppendTo ) const override
64
3
    {
65
3
      auto& level = event->getLevel();
66
3
      auto pLabel = m_labels.find(level->toInt());
67
3
      if (m_labels.end() != pLabel)
68
0
      {
69
0
        toAppendTo.append(pLabel->second);
70
0
        return;
71
0
      }
72
3
      auto name = level->toString();
73
3
      if (m_maxLength < name.length())
74
0
        name.erase(m_maxLength);
75
3
      if (m_lowerCase)
76
0
        name = StringHelper::toLowerCase(name);
77
3
      toAppendTo.append(name);
78
3
    }
Unexecuted instantiation: levelpatternconverter.cpp:(anonymous namespace)::LevelLabelPatternConverter::format(std::__1::shared_ptr<log4cxx::spi::LoggingEvent> const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) const
levelpatternconverter.cpp:(anonymous namespace)::LevelLabelPatternConverter::format(std::__1::shared_ptr<log4cxx::spi::LoggingEvent> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const
Line
Count
Source
64
3
    {
65
3
      auto& level = event->getLevel();
66
3
      auto pLabel = m_labels.find(level->toInt());
67
3
      if (m_labels.end() != pLabel)
68
0
      {
69
0
        toAppendTo.append(pLabel->second);
70
0
        return;
71
0
      }
72
3
      auto name = level->toString();
73
3
      if (m_maxLength < name.length())
74
0
        name.erase(m_maxLength);
75
3
      if (m_lowerCase)
76
0
        name = StringHelper::toLowerCase(name);
77
3
      toAppendTo.append(name);
78
3
    }
79
};
80
}
81
82
LevelPatternConverter::LevelPatternConverter() :
83
337k
  LoggingEventPatternConverter(LOG4CXX_STR("Level"),
84
337k
    LOG4CXX_STR("level"))
85
337k
{
86
337k
}
log4cxx::pattern::LevelPatternConverter::LevelPatternConverter()
Line
Count
Source
83
2.02k
  LoggingEventPatternConverter(LOG4CXX_STR("Level"),
84
2.02k
    LOG4CXX_STR("level"))
85
2.02k
{
86
2.02k
}
log4cxx::pattern::LevelPatternConverter::LevelPatternConverter()
Line
Count
Source
83
335k
  LoggingEventPatternConverter(LOG4CXX_STR("Level"),
84
335k
    LOG4CXX_STR("level"))
85
335k
{
86
335k
}
87
88
PatternConverterPtr LevelPatternConverter::newInstance(
89
  const std::vector<LogString>& options)
90
337k
{
91
337k
  if (options.empty() || options.front().empty())
92
326k
  {
93
326k
    return std::make_shared<LevelPatternConverter>();
94
326k
  }
95
96
11.3k
  LevelLabelPatternConverter::LabelMap labels;
97
11.3k
  size_t maxLength = LogString::npos;
98
11.3k
  bool lowerCase = false;
99
11.3k
  StringTokenizer items(options.front(), LOG4CXX_STR(","));
100
41.3k
  while (items.hasMoreTokens())
101
29.9k
  {
102
29.9k
    auto item = items.nextToken();
103
29.9k
    auto separator = item.find(0x3D /* '=' */);
104
29.9k
    if (LogString::npos == separator)
105
13.2k
    {
106
13.2k
      LogLog::warn(LOG4CXX_STR("Ignoring level option [") + item
107
13.2k
        + LOG4CXX_STR("] which is not in the form name=value"));
108
13.2k
      continue;
109
13.2k
    }
110
16.7k
    auto name = StringHelper::trim(item.substr(0, separator));
111
16.7k
    auto value = StringHelper::trim(item.substr(separator + 1));
112
16.7k
    if (StringHelper::equalsIgnoreCase(name, LOG4CXX_STR("LENGTH"), LOG4CXX_STR("length")))
113
0
    {
114
0
      int length = OptionConverter::toInt(value, 0);
115
0
      if (length <= 0)
116
0
        LogLog::warn(LOG4CXX_STR("Ignoring level length [") + value
117
0
          + LOG4CXX_STR("] which is not a positive integer"));
118
0
      else
119
0
        maxLength = static_cast<size_t>(length);
120
0
    }
121
16.7k
    else if (StringHelper::equalsIgnoreCase(name, LOG4CXX_STR("LOWERCASE"), LOG4CXX_STR("lowercase")))
122
0
    {
123
0
      lowerCase = OptionConverter::toBoolean(value, false);
124
0
    }
125
16.7k
    else if (auto level = Level::toLevelLS(name, LevelPtr()))
126
5.47k
    {
127
5.47k
      labels[level->toInt()] = value;
128
5.47k
    }
129
11.2k
    else
130
11.2k
    {
131
11.2k
      LogLog::warn(LOG4CXX_STR("Ignoring label for unknown level [") + name + LOG4CXX_STR("]"));
132
11.2k
    }
133
16.7k
  }
134
135
11.3k
  if (labels.empty() && LogString::npos == maxLength && !lowerCase)
136
9.32k
  {
137
9.32k
    return std::make_shared<LevelPatternConverter>();
138
9.32k
  }
139
2.02k
  return std::make_shared<LevelLabelPatternConverter>(labels, maxLength, lowerCase);
140
11.3k
}
log4cxx::pattern::LevelPatternConverter::newInstance(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&)
Line
Count
Source
90
164k
{
91
164k
  if (options.empty() || options.front().empty())
92
159k
  {
93
159k
    return std::make_shared<LevelPatternConverter>();
94
159k
  }
95
96
4.66k
  LevelLabelPatternConverter::LabelMap labels;
97
4.66k
  size_t maxLength = LogString::npos;
98
4.66k
  bool lowerCase = false;
99
4.66k
  StringTokenizer items(options.front(), LOG4CXX_STR(","));
100
20.3k
  while (items.hasMoreTokens())
101
15.7k
  {
102
15.7k
    auto item = items.nextToken();
103
15.7k
    auto separator = item.find(0x3D /* '=' */);
104
15.7k
    if (LogString::npos == separator)
105
6.87k
    {
106
6.87k
      LogLog::warn(LOG4CXX_STR("Ignoring level option [") + item
107
6.87k
        + LOG4CXX_STR("] which is not in the form name=value"));
108
6.87k
      continue;
109
6.87k
    }
110
8.85k
    auto name = StringHelper::trim(item.substr(0, separator));
111
8.85k
    auto value = StringHelper::trim(item.substr(separator + 1));
112
8.85k
    if (StringHelper::equalsIgnoreCase(name, LOG4CXX_STR("LENGTH"), LOG4CXX_STR("length")))
113
0
    {
114
0
      int length = OptionConverter::toInt(value, 0);
115
0
      if (length <= 0)
116
0
        LogLog::warn(LOG4CXX_STR("Ignoring level length [") + value
117
0
          + LOG4CXX_STR("] which is not a positive integer"));
118
0
      else
119
0
        maxLength = static_cast<size_t>(length);
120
0
    }
121
8.85k
    else if (StringHelper::equalsIgnoreCase(name, LOG4CXX_STR("LOWERCASE"), LOG4CXX_STR("lowercase")))
122
0
    {
123
0
      lowerCase = OptionConverter::toBoolean(value, false);
124
0
    }
125
8.85k
    else if (auto level = Level::toLevelLS(name, LevelPtr()))
126
5.47k
    {
127
5.47k
      labels[level->toInt()] = value;
128
5.47k
    }
129
3.37k
    else
130
3.37k
    {
131
3.37k
      LogLog::warn(LOG4CXX_STR("Ignoring label for unknown level [") + name + LOG4CXX_STR("]"));
132
3.37k
    }
133
8.85k
  }
134
135
4.66k
  if (labels.empty() && LogString::npos == maxLength && !lowerCase)
136
2.64k
  {
137
2.64k
    return std::make_shared<LevelPatternConverter>();
138
2.64k
  }
139
2.02k
  return std::make_shared<LevelLabelPatternConverter>(labels, maxLength, lowerCase);
140
4.66k
}
log4cxx::pattern::LevelPatternConverter::newInstance(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&)
Line
Count
Source
90
173k
{
91
173k
  if (options.empty() || options.front().empty())
92
166k
  {
93
166k
    return std::make_shared<LevelPatternConverter>();
94
166k
  }
95
96
6.68k
  LevelLabelPatternConverter::LabelMap labels;
97
6.68k
  size_t maxLength = LogString::npos;
98
6.68k
  bool lowerCase = false;
99
6.68k
  StringTokenizer items(options.front(), LOG4CXX_STR(","));
100
20.9k
  while (items.hasMoreTokens())
101
14.2k
  {
102
14.2k
    auto item = items.nextToken();
103
14.2k
    auto separator = item.find(0x3D /* '=' */);
104
14.2k
    if (LogString::npos == separator)
105
6.37k
    {
106
6.37k
      LogLog::warn(LOG4CXX_STR("Ignoring level option [") + item
107
6.37k
        + LOG4CXX_STR("] which is not in the form name=value"));
108
6.37k
      continue;
109
6.37k
    }
110
7.87k
    auto name = StringHelper::trim(item.substr(0, separator));
111
7.87k
    auto value = StringHelper::trim(item.substr(separator + 1));
112
7.87k
    if (StringHelper::equalsIgnoreCase(name, LOG4CXX_STR("LENGTH"), LOG4CXX_STR("length")))
113
0
    {
114
0
      int length = OptionConverter::toInt(value, 0);
115
0
      if (length <= 0)
116
0
        LogLog::warn(LOG4CXX_STR("Ignoring level length [") + value
117
0
          + LOG4CXX_STR("] which is not a positive integer"));
118
0
      else
119
0
        maxLength = static_cast<size_t>(length);
120
0
    }
121
7.87k
    else if (StringHelper::equalsIgnoreCase(name, LOG4CXX_STR("LOWERCASE"), LOG4CXX_STR("lowercase")))
122
0
    {
123
0
      lowerCase = OptionConverter::toBoolean(value, false);
124
0
    }
125
7.87k
    else if (auto level = Level::toLevelLS(name, LevelPtr()))
126
3
    {
127
3
      labels[level->toInt()] = value;
128
3
    }
129
7.86k
    else
130
7.86k
    {
131
7.86k
      LogLog::warn(LOG4CXX_STR("Ignoring label for unknown level [") + name + LOG4CXX_STR("]"));
132
7.86k
    }
133
7.87k
  }
134
135
6.68k
  if (labels.empty() && LogString::npos == maxLength && !lowerCase)
136
6.68k
  {
137
6.68k
    return std::make_shared<LevelPatternConverter>();
138
6.68k
  }
139
3
  return std::make_shared<LevelLabelPatternConverter>(labels, maxLength, lowerCase);
140
6.68k
}
141
142
void LevelPatternConverter::format( const spi::LoggingEventPtr& event, LogString& toAppendTo ) const
143
1.65M
{
144
1.65M
  toAppendTo.append(event->getLevel()->toString());
145
1.65M
}
log4cxx::pattern::LevelPatternConverter::format(std::__1::shared_ptr<log4cxx::spi::LoggingEvent> const&, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) const
Line
Count
Source
143
938k
{
144
938k
  toAppendTo.append(event->getLevel()->toString());
145
938k
}
log4cxx::pattern::LevelPatternConverter::format(std::__1::shared_ptr<log4cxx::spi::LoggingEvent> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const
Line
Count
Source
143
715k
{
144
715k
  toAppendTo.append(event->getLevel()->toString());
145
715k
}
146
147
148
/**
149
 * {@inheritDoc}
150
 */
151
LogString LevelPatternConverter::getStyleClass(const ObjectPtr& obj) const
152
0
{
153
0
  LoggingEventPtr e = LOG4CXX_NS::cast<LoggingEvent>(obj);
154
155
0
  if (e != NULL)
156
0
  {
157
0
    int lint = e->getLevel()->toInt();
158
159
0
    switch (lint)
160
0
    {
161
0
      case Level::TRACE_INT:
162
0
        return LOG4CXX_STR("level trace");
163
164
0
      case Level::DEBUG_INT:
165
0
        return LOG4CXX_STR("level debug");
166
167
0
      case Level::INFO_INT:
168
0
        return LOG4CXX_STR("level info");
169
170
0
      case Level::WARN_INT:
171
0
        return LOG4CXX_STR("level warn");
172
173
0
      case Level::ERROR_INT:
174
0
        return LOG4CXX_STR("level error");
175
176
0
      case Level::FATAL_INT:
177
0
        return LOG4CXX_STR("level fatal");
178
179
0
      default:
180
0
        return LogString(LOG4CXX_STR("level ")) + e->getLevel()->toString();
181
0
    }
182
0
  }
183
184
0
  return LOG4CXX_STR("level");
185
0
}
Unexecuted instantiation: log4cxx::pattern::LevelPatternConverter::getStyleClass(std::__1::shared_ptr<log4cxx::helpers::Object> const&) const
Unexecuted instantiation: log4cxx::pattern::LevelPatternConverter::getStyleClass(std::__1::shared_ptr<log4cxx::helpers::Object> const&) const