Coverage Report

Created: 2026-06-15 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/rollingpolicybase.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/rolling/rollingpolicybase.h>
20
#include <log4cxx/pattern/formattinginfo.h>
21
#include <log4cxx/helpers/stringhelper.h>
22
#include <log4cxx/helpers/loglog.h>
23
#include <log4cxx/helpers/exception.h>
24
#include <log4cxx/pattern/patternparser.h>
25
#include <log4cxx/pattern/integerpatternconverter.h>
26
#include <log4cxx/pattern/datepatternconverter.h>
27
#include <log4cxx/helpers/optionconverter.h>
28
#include <log4cxx/private/rollingpolicybase_priv.h>
29
30
using namespace LOG4CXX_NS;
31
using namespace LOG4CXX_NS::rolling;
32
using namespace LOG4CXX_NS::helpers;
33
using namespace LOG4CXX_NS::pattern;
34
35
IMPLEMENT_LOG4CXX_OBJECT(RollingPolicyBase)
36
37
RollingPolicyBase::RollingPolicyBase() :
38
912
  m_priv(std::make_unique<RollingPolicyBasePrivate>())
39
912
{
40
912
}
41
42
RollingPolicyBase::RollingPolicyBase( std::unique_ptr<RollingPolicyBasePrivate> priv ) :
43
0
  m_priv(std::move(priv)){
44
0
}
45
46
RollingPolicyBase::~RollingPolicyBase()
47
912
{
48
912
}
49
50
void RollingPolicyBase::activateOptions( LOG4CXX_ACTIVATE_OPTIONS_FORMAL_PARAMETERS  )
51
0
{
52
0
  m_priv->activateOptions(getFormatSpecifiers());
53
0
}
54
55
void RollingPolicyBase::RollingPolicyBasePrivate::activateOptions(const pattern::PatternMap& map)
56
0
{
57
0
  if (this->fileNamePatternStr.length() > 0)
58
0
  {
59
0
    this->parseFileNamePattern(map);
60
0
  }
61
0
  else
62
0
  {
63
0
    LogString msg(LOG4CXX_STR("The FileNamePattern option must be set before using FixedWindowRollingPolicy."));
64
0
    LogString ref1(LOG4CXX_STR("See also http://logging.apache.org/log4j/codes.html#tbr_fnp_not_set"));
65
0
    LogLog::warn(msg);
66
0
    LogLog::warn(ref1);
67
0
    throw IllegalStateException();
68
0
  }
69
0
}
70
71
72
void RollingPolicyBase::setOption(const LogString& option, const LogString& value)
73
0
{
74
0
  if (StringHelper::equalsIgnoreCase(option,
75
0
      LOG4CXX_STR("FILENAMEPATTERN"),
76
0
      LOG4CXX_STR("filenamepattern")))
77
0
  {
78
0
    m_priv->fileNamePatternStr = value;
79
0
  }else if (StringHelper::equalsIgnoreCase(option,
80
0
      LOG4CXX_STR("CREATEINTERMEDIATEDIRECTORIES"),
81
0
      LOG4CXX_STR("createintermediatedirectories")))
82
0
  {
83
0
    m_priv->createIntermediateDirectories = OptionConverter::toBoolean(value, false);
84
0
  }
85
0
}
86
87
void RollingPolicyBase::setFileNamePattern(const LogString& fnp)
88
912
{
89
912
  m_priv->fileNamePatternStr = fnp;
90
912
}
91
92
93
LogString RollingPolicyBase::getFileNamePattern() const
94
4.56k
{
95
4.56k
  return m_priv->fileNamePatternStr;
96
4.56k
}
97
98
/**
99
 *   Parse file name pattern.
100
 */
101
void RollingPolicyBase::parseFileNamePattern()
102
4.56k
{
103
4.56k
  m_priv->parseFileNamePattern(getFormatSpecifiers());
104
4.56k
}
105
106
void RollingPolicyBase::RollingPolicyBasePrivate::parseFileNamePattern(const pattern::PatternMap& map)
107
4.56k
{
108
4.56k
  this->patternConverters = PatternParser::parse(this->fileNamePatternStr, map);
109
4.56k
}
110
111
/**
112
 * Format file name.
113
 *
114
 * @param obj object to be evaluted in formatting, may not be null.
115
 * @param buf string buffer to which formatted file name is appended, may not be null.
116
 */
117
void RollingPolicyBase::formatFileName(
118
  const ObjectPtr& obj,
119
  LogString& toAppendTo) const
120
8.21k
{
121
8.21k
  for (auto item : m_priv->patternConverters)
122
24.6k
  {
123
24.6k
    auto startField = toAppendTo.length();
124
24.6k
    item->format(obj, toAppendTo);
125
24.6k
    item->getFormattingInfo().adjustField(startField, toAppendTo);
126
24.6k
  }
127
8.21k
}
128
#if LOG4CXX_ABI_VERSION <= 15
129
void RollingPolicyBase::formatFileName(
130
  const ObjectPtr& obj,
131
  LogString& toAppendTo,
132
  Pool& pool) const
133
0
{
134
0
  formatFileName(obj, toAppendTo);
135
0
}
136
#endif
137
138
PatternConverterPtr RollingPolicyBase::getIntegerPatternConverter() const
139
0
{
140
0
  for (std::vector<PatternConverterPtr>::const_iterator
141
0
    converterIter = m_priv->patternConverters.begin();
142
0
    converterIter != m_priv->patternConverters.end();
143
0
    converterIter++)
144
0
  {
145
0
    IntegerPatternConverterPtr intPattern;
146
0
    PatternConverterPtr patternptr = (*converterIter);
147
0
    intPattern = LOG4CXX_NS::cast<IntegerPatternConverter>(patternptr);
148
149
0
    if (intPattern != NULL)
150
0
    {
151
0
      return *converterIter;
152
0
    }
153
0
  }
154
155
0
  PatternConverterPtr noMatch;
156
0
  return noMatch;
157
0
}
158
159
PatternConverterPtr RollingPolicyBase::getDatePatternConverter() const
160
4.56k
{
161
4.56k
  for (std::vector<PatternConverterPtr>::const_iterator
162
4.56k
    converterIter = m_priv->patternConverters.begin();
163
9.12k
    converterIter != m_priv->patternConverters.end();
164
4.56k
    converterIter++)
165
9.12k
  {
166
9.12k
    DatePatternConverterPtr datePattern;
167
9.12k
    PatternConverterPtr patternptr = (*converterIter);
168
9.12k
    datePattern = LOG4CXX_NS::cast<DatePatternConverter>(patternptr);
169
170
9.12k
    if (datePattern != NULL)
171
4.56k
    {
172
4.56k
      return *converterIter;
173
4.56k
    }
174
9.12k
  }
175
176
0
  PatternConverterPtr noMatch;
177
0
  return noMatch;
178
4.56k
}
179
180
7
bool RollingPolicyBase::getCreateIntermediateDirectories() const{
181
7
  return m_priv->createIntermediateDirectories;
182
7
}
183
184
0
void RollingPolicyBase::setCreateIntermediateDirectories(bool createIntermediate){
185
0
  m_priv->createIntermediateDirectories = createIntermediate;
186
0
}
187
188
PatternConverterList RollingPolicyBase::getPatternConverterList() const
189
0
{
190
0
  return m_priv->patternConverters;
191
0
}