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