Coverage Report

Created: 2025-10-13 07:02

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/helpers/object.h>
23
24
namespace LOG4CXX_NS
25
{
26
class Level;
27
class File;
28
typedef std::shared_ptr<Level> LevelPtr;
29
30
namespace spi
31
{
32
class LoggerRepository;
33
typedef std::shared_ptr<LoggerRepository> LoggerRepositoryPtr;
34
}
35
36
namespace helpers
37
{
38
class Properties;
39
40
class Class;
41
42
/** A convenience class to convert property values to specific types.*/
43
class LOG4CXX_EXPORT OptionConverter
44
{
45
    /** OptionConverter is a static class. */
46
  private:
47
0
    OptionConverter() {}
48
49
  public:
50
    static LogString convertSpecialChars(const LogString& s);
51
52
    /**
53
     The boolean equivalent of \c value if it is not empty, otherwise \c defaultValue.
54
55
    If <code>value</code> is "true", then <code>true</code> is
56
    returned. If <code>value</code> is "false", then
57
    <code>true</code> is returned. Case of \c value is unimportant.
58
59
    @return  <code>defaultValue</code> if \c value is not "true" or "false", otherwise the boolean equivalent of \c value
60
    */
61
    static bool toBoolean(const LogString& value, bool defaultValue);
62
    /**
63
     The numeric equivalent of \c value if it is not empty, otherwise \c defaultValue.
64
65
    @return Zero if \c value does not begin with a valid integral number otherwise the numeric equivalent of \c value
66
    */
67
    static int toInt(const LogString& value, int defaultValue);
68
    /**
69
     The numeric equivalent of \c value if it is not empty, otherwise \c defaultValue.
70
71
     A suffix "KB", "MB" or "GB" after an integer value
72
     converts the provided number respectively to kilobytes, megabytes
73
     and gigabytes. For example, the value "10KB" will be interpreted as 10240.
74
    */
75
    static long toFileSize(const LogString& value, long defaultValue);
76
    /**
77
    The Level indicated by \c value if recognised otherwise \c defaultValue.
78
79
    To be recognised, \c value must be one of "Trace", "Debug", "Info", "Warn", "Error", "Fatal", "Off", "All"
80
    or a custom level in which case it is of the form <code>{levelName}#{registeredClassName}</code>.
81
82
    <p>Case of \c value is unimportant.
83
    */
84
    static LevelPtr toLevel(const LogString& value,
85
      const LevelPtr& defaultValue);
86
87
    /**
88
    Find the value corresponding to <code>key</code> in
89
    <code>props</code>. Then perform variable substitution on the
90
    found value.
91
    */
92
    static LogString findAndSubst(const LogString& key, Properties& props);
93
94
    /**
95
    Perform variable substitution in string <code>val</code> from the
96
    values of keys found in the system propeties.
97
98
    <p>The variable substitution delimeters are <b>${</b> and <b>}</b>.
99
100
    <p>For example, if the System properties contains "key=value", then
101
    the call
102
    ~~~{.cpp}
103
    auto s = OptionConverter.substituteVars("Value of key is ${key}.");
104
    ~~~
105
106
    will set the variable <code>s</code> to "Value of key is value.".
107
108
    <p>If no value could be found for the specified key, then the
109
    <code>props</code> parameter is searched, if the value could not
110
    be found there, then substitution defaults to the empty string.
111
112
    <p>For example, if system propeties contains no value for the key
113
    "inexistentKey", then the call
114
115
    ~~~{.cpp}
116
    auto s = OptionConverter.subsVars("Value of inexistentKey is [${inexistentKey}]");
117
    ~~~
118
    will set <code>s</code> to "Value of inexistentKey is []"
119
120
    <p>An IllegalArgumentException is thrown if
121
    <code>val</code> contains a start delimeter "${" which is not
122
    balanced by a stop delimeter "}". </p>
123
124
    @param val The string on which variable substitution is performed.
125
    @param props The properties from which variable substitution is performed.
126
    @throws IllegalArgumentException if <code>val</code> is malformed.
127
    */
128
    static LogString substVars(const LogString& val, Properties& props);
129
130
    /**
131
     *  Gets the specified system property.
132
    @param key The key to search for.
133
    @param def The default value to return.
134
    @return the string value of the system property, or the default
135
    value if there is no property with that key.
136
    */
137
    static LogString getSystemProperty(const LogString& key, const LogString& def);
138
139
    /**
140
    Instantiate an object given a class name. Check that the
141
    <code>className</code> is a subclass of
142
    <code>superClass</code>. If that test fails or the object could
143
    not be instantiated, then <code>defaultValue</code> is returned.
144
145
    @param className The fully qualified class name of the object to instantiate.
146
    @param superClass The class to which the new object should belong.
147
    @param defaultValue The object to return in case of non-fulfillment
148
    */
149
    static ObjectPtr instantiateByClassName(const LogString& className,
150
      const Class& superClass, const ObjectPtr& defaultValue);
151
152
    static ObjectPtr instantiateByKey(Properties& props,
153
      const LogString& key, const Class& superClass,
154
      const ObjectPtr& defaultValue);
155
156
    /**
157
    Configure log4cxx given a configFileName.
158
159
    <p>The configFileName must point to a file which will be
160
    interpreted by a new instance of a log4cxx configurator.
161
162
    <p>All configurations steps are taken on the
163
    <code>hierarchy</code> passed as a parameter.
164
165
    <p>
166
    @param configFileName The location of the configuration file.
167
    @param clazz The classname, of the log4cxx configurator which
168
    will parse the file <code>configFileName</code>. This must be
169
    a subclass of Configurator, or null. If this value is null then
170
    a default configurator of PropertyConfigurator is used, unless the
171
    filename pointed to by <code>configFileName</code> ends in '.xml',
172
    in which case DOMConfigurator is used.
173
    @param hierarchy The Hierarchy to act on.
174
    @param delay 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 void selectAndConfigure(const File& configFileName,
179
      const LogString& clazz, spi::LoggerRepositoryPtr hierarchy, int delay = 0);
180
};
181
}  // namespace helpers
182
} // namespace log4cxx
183
184
#endif //_LOG4CXX_HELPER_OPTION_CONVERTER_H
185