Coverage Report

Created: 2026-05-30 06:57

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( LOG4CXX_FORMAT_EVENT_FORMAL_PARAMETERS ) const
46
0
{
47
0
  size_t startIndex = toAppendTo.size();
48
0
  auto& info = getFormattingInfo();
49
0
  if (m_priv->name.empty()) // Full MDC required?
50
0
  {
51
0
    const size_t separCount = 2;
52
0
    const size_t quoteCount = 2;
53
0
    LogString separ = LOG4CXX_STR("{");
54
0
    size_t remainingLength = info.getMaxLength() - 1;
55
0
    for (auto key : event->getMDCKeySet())
56
0
    {
57
0
      LogString value;
58
0
      event->getMDC(key, value);
59
0
      auto itemLength = separCount + key.length() + quoteCount + value.length() + quoteCount;
60
0
      if (remainingLength < itemLength)
61
0
        break;
62
0
      toAppendTo.append(separ);
63
0
      JSONLayout::appendItem(key, toAppendTo);
64
0
      toAppendTo.append(LOG4CXX_STR(":"));
65
0
      JSONLayout::appendItem(value, toAppendTo);
66
0
      separ = LOG4CXX_STR(",");
67
0
      remainingLength -= itemLength;
68
0
    }
69
0
    if (LOG4CXX_STR(",") == separ)
70
0
      toAppendTo.append(LOG4CXX_STR("}"));
71
0
  }
72
0
  else
73
0
  {
74
0
    LogString value;
75
0
    event->getMDC(m_priv->name, value);
76
0
    if (info.getMaxLength() < value.length())
77
0
      toAppendTo.append(value.substr(value.length() - info.getMaxLength()));
78
0
    else
79
0
      toAppendTo.append(value);
80
0
  }
81
0
  if (!m_priv->style.empty()) // In a quoted context?
82
0
  {
83
0
    auto quote = m_priv->style.front();
84
0
    size_t endIndex;
85
0
    while ((endIndex = toAppendTo.find(quote, startIndex)) != toAppendTo.npos)
86
0
    {
87
0
      toAppendTo.insert(endIndex + 1, 1, quote);
88
0
      startIndex = endIndex + 2;
89
0
    }
90
0
  }
91
0
}