Coverage Report

Created: 2025-07-01 06:08

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