Coverage Report

Created: 2026-01-25 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/formattinginfo.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/formattinginfo.h>
20
#include <limits.h>
21
22
using namespace LOG4CXX_NS;
23
using namespace LOG4CXX_NS::pattern;
24
25
struct FormattingInfo::FormattingInfoPrivate
26
{
27
  FormattingInfoPrivate(const bool leftAlign1, const int minLength1, const int maxLength1):
28
83.4k
    minLength(minLength1),
29
83.4k
    maxLength(maxLength1),
30
83.4k
    leftAlign(leftAlign1) {}
31
32
  /**
33
   * Minimum length.
34
   */
35
  const int minLength;
36
37
  /**
38
   * Maximum length.
39
   */
40
  const int maxLength;
41
42
  /**
43
   * Alignment.
44
   */
45
  const bool leftAlign;
46
};
47
48
IMPLEMENT_LOG4CXX_OBJECT(FormattingInfo)
49
50
/**
51
 * Creates new instance.
52
 * @param leftAlign left align if true.
53
 * @param minLength minimum length.
54
 * @param maxLength maximum length.
55
 */
56
FormattingInfo::FormattingInfo(
57
  const bool leftAlign1, const int minLength1, const int maxLength1) :
58
83.4k
  m_priv(std::make_unique<FormattingInfoPrivate>(leftAlign1, minLength1, maxLength1))
59
83.4k
{
60
83.4k
}
Unexecuted instantiation: log4cxx::pattern::FormattingInfo::FormattingInfo(bool, int, int)
log4cxx::pattern::FormattingInfo::FormattingInfo(bool, int, int)
Line
Count
Source
58
83.4k
  m_priv(std::make_unique<FormattingInfoPrivate>(leftAlign1, minLength1, maxLength1))
59
83.4k
{
60
83.4k
}
61
62
83.4k
FormattingInfo::~FormattingInfo() {}
63
64
/**
65
 * Gets default instance.
66
 * @return default instance.
67
 */
68
FormattingInfoPtr FormattingInfo::getDefault()
69
610k
{
70
610k
  static helpers::WideLife<FormattingInfoPtr> def= std::make_shared<FormattingInfo>(false, 0, INT_MAX);
71
610k
  return def;
72
610k
}
73
74
/**
75
 * Adjust the content of the buffer based on the specified lengths and alignment.
76
 *
77
 * @param fieldStart start of field in buffer.
78
 * @param buffer buffer to be modified.
79
 */
80
void FormattingInfo::format(const int fieldStart, LogString& buffer) const
81
871k
{
82
871k
  int rawLength = int(buffer.length() - fieldStart);
83
84
871k
  if (rawLength > m_priv->maxLength)
85
0
  {
86
0
    buffer.erase(buffer.begin() + fieldStart,
87
0
      buffer.begin() + fieldStart + (rawLength - m_priv->maxLength));
88
0
  }
89
871k
  else if (rawLength < m_priv->minLength)
90
84.3k
  {
91
84.3k
    if (m_priv->leftAlign)
92
0
    {
93
0
      buffer.append(m_priv->minLength - rawLength, (logchar) 0x20 /* ' ' */);
94
0
    }
95
84.3k
    else
96
84.3k
    {
97
84.3k
      buffer.insert(fieldStart, m_priv->minLength - rawLength, 0x20 /* ' ' */);
98
84.3k
    }
99
84.3k
  }
100
871k
}
log4cxx::pattern::FormattingInfo::format(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) const
Line
Count
Source
81
485k
{
82
485k
  int rawLength = int(buffer.length() - fieldStart);
83
84
485k
  if (rawLength > m_priv->maxLength)
85
0
  {
86
0
    buffer.erase(buffer.begin() + fieldStart,
87
0
      buffer.begin() + fieldStart + (rawLength - m_priv->maxLength));
88
0
  }
89
485k
  else if (rawLength < m_priv->minLength)
90
45.7k
  {
91
45.7k
    if (m_priv->leftAlign)
92
0
    {
93
0
      buffer.append(m_priv->minLength - rawLength, (logchar) 0x20 /* ' ' */);
94
0
    }
95
45.7k
    else
96
45.7k
    {
97
45.7k
      buffer.insert(fieldStart, m_priv->minLength - rawLength, 0x20 /* ' ' */);
98
45.7k
    }
99
45.7k
  }
100
485k
}
log4cxx::pattern::FormattingInfo::format(int, std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >&) const
Line
Count
Source
81
386k
{
82
386k
  int rawLength = int(buffer.length() - fieldStart);
83
84
386k
  if (rawLength > m_priv->maxLength)
85
0
  {
86
0
    buffer.erase(buffer.begin() + fieldStart,
87
0
      buffer.begin() + fieldStart + (rawLength - m_priv->maxLength));
88
0
  }
89
386k
  else if (rawLength < m_priv->minLength)
90
38.6k
  {
91
38.6k
    if (m_priv->leftAlign)
92
0
    {
93
0
      buffer.append(m_priv->minLength - rawLength, (logchar) 0x20 /* ' ' */);
94
0
    }
95
38.6k
    else
96
38.6k
    {
97
38.6k
      buffer.insert(fieldStart, m_priv->minLength - rawLength, 0x20 /* ' ' */);
98
38.6k
    }
99
38.6k
  }
100
386k
}
101
102
bool FormattingInfo::isLeftAligned() const
103
15.4k
{
104
15.4k
  return m_priv->leftAlign;
105
15.4k
}
106
107
int FormattingInfo::getMinLength() const
108
75.7k
{
109
75.7k
  return m_priv->minLength;
110
75.7k
}
111
112
int FormattingInfo::getMaxLength() const
113
80.0k
{
114
80.0k
  return m_priv->maxLength;
115
80.0k
}