Coverage Report

Created: 2025-07-01 06:08

/src/logging-log4cxx/src/main/cpp/messagepatternconverter.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/pattern/messagepatternconverter.h>
20
#include <log4cxx/spi/loggingevent.h>
21
#include <log4cxx/spi/location/locationinfo.h>
22
23
24
using namespace LOG4CXX_NS;
25
using namespace LOG4CXX_NS::pattern;
26
27
IMPLEMENT_LOG4CXX_OBJECT(MessagePatternConverter)
28
29
namespace {
30
/**
31
 * Formats the message of an logging event for a quoted context
32
  */
33
class QuotedMessagePatternConverter : public LoggingEventPatternConverter
34
{
35
  logchar m_quote;
36
  public:
37
    QuotedMessagePatternConverter(logchar quote)
38
433
      : LoggingEventPatternConverter(LOG4CXX_STR("Message"), LOG4CXX_STR("quoted"))
39
433
      , m_quote(quote)
40
433
      {}
41
42
    using LoggingEventPatternConverter::format;
43
44
    // Duplicate any quote character in the event message
45
    void format
46
      ( const spi::LoggingEventPtr& event
47
      , LogString&                  toAppendTo
48
      , helpers::Pool&              p
49
      ) const override
50
0
    {
51
0
      auto& input = event->getRenderedMessage();
52
0
      size_t endIndex, startIndex = 0;
53
0
      while ((endIndex = input.find(m_quote, startIndex)) != input.npos)
54
0
      {
55
0
        toAppendTo.append(input.substr(startIndex, endIndex - startIndex + 1));
56
0
        toAppendTo += m_quote;
57
0
        startIndex = endIndex + 1;
58
0
      }
59
0
      toAppendTo.append(input.substr(startIndex));
60
0
    }
61
};
62
}
63
64
MessagePatternConverter::MessagePatternConverter()
65
1
  : LoggingEventPatternConverter(LOG4CXX_STR("Message")
66
1
  , LOG4CXX_STR("message"))
67
1
{
68
1
}
Unexecuted instantiation: log4cxx::pattern::MessagePatternConverter::MessagePatternConverter()
log4cxx::pattern::MessagePatternConverter::MessagePatternConverter()
Line
Count
Source
65
1
  : LoggingEventPatternConverter(LOG4CXX_STR("Message")
66
1
  , LOG4CXX_STR("message"))
67
1
{
68
1
}
69
70
PatternConverterPtr MessagePatternConverter::newInstance(
71
  const std::vector<LogString>& options)
72
8.66k
{
73
8.66k
  if (options.empty() || options.front().empty())
74
8.23k
  {
75
8.23k
    static helpers::WideLife<PatternConverterPtr> def = std::make_shared<MessagePatternConverter>();
76
8.23k
    return def;
77
8.23k
  }
78
433
  return std::make_shared<QuotedMessagePatternConverter>(options.front().front());
79
8.66k
}
80
81
void MessagePatternConverter::format
82
  ( const spi::LoggingEventPtr& event
83
  , LogString&                  toAppendTo
84
  , helpers::Pool&           /* p */
85
  ) const
86
0
{
87
0
  toAppendTo.append(event->getRenderedMessage());
88
0
}
89