optionconverter.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: optionconverter.cpp
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * changes Feb 2009, Martin Heinrich
  10. * - Fixed a problem were OptionConverter::toBoolean would not
  11. * return the default value, if the conversion fails.
  12. *
  13. *
  14. * Copyright 2007 - 2009 Martin Heinrich
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. ******************************************************************************/
  29. /******************************************************************************
  30. * Dependencies
  31. ******************************************************************************/
  32. #include "log4qt/helpers/optionconverter.h"
  33. #include <QtCore/QDebug>
  34. #include "log4qt/helpers/logerror.h"
  35. #include "log4qt/helpers/properties.h"
  36. #include "log4qt/logger.h"
  37. #include "log4qt/consoleappender.h"
  38. namespace Log4Qt
  39. {
  40. /**************************************************************************
  41. * Declarations
  42. **************************************************************************/
  43. /**************************************************************************
  44. * C helper functions
  45. **************************************************************************/
  46. LOG4QT_DECLARE_STATIC_LOGGER(logger, Log4Qt::OptionConverter)
  47. /**************************************************************************
  48. * Class implementation: OptionConverter
  49. **************************************************************************/
  50. QString OptionConverter::findAndSubst(const Properties &rProperties,
  51. const QString &rKey)
  52. {
  53. QString value = rProperties.property(rKey);
  54. if (value.isNull())
  55. return value;
  56. const QString begin_subst = QLatin1String("${");
  57. const QString end_subst = QLatin1String("}");
  58. const int begin_length = begin_subst.length();
  59. const int end_length = end_subst.length();
  60. // Don't return a null string, the null string indicates that the
  61. // property key does not exist.
  62. QString result = QLatin1String("");
  63. int i = 0;
  64. int begin;
  65. int end;
  66. while (i < value.length())
  67. {
  68. begin = value.indexOf(begin_subst, i);
  69. if (begin == -1)
  70. {
  71. result += value.mid(i);
  72. i = value.length();
  73. }
  74. else
  75. {
  76. result += value.mid(i, begin - i);
  77. end = value.indexOf(end_subst, i + begin_length);
  78. if (end == -1)
  79. {
  80. LogError e = LOG4QT_ERROR(QT_TR_NOOP("Missing closing bracket for opening bracket at %1. Invalid subsitution in value %2."),
  81. CONFIGURATOR_INVALID_SUBSTITUTION_ERROR,
  82. "Log4Qt::OptionConverter");
  83. e << begin << value;
  84. logger()->error(e);
  85. return result;
  86. }
  87. else
  88. {
  89. result += findAndSubst(rProperties, value.mid(begin + begin_length, end - begin - end_length - 1));
  90. i = end + end_length;
  91. }
  92. }
  93. }
  94. return result;
  95. }
  96. QString OptionConverter::classNameJavaToCpp(const QString &rClassName)
  97. {
  98. const QLatin1String java_class_delimiter(".");
  99. const QLatin1String cpp_class_delimiter("::");
  100. QString result = rClassName;
  101. return result.replace(java_class_delimiter, cpp_class_delimiter);
  102. }
  103. bool OptionConverter::toBoolean(const QString &rOption,
  104. bool *p_ok)
  105. {
  106. const QLatin1String str_true("true");
  107. const QLatin1String str_enabled("enabled");
  108. const QLatin1String str_one("1");
  109. const QLatin1String str_false("false");
  110. const QLatin1String str_disabled("disabled");
  111. const QLatin1String str_zero("0");
  112. if (p_ok)
  113. *p_ok = true;
  114. QString s = rOption.trimmed().toLower();
  115. if (s == str_true || s == str_enabled || s == str_one)
  116. return true;
  117. if (s == str_false || s == str_disabled || s == str_zero)
  118. return false;
  119. if (p_ok)
  120. *p_ok = false;
  121. LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a boolean"),
  122. CONFIGURATOR_INVALID_OPTION_ERROR,
  123. "Log4Qt::OptionConverter");
  124. e << rOption;
  125. logger()->error(e);
  126. return false;
  127. }
  128. bool OptionConverter::toBoolean(const QString &rOption,
  129. bool default_value)
  130. {
  131. bool ok;
  132. bool result = toBoolean(rOption, &ok);
  133. if (ok)
  134. return result;
  135. else
  136. return default_value;
  137. }
  138. qint64 OptionConverter::toFileSize(const QString &rOption,
  139. bool *p_ok)
  140. {
  141. // - Search for unit
  142. // - Convert characters befor unit to int
  143. // - Error, if
  144. // - the conversion failed
  145. // - the value < 0
  146. // - there is text after the unit characters
  147. if (p_ok)
  148. *p_ok = false;
  149. QString s = rOption.trimmed().toLower();
  150. qint64 f = 1;
  151. int i;
  152. i = s.indexOf(QLatin1String("kb"));
  153. if (i >= 0)
  154. f = 1024;
  155. else
  156. {
  157. i = s.indexOf(QLatin1String("mb"));
  158. if (i >= 0)
  159. f = 1024 * 1024;
  160. else
  161. {
  162. i = s.indexOf(QLatin1String("gb"));
  163. if (i >= 0)
  164. f = 1024 * 1024 * 1024;
  165. }
  166. }
  167. if (i < 0)
  168. i = s.length();
  169. bool ok;
  170. qint64 value = s.left(i).toLongLong(&ok);
  171. if (!ok || value < 0 || s.length() > i + 2)
  172. {
  173. LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a file size"),
  174. CONFIGURATOR_INVALID_OPTION_ERROR,
  175. "Log4Qt::OptionConverter");
  176. e << rOption;
  177. logger()->error(e);
  178. return 0;
  179. }
  180. if (p_ok)
  181. *p_ok = true;
  182. return value * f;
  183. }
  184. int OptionConverter::toInt(const QString &rOption,
  185. bool *p_ok)
  186. {
  187. int value = rOption.trimmed().toInt(p_ok);
  188. if (*p_ok)
  189. return value;
  190. LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for an integer"),
  191. CONFIGURATOR_INVALID_OPTION_ERROR,
  192. "Log4Qt::OptionConverter");
  193. e << rOption;
  194. logger()->error(e);
  195. return 0;
  196. }
  197. qint64 OptionConverter::toQInt64(const QString &rOption,
  198. bool *p_ok)
  199. {
  200. int value = rOption.trimmed().toLongLong(p_ok);
  201. if (*p_ok)
  202. return value;
  203. LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for an qint64"),
  204. CONFIGURATOR_INVALID_OPTION_ERROR,
  205. "Log4Qt::OptionConverter");
  206. e << rOption;
  207. logger()->error(e);
  208. return 0;
  209. }
  210. Level OptionConverter::toLevel(const QString &rOption,
  211. bool *p_ok)
  212. {
  213. bool ok;
  214. Level level = Level::fromString(rOption.toUpper().trimmed(), &ok);
  215. if (p_ok)
  216. *p_ok = ok;
  217. if (ok)
  218. return level;
  219. LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a level"),
  220. CONFIGURATOR_INVALID_OPTION_ERROR,
  221. "Log4Qt::OptionConverter");
  222. e << rOption;
  223. logger()->error(e);
  224. return level;
  225. }
  226. Level OptionConverter::toLevel(const QString &rOption,
  227. const Level &rDefaultValue)
  228. {
  229. bool ok;
  230. Level result = toLevel(rOption, &ok);
  231. if (ok)
  232. return result;
  233. else
  234. return rDefaultValue;
  235. }
  236. int OptionConverter::toTarget(const QString &rOption,
  237. bool *p_ok)
  238. {
  239. const QLatin1String java_stdout("system.out");
  240. const QLatin1String cpp_stdout("stdout_target");
  241. const QLatin1String java_stderr("system.err");
  242. const QLatin1String cpp_stderr("stderr_target");
  243. if (p_ok)
  244. *p_ok = true;
  245. QString s = rOption.trimmed().toLower();
  246. if (s == java_stdout || s == cpp_stdout)
  247. return ConsoleAppender::STDOUT_TARGET;
  248. if (s == java_stderr || s == cpp_stderr)
  249. return ConsoleAppender::STDERR_TARGET;
  250. if (p_ok)
  251. *p_ok = false;
  252. LogError e = LOG4QT_ERROR(QT_TR_NOOP("Invalid option string '%1' for a target"),
  253. CONFIGURATOR_INVALID_OPTION_ERROR,
  254. "Log4Qt::OptionConverter");
  255. e << rOption;
  256. logger()->error(e);
  257. return ConsoleAppender::STDOUT_TARGET;
  258. }
  259. /**************************************************************************
  260. * Implementation: Operators, Helper
  261. **************************************************************************/
  262. } // namespace Log4Qt