/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 | 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.erase(m_priv->patternConverters.begin(), m_priv->patternConverters.end()); |
99 | 0 | m_priv->patternFields.erase(m_priv->patternFields.begin(), m_priv->patternFields.end()); |
100 | 0 | PatternParser::parse(m_priv->fileNamePatternStr, |
101 | 0 | m_priv->patternConverters, |
102 | 0 | m_priv->patternFields, |
103 | 0 | getFormatSpecifiers()); |
104 | 0 | } |
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 | 0 | { |
117 | 0 | std::vector<FormattingInfoPtr>::const_iterator formatterIter = |
118 | 0 | m_priv->patternFields.begin(); |
119 | |
|
120 | 0 | for (std::vector<PatternConverterPtr>::const_iterator |
121 | 0 | converterIter = m_priv->patternConverters.begin(); |
122 | 0 | converterIter != m_priv->patternConverters.end(); |
123 | 0 | converterIter++, formatterIter++) |
124 | 0 | { |
125 | 0 | auto startField = toAppendTo.length(); |
126 | 0 | (*converterIter)->format(obj, toAppendTo, pool); |
127 | 0 | (*formatterIter)->format((int)startField, toAppendTo); |
128 | 0 | } |
129 | 0 | } |
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 | 0 | { |
155 | 0 | for (std::vector<PatternConverterPtr>::const_iterator |
156 | 0 | converterIter = m_priv->patternConverters.begin(); |
157 | 0 | converterIter != m_priv->patternConverters.end(); |
158 | 0 | converterIter++) |
159 | 0 | { |
160 | 0 | DatePatternConverterPtr datePattern; |
161 | 0 | PatternConverterPtr patternptr = (*converterIter); |
162 | 0 | datePattern = LOG4CXX_NS::cast<DatePatternConverter>(patternptr); |
163 | |
|
164 | 0 | if (datePattern != NULL) |
165 | 0 | { |
166 | 0 | return *converterIter; |
167 | 0 | } |
168 | 0 | } |
169 | | |
170 | 0 | PatternConverterPtr noMatch; |
171 | 0 | return noMatch; |
172 | 0 | } |
173 | | |
174 | 0 | bool RollingPolicyBase::getCreateIntermediateDirectories() const{ |
175 | 0 | return m_priv->createIntermediateDirectories; |
176 | 0 | } |
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 | } |