Coverage Report

Created: 2026-12-01 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/include/log4cxx/helpers/optionconverter.h
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
#ifndef _LOG4CXX_HELPER_OPTION_CONVERTER_H
19
#define _LOG4CXX_HELPER_OPTION_CONVERTER_H
20
21
#include <log4cxx/logstring.h>
22
#include <log4cxx/spi/configurator.h>
23
24
namespace LOG4CXX_NS
25
{
26
class Level;
27
class File;
28
typedef std::shared_ptr<Level> LevelPtr;
29
30
31
namespace helpers
32
{
33
class Properties;
34
35
class Class;
36
37
/** A convenience class to convert property values to specific types.*/
38
class LOG4CXX_EXPORT OptionConverter
39
{
40
    /** OptionConverter is a static class. */
41
  private:
42
0
    OptionConverter() {}
43
44
  public:
45
    static LogString convertSpecialChars(const LogString& s);
46
47
    /**
48
     The boolean equivalent of \c value if it is not empty, otherwise \c defaultValue.
49
50
    If <code>value</code> is "true", then <code>true</code> is
51
    returned. If <code>value</code> is "false", then
52
    <code>true</code> is returned. Case of \c value is unimportant.
53
54
    @return  <code>defaultValue</code> if \c value is not "true" or "false", otherwise the boolean equivalent of \c value
55
    */
56
    static bool toBoolean(const LogString& value, bool defaultValue);
57
    /**
58
     The numeric equivalent of \c value if it is not empty, otherwise \c defaultValue.
59
60
    @return Zero if \c value does not begin with a valid integral number otherwise the numeric equivalent of \c value
61
    */
62
    static int toInt(const LogString& value, int defaultValue);
63
    /**
64
     The numeric equivalent of \c value if it is an integer in the range 0 - 2^63, otherwise \c defaultValue.
65
66
     A suffix "KB", "MB" or "GB" after an integer value
67
     converts the provided number respectively to kilobytes, megabytes
68
     and gigabytes. For example, the value "10KB" will be interpreted as 10240.
69
    */
70
    static size_t toFileSize(const LogString& value, size_t defaultValue);
71
    /**
72
    The Level indicated by \c value if recognised otherwise \c defaultValue.
73
74
    To be recognised, \c value must be one of "Trace", "Debug", "Info", "Warn", "Error", "Fatal", "Off", "All"
75
    or a custom level in which case it is of the form <code>{levelName}#{registeredClassName}</code>.
76
77
    <p>Case of \c value is unimportant.
78
    */
79
    static LevelPtr toLevel(const LogString& value,
80
      const LevelPtr& defaultValue);
81
82
    /**
83
    Find the value corresponding to <code>key</code> in
84
    <code>props</code>. Then perform variable substitution on the
85
    found value.
86
    */
87
    static LogString findAndSubst(const LogString& key, const Properties& props);
88
89
    /**
90
    Perform variable substitution in string <code>val</code> from the
91
    values of keys found in the system propeties.
92
93
    <p>The variable substitution delimeters are <b>${</b> and <b>}</b>.
94
95
    <p>For example, if the System properties contains "key=value", then
96
    the call
97
    ~~~{.cpp}
98
    auto s = OptionConverter.substituteVars("Value of key is ${key}.");
99
    ~~~
100
101
    will set the variable <code>s</code> to "Value of key is value.".
102
103
    <p>If no value could be found for the specified key, then the
104
    <code>props</code> parameter is searched, if the value could not
105
    be found there, then substitution defaults to the empty string.
106
107
    <p>For example, if system propeties contains no value for the key
108
    "inexistentKey", then the call
109
110
    ~~~{.cpp}
111
    auto s = OptionConverter.subsVars("Value of inexistentKey is [${inexistentKey}]");
112
    ~~~
113
    will set <code>s</code> to "Value of inexistentKey is []"
114
115
    <p>An IllegalArgumentException is thrown if
116
    <code>val</code> contains a start delimeter "${" which is not
117
    balanced by a stop delimeter "}". </p>
118
119
    @param val The string on which variable substitution is performed.
120
    @param props The properties from which variable substitution is performed.
121
    @throws IllegalArgumentException if <code>val</code> is malformed.
122
    */
123
    static LogString substVars(const LogString& val, const Properties& props);
124
125
    /**
126
     *  Gets the specified system property.
127
    @param key The key to search for.
128
    @param def The default value to return.
129
    @return the string value of the system property, or the default
130
    value if there is no property with that key.
131
    */
132
    static LogString getSystemProperty(const LogString& key, const LogString& def);
133
134
    /**
135
    Instantiate an object given a class name.
136
    Returns \c defaultValue if the named class is not a registered class
137
    or the class does not inherit from \c superClass.
138
139
    @param className The registered name of the class to construct.
140
    @param superClass The class from which the new object must inherit.
141
    @param defaultValue The value to return in case of failure
142
    */
143
    static ObjectPtr instantiateByClassName
144
      ( const LogString& className
145
      , const Class&     superClass
146
      , const ObjectPtr& defaultValue
147
      );
148
149
    /**
150
    Instantiate an object using the value in \c props associated with \c key as a registered class name.
151
    Returns \c defaultValue if the named class is not a registered class
152
    or the class does not inherit from \c superClass.
153
154
    @param key The \c props entry whose value is the registered name of the class to construct.
155
    @param superClass The class from which the new object must inherit.
156
    @param defaultValue The value to return in case of failure
157
    */
158
    static ObjectPtr instantiateByKey
159
      ( const Properties& props
160
      , const LogString&  key
161
      , const Class&      superClass
162
      , const ObjectPtr&  defaultValue
163
      );
164
165
    /**
166
    Use the information in \c configFileName to configure Log4cxx.
167
168
    @param configFileName Holds the path of the configuration file.
169
    @param className The spi::Configurator subclass used to process \c configFileName.
170
    If empty, a registered class whose name ends with "Configurator" and
171
    whose spi::Configurator::isValid instance method
172
    returns <code>true</code> is used to process the configuration file.
173
    @param target Holds the configuration result
174
    @param millisecondDelay If greater than zero, the milliseconds to sleep
175
    between checking if <code>configFileName</code> has been modified
176
    and needs to be reloaded.
177
    */
178
    static spi::ConfigurationStatus selectAndConfigure
179
      ( const File&                     configFileName
180
      , const LogString&                className = {}
181
      , const spi::LoggerRepositoryPtr& target = {}
182
      , int                             millisecondDelay = 0
183
      );
184
};
185
}  // namespace helpers
186
} // namespace log4cxx
187
188
#endif //_LOG4CXX_HELPER_OPTION_CONVERTER_H
189