Coverage Report

Created: 2026-02-26 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/mdcpatternconverter.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/pattern/mdcpatternconverter.h>
19
#include <log4cxx/private/patternconverter_priv.h>
20
#include <log4cxx/spi/loggingevent.h>
21
#include <log4cxx/jsonlayout.h>
22
23
using namespace LOG4CXX_NS;
24
using namespace LOG4CXX_NS::pattern;
25
26
IMPLEMENT_LOG4CXX_OBJECT(MDCPatternConverter)
27
28
MDCPatternConverter::MDCPatternConverter
29
  ( const LogString&              name
30
  , const LogString&              style
31
  , const std::vector<LogString>& options
32
  )
33
0
  : LoggingEventPatternConverter(std::make_unique<PatternConverter::PatternConverterPrivate>(name, style))
34
0
{
35
0
}
Unexecuted instantiation: log4cxx::pattern::MDCPatternConverter::MDCPatternConverter(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::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::pattern::MDCPatternConverter::MDCPatternConverter(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::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&)
36
37
PatternConverterPtr MDCPatternConverter::newInstance(
38
  const std::vector<LogString>& options)
39
0
{
40
0
  if (options.empty())
41
0
    return std::make_shared<MDCPatternConverter>();
42
0
  return std::make_shared<MDCPatternConverter>(LogString(), options.front());
43
0
}
44
45
void MDCPatternConverter::format
46
  ( const spi::LoggingEventPtr& event
47
  , LogString&                  toAppendTo
48
  , helpers::Pool&           /* p */
49
  ) const
50
0
{
51
0
  size_t startIndex = toAppendTo.size();
52
0
  auto& info = getFormattingInfo();
53
0
  if (m_priv->name.empty()) // Full MDC required?
54
0
  {
55
0
    const size_t separCount = 2;
56
0
    const size_t quoteCount = 2;
57
0
    LogString separ = LOG4CXX_STR("{");
58
0
    size_t remainingLength = info.getMaxLength() - 1;
59
0
    for (auto key : event->getMDCKeySet())
60
0
    {
61
0
      LogString value;
62
0
      event->getMDC(key, value);
63
0
      auto itemLength = separCount + key.length() + quoteCount + value.length() + quoteCount;
64
0
      if (remainingLength < itemLength)
65
0
        break;
66
0
      toAppendTo.append(separ);
67
0
      JSONLayout::appendItem(key, toAppendTo);
68
0
      toAppendTo.append(LOG4CXX_STR(":"));
69
0
      JSONLayout::appendItem(value, toAppendTo);
70
0
      separ = LOG4CXX_STR(",");
71
0
      remainingLength -= itemLength;
72
0
    }
73
0
    if (LOG4CXX_STR(",") == separ)
74
0
      toAppendTo.append(LOG4CXX_STR("}"));
75
0
  }
76
0
  else
77
0
  {
78
0
    LogString value;
79
0
    event->getMDC(m_priv->name, value);
80
0
    if (info.getMaxLength() < value.length())
81
0
      toAppendTo.append(value.substr(value.length() - info.getMaxLength()));
82
0
    else
83
0
      toAppendTo.append(value);
84
0
  }
85
0
  if (!m_priv->style.empty()) // In a quoted context?
86
0
  {
87
0
    auto quote = m_priv->style.front();
88
0
    size_t endIndex;
89
0
    while ((endIndex = toAppendTo.find(quote, startIndex)) != toAppendTo.npos)
90
0
    {
91
0
      toAppendTo.insert(endIndex + 1, 1, quote);
92
0
      startIndex = endIndex + 2;
93
0
    }
94
0
  }
95
0
}