factory.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: factory.h
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * Copyright 2007 Martin Heinrich
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************/
  24. #ifndef LOG4QT_HELPERS_FACTORY_H
  25. #define LOG4QT_HELPERS_FACTORY_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include <QtCore/QHash>
  30. #include <QtCore/QMutex>
  31. #include <QtCore/QStringList>
  32. QT_BEGIN_NAMESPACE
  33. class QMetaProperty;
  34. class QObject;
  35. QT_END_NAMESPACE
  36. /******************************************************************************
  37. * Declarations
  38. ******************************************************************************/
  39. namespace Log4Qt
  40. {
  41. class Appender;
  42. class Filter;
  43. class Layout;
  44. /*!
  45. * \brief The class Factory provides factories for Appender, Filter and
  46. * Layout objects.
  47. *
  48. * The functions createAppender(), createFilter() and createLayout()
  49. * allow to create objects by specifying their class names. By default
  50. * all classes of the package are recognised with their Log4j and Log4Qt
  51. * classanmes. For example an object of the class FileAppender can be
  52. * craeted using "org.apache.log4j.FileAppender" or "Log4Qt::FileAppender".
  53. * Additional classes can be registered using registerAppender(),
  54. * registerFilter() and registerLayout().
  55. *
  56. * An QObject property can be set from a string value with
  57. * setObjectProperty(). The function handles the required error checking
  58. * and type conversion.
  59. *
  60. * \note All the functions declared in this class are thread-safe.
  61. *
  62. * \sa PropertyConfigurator
  63. */
  64. class Factory
  65. {
  66. public:
  67. /*!
  68. * Prototype for an Appender factory function. The function creates
  69. * an Appender object on the heap and returns a pointer to it.
  70. *
  71. * \sa registerAppender(), createAppender()
  72. */
  73. typedef Appender *(*AppenderFactoryFunc)();
  74. /*!
  75. * Prototype for a Filter factory function. The function creates
  76. * a Filter object on the heap and returns a pointer to it.
  77. *
  78. * \sa registerFilter(), createFilter()
  79. */
  80. typedef Filter *(*FilterFactoryFunc)();
  81. /*!
  82. * Prototype for a Layout factory function. The function creates
  83. * a Layout object on the heap and returns a pointer to it.
  84. *
  85. * \sa registerLayout(), createLayout()
  86. */
  87. typedef Layout *(*LayoutFactoryFunc)();
  88. private:
  89. Factory();
  90. Q_DISABLE_COPY(Factory)
  91. public:
  92. /*!
  93. * Creates an object for the class \a rAppenderClassName on the heap
  94. * and returns a pointer to it. If the class has no registered factory
  95. * function a null pointer is returned.
  96. *
  97. * \sa registerAppender(), unregisterAppender(), registeredAppenders()
  98. */
  99. static Appender *createAppender(const QString &rAppenderClassName);
  100. /*!
  101. * This is an overloaded member function, provided for convenience.
  102. */
  103. static Appender *createAppender(const char *pAppenderClassName);
  104. /*!
  105. * Creates an object for the class \a rFilterClassName on the heap
  106. * and returns a pointer to it. If the class has no registered factory
  107. * function a null pointer is returned.
  108. *
  109. * \sa registerFilter(), unregisterFilter(), registeredFilters()
  110. */
  111. static Filter *createFilter(const QString &rFilterClassName);
  112. /*!
  113. * This is an overloaded member function, provided for convenience.
  114. */
  115. static Filter *createFilter(const char *pFilterClassName);
  116. /*!
  117. * Creates an object for the class \a rLayoutClassName on the heap
  118. * and returns a pointer to it. If the class has no registered factory
  119. * function a null pointer is returned.
  120. *
  121. * \sa registerLayout(), unregisterLayout(), registeredLayouts()
  122. */
  123. static Layout *createLayout(const QString &rLayoutClassName);
  124. /*!
  125. * This is an overloaded member function, provided for convenience.
  126. */
  127. static Layout *createLayout(const char *pLayoutClassName);
  128. /*!
  129. * Returns the Factory instance.
  130. */
  131. static Factory *instance();
  132. /*!
  133. * Registers the Appender factory function \a pAppenderFactoryFunc
  134. * for the class \a rAppenderClassName. If a registered factory
  135. * function exists for the class, it is replaced with
  136. * \a pAppenderFactoryFunc.
  137. *
  138. * \sa unregisterAppender(), registeredAppenders(), createAppender()
  139. */
  140. static void registerAppender(const QString &rAppenderClassName,
  141. AppenderFactoryFunc pAppenderFactoryFunc);
  142. /*!
  143. * This is an overloaded member function, provided for convenience.
  144. */
  145. static void registerAppender(const char *pAppenderClassName,
  146. AppenderFactoryFunc pAppenderFactoryFunc);
  147. /*!
  148. * Registers the Filter factory function \a pFilterFactoryFunc
  149. * for the class \a rFilterClassName. If a registered factory
  150. * function exists for the class, it is replaced with
  151. * \a pFilterFactoryFunc.
  152. *
  153. * \sa unregisterFilter(), registeredFilters(), createFilter()
  154. */
  155. static void registerFilter(const QString &rFilterClassName,
  156. FilterFactoryFunc pFilterFactoryFunc);
  157. /*!
  158. * This is an overloaded member function, provided for convenience.
  159. */
  160. static void registerFilter(const char *pFilterClassName,
  161. FilterFactoryFunc pFilterFactoryFunc);
  162. /*!
  163. * Registers the Layout factory function \a pLayoutFactoryFunc
  164. * for the class \a rLayoutClassName. If a registered factory
  165. * function exists for the class, it is replaced with
  166. * \a pLayoutFactoryFunc.
  167. *
  168. * \sa unregisterLayout(), registeredLayout(), createLayout()
  169. */
  170. static void registerLayout(const QString &rLayoutClassName,
  171. LayoutFactoryFunc pLayoutFactoryFunc);
  172. /*!
  173. * This is an overloaded member function, provided for convenience.
  174. */
  175. static void registerLayout(const char *pLayoutClassName,
  176. LayoutFactoryFunc pLayoutFactoryFunc);
  177. /*!
  178. * Returns a list of the class names for registered Appender factory
  179. * functions.
  180. *
  181. * \sa registerAppender(), unregisterAppender()
  182. */
  183. static QStringList registeredAppenders();
  184. /*!
  185. * Returns a list of the class names for registered Filter factory
  186. * functions.
  187. *
  188. * \sa registerFilter(), unregisterFilter()
  189. */
  190. static QStringList registeredFilters();
  191. /*!
  192. * Returns a list of the class names for registered Layout factory
  193. * functions.
  194. *
  195. * \sa registerLayout(), unregisterLayout()
  196. */
  197. static QStringList registeredLayouts();
  198. /*!
  199. * Sets the property \a rProperty of the object \a pObject to the
  200. * value \a rValue. The function will test that the property
  201. * \a rProperty is writeable and of a type the function can convert to.
  202. * The types bool, int, Level and QString are supported.
  203. *
  204. * \sa OptionConverter
  205. */
  206. static void setObjectProperty(QObject *pObject,
  207. const QString &rProperty,
  208. const QString &rValue);
  209. /*!
  210. * This is an overloaded member function, provided for convenience.
  211. */
  212. static void setObjectProperty(QObject *pObject,
  213. const char *pProperty,
  214. const QString &rValue);
  215. /*!
  216. * Unregisters the Appender factory function for the class
  217. * \a rAppenderClassName.
  218. *
  219. * \sa registerAppender(), registeredAppenders()
  220. */
  221. static void unregisterAppender(const QString &rAppenderClassName);
  222. /*!
  223. * This is an overloaded member function, provided for convenience.
  224. */
  225. static void unregisterAppender(const char *pAppenderClassName);
  226. /*!
  227. * Unregisters the Filter factory function for the class
  228. * \a rFilterClassName.
  229. *
  230. * \sa registerFilter(), registeredFilters()
  231. */
  232. static void unregisterFilter(const QString &rFilterClassName);
  233. /*!
  234. * This is an overloaded member function, provided for convenience.
  235. */
  236. static void unregisterFilter(const char *pFilterClassName);
  237. /*!
  238. * Unregisters the Layout factory function for the class
  239. * \a rLayoutClassName.
  240. *
  241. * \sa registerLayout(), registeredLayouts()
  242. */
  243. static void unregisterLayout(const QString &rLayoutClassName);
  244. /*!
  245. * This is an overloaded member function, provided for convenience.
  246. */
  247. static void unregisterLayout(const char *pLayoutClassName);
  248. private:
  249. Appender *doCreateAppender(const QString &rAppenderClassName);
  250. Filter *doCreateFilter(const QString &rFilterClassName);
  251. Layout *doCreateLayout(const QString &rLayoutClassName);
  252. void doRegisterAppender(const QString &rAppenderClassName,
  253. AppenderFactoryFunc pAppenderFactoryFunc);
  254. void doRegisterFilter(const QString &rFilterClassName,
  255. FilterFactoryFunc pFilterFactoryFunc);
  256. void doRegisterLayout(const QString &rLayoutClassName,
  257. LayoutFactoryFunc pLayoutFactoryFunc);
  258. void doSetObjectProperty(QObject *pObject,
  259. const QString &rProperty,
  260. const QString &rValue);
  261. void doUnregisterAppender(const QString &rAppenderClassName);
  262. void doUnregisterFilter(const QString &rFilterClassName);
  263. void doUnregisterLayout(const QString &rLayoutClassName);
  264. void registerDefaultAppenders();
  265. void registerDefaultFilters();
  266. void registerDefaultLayouts();
  267. bool validateObjectProperty(QMetaProperty &rMetaProperty,
  268. const QString &rProperty,
  269. QObject *pObject);
  270. private:
  271. mutable QMutex mObjectGuard;
  272. QHash<QString, AppenderFactoryFunc> mAppenderRegistry;
  273. QHash<QString, FilterFactoryFunc> mFilterRegistry;
  274. QHash<QString, LayoutFactoryFunc> mLayoutRegistry;
  275. };
  276. /**************************************************************************
  277. * Operators, Helper
  278. **************************************************************************/
  279. #ifndef QT_NO_DEBUG_STREAM
  280. /*!
  281. * \relates Factory
  282. *
  283. * Writes all object member variables to the given debug stream \a rDebug and
  284. * returns the stream.
  285. *
  286. * <tt>
  287. * %Factory(appenderfactories:("Log4Qt::DebugAppender", "Log4Qt::NullAppender",
  288. * "Log4Qt::ConsoleAppender", "org.apache.log4j.varia.DebugAppender",
  289. * "org.apache.log4j.FileAppender", "org.apache.log4j.RollingFileAppender",
  290. * "org.apache.log4j.DailyRollingFileAppender",
  291. * "org.apache.log4j.varia.ListAppender",
  292. * "org.apache.log4j.varia.NullAppender",
  293. * "Log4Qt::FileAppender", "org.apache.log4j.ConsoleAppender",
  294. * "Log4Qt::DailyRollingFileAppender", "Log4Qt::ListAppender",
  295. * "Log4Qt::RollingFileAppender") filterfactories:
  296. * ("Log4Qt::DenyAllFilter", "Log4Qt::StringMatchFilter",
  297. * "Log4Qt::LevelRangeFilter", "org.apache.log4j.varia.DenyAllFilter",
  298. * "org.apache.log4j.varia.LevelRangeFilter",
  299. * "org.apache.log4j.varia.StringMatchFilter", "Log4Qt::LevelMatchFilter",
  300. * "org.apache.log4j.varia.LevelMatchFilter") layoutfactories:
  301. * ("org.apache.log4j.SimpleLayout", "Log4Qt::PatternLayout",
  302. * "Log4Qt::SimpleLayout", "org.apache.log4j.TTCCLayout",
  303. * "Log4Qt::TTCCLayout", "org.apache.log4j.PatternLayout") )
  304. * </tt>
  305. * \sa QDebug, Factory::logManager()
  306. */
  307. QDebug operator<<(QDebug debug,
  308. const Factory &rFactory);
  309. #endif // QT_NO_DEBUG_STREAM
  310. /**************************************************************************
  311. * Inline
  312. **************************************************************************/
  313. inline Appender *Factory::createAppender(const QString &rAppenderClassName)
  314. {
  315. return instance()->doCreateAppender(rAppenderClassName);
  316. }
  317. inline Appender *Factory::createAppender(const char *pAppenderClassName)
  318. {
  319. return instance()->doCreateAppender(QLatin1String(pAppenderClassName));
  320. }
  321. inline Filter *Factory::createFilter(const QString &rFilterClassName)
  322. {
  323. return instance()->doCreateFilter(rFilterClassName);
  324. }
  325. inline Filter *Factory::createFilter(const char *pFilterClassName)
  326. {
  327. return instance()->doCreateFilter(QLatin1String(pFilterClassName));
  328. }
  329. inline Layout *Factory::createLayout(const QString &rLayoutClassName)
  330. {
  331. return instance()->doCreateLayout(rLayoutClassName);
  332. }
  333. inline Layout *Factory::createLayout(const char *pLayoutClassName)
  334. {
  335. return instance()->doCreateLayout(QLatin1String(pLayoutClassName));
  336. }
  337. inline void Factory::registerAppender(const QString &rAppenderClassName,
  338. AppenderFactoryFunc pAppenderFactoryFunc)
  339. {
  340. instance()->doRegisterAppender(rAppenderClassName, pAppenderFactoryFunc);
  341. }
  342. inline void Factory::registerAppender(const char *pAppenderClassName,
  343. AppenderFactoryFunc pAppenderFactoryFunc)
  344. {
  345. instance()->doRegisterAppender(QLatin1String(pAppenderClassName), pAppenderFactoryFunc);
  346. }
  347. inline void Factory::registerFilter(const QString &rFilterClassName,
  348. FilterFactoryFunc pFilterFactoryFunc)
  349. {
  350. instance()->doRegisterFilter(rFilterClassName, pFilterFactoryFunc);
  351. }
  352. inline void Factory::registerFilter(const char *pFilterClassName,
  353. FilterFactoryFunc pFilterFactoryFunc)
  354. {
  355. instance()->doRegisterFilter(QLatin1String(pFilterClassName), pFilterFactoryFunc);
  356. }
  357. inline void Factory::registerLayout(const QString &rLayoutClassName,
  358. LayoutFactoryFunc pLayoutFactoryFunc)
  359. {
  360. instance()->doRegisterLayout(rLayoutClassName, pLayoutFactoryFunc);
  361. }
  362. inline void Factory::registerLayout(const char *pLayoutClassName,
  363. LayoutFactoryFunc pLayoutFactoryFunc)
  364. {
  365. instance()->doRegisterLayout(QLatin1String(pLayoutClassName), pLayoutFactoryFunc);
  366. }
  367. inline QStringList Factory::registeredAppenders()
  368. {
  369. QMutexLocker locker(&instance()->mObjectGuard);
  370. return instance()->mAppenderRegistry.keys();
  371. }
  372. inline QStringList Factory::registeredFilters()
  373. {
  374. QMutexLocker locker(&instance()->mObjectGuard);
  375. return instance()->mFilterRegistry.keys();
  376. }
  377. inline QStringList Factory::registeredLayouts()
  378. {
  379. QMutexLocker locker(&instance()->mObjectGuard);
  380. return instance()->mLayoutRegistry.keys();
  381. }
  382. inline void Factory::setObjectProperty(QObject *pObject,
  383. const QString &rProperty,
  384. const QString &rValue)
  385. {
  386. instance()->doSetObjectProperty(pObject, rProperty, rValue);
  387. }
  388. inline void Factory::setObjectProperty(QObject *pObject,
  389. const char *pProperty,
  390. const QString &rValue)
  391. {
  392. instance()->doSetObjectProperty(pObject, QLatin1String(pProperty), rValue);
  393. }
  394. inline void Factory::unregisterAppender(const QString &rAppenderClassName)
  395. {
  396. instance()->doUnregisterAppender(rAppenderClassName);
  397. }
  398. inline void Factory::unregisterAppender(const char *pAppenderClassName)
  399. {
  400. instance()->doUnregisterAppender(QLatin1String(pAppenderClassName));
  401. }
  402. inline void Factory::unregisterFilter(const QString &rFilterClassName)
  403. {
  404. instance()->doUnregisterFilter(rFilterClassName);
  405. }
  406. inline void Factory::unregisterFilter(const char *pFilterClassName)
  407. {
  408. instance()->doUnregisterFilter(QLatin1String(pFilterClassName));
  409. }
  410. inline void Factory::unregisterLayout(const QString &rLayoutClassName)
  411. {
  412. instance()->doUnregisterLayout(rLayoutClassName);
  413. }
  414. inline void Factory::unregisterLayout(const char *pLayoutClassName)
  415. {
  416. instance()->doUnregisterLayout(QLatin1String(pLayoutClassName));
  417. }
  418. } // namespace Log4Qt
  419. // Q_DECLARE_TYPEINFO(Log4Qt::Factory, Q_COMPLEX_TYPE); // use default
  420. #endif // LOG4QT_HELPERS_FACTORY_H