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/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
0
  m_priv(std::make_unique<RollingPolicyBasePrivate>())
39
0
{
40
0
}
41
42
RollingPolicyBase::RollingPolicyBase( std::unique_ptr<RollingPolicyBasePrivate> priv ) :
43
0
  m_priv(std::move(priv)){
44
0
}
45
46
RollingPolicyBase::~RollingPolicyBase()
47
0
{
48
0
}
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
0
{
84
0
  m_priv->fileNamePatternStr = fnp;
85
0
}
86
87
88
LogString RollingPolicyBase::getFileNamePattern() const
89
0
{
90
0
  return m_priv->fileNamePatternStr;
91
0
}
92
93
/**
94
 *   Parse file name pattern.
95
 */
96
void RollingPolicyBase::parseFileNamePattern()
97
0
{
98
0
  m_priv->patternConverters = PatternParser::parse(m_priv->fileNamePatternStr, getFormatSpecifiers());
99
0
}
100
101
/**
102
 * Format file name.
103
 *
104
 * @param obj object to be evaluted in formatting, may not be null.
105
 * @param buf string buffer to which formatted file name is appended, may not be null.
106
 */
107
void RollingPolicyBase::formatFileName(
108
  const ObjectPtr& obj,
109
  LogString& toAppendTo,
110
  Pool& pool) const
111
0
{
112
0
  for (auto item : m_priv->patternConverters)
113
0
  {
114
0
    auto startField = toAppendTo.length();
115
0
    item->format(obj, toAppendTo, pool);
116
0
    item->getFormattingInfo().format((int)startField, toAppendTo);
117
0
  }
118
0
}
119
120
121
PatternConverterPtr RollingPolicyBase::getIntegerPatternConverter() const
122
0
{
123
0
  for (std::vector<PatternConverterPtr>::const_iterator
124
0
    converterIter = m_priv->patternConverters.begin();
125
0
    converterIter != m_priv->patternConverters.end();
126
0
    converterIter++)
127
0
  {
128
0
    IntegerPatternConverterPtr intPattern;
129
0
    PatternConverterPtr patternptr = (*converterIter);
130
0
    intPattern = LOG4CXX_NS::cast<IntegerPatternConverter>(patternptr);
131
132
0
    if (intPattern != NULL)
133
0
    {
134
0
      return *converterIter;
135
0
    }
136
0
  }
137
138
0
  PatternConverterPtr noMatch;
139
0
  return noMatch;
140
0
}
141
142
PatternConverterPtr RollingPolicyBase::getDatePatternConverter() const
143
0
{
144
0
  for (std::vector<PatternConverterPtr>::const_iterator
145
0
    converterIter = m_priv->patternConverters.begin();
146
0
    converterIter != m_priv->patternConverters.end();
147
0
    converterIter++)
148
0
  {
149
0
    DatePatternConverterPtr datePattern;
150
0
    PatternConverterPtr patternptr = (*converterIter);
151
0
    datePattern = LOG4CXX_NS::cast<DatePatternConverter>(patternptr);
152
153
0
    if (datePattern != NULL)
154
0
    {
155
0
      return *converterIter;
156
0
    }
157
0
  }
158
159
0
  PatternConverterPtr noMatch;
160
0
  return noMatch;
161
0
}
162
163
0
bool RollingPolicyBase::getCreateIntermediateDirectories() const{
164
0
  return m_priv->createIntermediateDirectories;
165
0
}
166
167
0
void RollingPolicyBase::setCreateIntermediateDirectories(bool createIntermediate){
168
0
  m_priv->createIntermediateDirectories = createIntermediate;
169
0
}
170
171
PatternConverterList RollingPolicyBase::getPatternConverterList() const
172
0
{
173
0
  return m_priv->patternConverters;
174
0
}