Category.hh 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. /*
  2. * Category.hh
  3. *
  4. * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
  5. * Copyright 2000, Bastiaan Bakker. All rights reserved.
  6. *
  7. * See the COPYING file for the terms of usage and distribution.
  8. */
  9. #ifndef _LOG4CPP_CATEGORY_HH
  10. #define _LOG4CPP_CATEGORY_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <log4cpp/Appender.hh>
  13. #include <log4cpp/LoggingEvent.hh>
  14. #include <log4cpp/Priority.hh>
  15. #include <log4cpp/CategoryStream.hh>
  16. #include <log4cpp/threading/Threading.hh>
  17. #include <log4cpp/convenience.h>
  18. #include <map>
  19. #include <vector>
  20. #include <cstdarg>
  21. #include <stdexcept>
  22. namespace log4cpp {
  23. /**
  24. * This is the central class in the log4j package. One of the distintive
  25. * features of log4j (and hence log4cpp) are hierarchal categories and
  26. * their evaluation.
  27. **/
  28. class LOG4CPP_EXPORT Category {
  29. friend class HierarchyMaintainer;
  30. public:
  31. /**
  32. * Return the root of the Category hierarchy.
  33. *
  34. * <p>The root category is always instantiated and available. It's
  35. * name is the empty string.
  36. * <p>Unlike in log4j, calling <code>Category.getInstance("")</code>
  37. * <em>does</em> retrieve the root category
  38. * and not a category just under root named "".
  39. * @returns The root category
  40. **/
  41. static Category& getRoot();
  42. /**
  43. * Set the priority of the root Category.
  44. * @param priority The new priority for the root Category
  45. **/
  46. static void setRootPriority(Priority::Value priority);
  47. /**
  48. * Get the priority of the <code>root</code> Category.
  49. * @returns the priority of the root category
  50. **/
  51. static Priority::Value getRootPriority() LOG4CPP_NOTHROW;
  52. /**
  53. * Instantiate a Category with name <code>name</code>. This
  54. * method does not set priority of the category which is by
  55. * default <code>Priority::NOTSET</code>.
  56. *
  57. * @param name The name of the category to retrieve.
  58. **/
  59. static Category& getInstance(const std::string& name);
  60. /**
  61. * If the named category exists (in the default hierarchy) then it
  62. * returns a reference to the category, otherwise it returns NULL.
  63. * @since 0.2.7
  64. **/
  65. static Category* exists(const std::string& name);
  66. /**
  67. * Returns all the currently defined categories as a vector of
  68. * Category pointers. Note: this function does not pass ownership
  69. * of the categories in the vector to the caller, only the ownership
  70. * of the vector. However vector<Category&>* is not legal C++,
  71. * so we can't follow the default ownership conventions.
  72. *
  73. * <p>Unlike in log4j, the root category <em>is</em> included
  74. * in the returned set.
  75. *
  76. * @since 0.3.2. Before 0.3.2 this method returned a std::set
  77. **/
  78. static std::vector<Category*>* getCurrentCategories();
  79. /**
  80. * This method will remove all Appenders from Categories.XXX
  81. **/
  82. static void shutdown();
  83. /**
  84. * This method will remove all Appenders from Categories.XXX and delete all appenders.
  85. * Releases more memory than shutdown() by deleting appenders.
  86. **/
  87. static void shutdownForced();
  88. /**
  89. * Destructor for Category.
  90. **/
  91. virtual ~Category();
  92. /**
  93. * Return the category name.
  94. * @returns The category name.
  95. */
  96. virtual const std::string& getName() const LOG4CPP_NOTHROW;
  97. /**
  98. * Set the priority of this Category.
  99. * @param priority The priority to set. Use Priority::NOTSET to let
  100. * the category use its parents priority as effective priority.
  101. * @exception std::invalid_argument if the caller tries to set
  102. * Priority::NOTSET on the Root Category.
  103. **/
  104. virtual void setPriority(Priority::Value priority);
  105. /**
  106. * Returns the assigned Priority, if any, for this Category.
  107. * @return Priority - the assigned Priority, can be Priority::NOTSET
  108. **/
  109. virtual Priority::Value getPriority() const LOG4CPP_NOTHROW;
  110. /**
  111. * Starting from this Category, search the category hierarchy for a
  112. * set priority and return it. Otherwise, return the priority
  113. * of the root category.
  114. *
  115. * <p>The Category class is designed so that this method executes as
  116. * quickly as possible.
  117. **/
  118. virtual Priority::Value getChainedPriority() const LOG4CPP_NOTHROW;
  119. /**
  120. * Returns true if the chained priority of the Category is equal to
  121. * or higher than given priority.
  122. * @param priority The priority to compare with.
  123. * @returns whether logging is enable for this priority.
  124. **/
  125. virtual bool isPriorityEnabled(Priority::Value priority) const LOG4CPP_NOTHROW;
  126. /**
  127. * Adds an Appender to this Category.
  128. * This method passes ownership from the caller to the Category.
  129. * @since 0.2.7
  130. * @param appender The Appender to wich this category has to log.
  131. * @exception std::invalid_argument if the appender is NULL.
  132. **/
  133. virtual void addAppender(Appender* appender);
  134. /**
  135. * Adds an Appender for this Category.
  136. * This method does not pass ownership from the caller to the Category.
  137. * @since 0.2.7
  138. * @param appender The Appender this category has to log to.
  139. **/
  140. virtual void addAppender(Appender& appender);
  141. /**
  142. * Adds an Appender to this Category.
  143. * This method passes ownership from the caller to the Category.
  144. * @deprecated use addAppender(Appender*) or removeAllAppenders()
  145. * instead.
  146. * @param appender The Appender this category has to log to or NULL
  147. * to remove the current Appenders.
  148. **/
  149. inline void setAppender(Appender* appender) {
  150. if (appender) {
  151. addAppender(appender);
  152. } else {
  153. removeAllAppenders();
  154. }
  155. };
  156. /**
  157. * Adds an Appender for this Category.
  158. * This method does not pass ownership from the caller to the Category.
  159. * @deprecated use addAppender(Appender&) instead.
  160. * @param appender The Appender this category has to log to.
  161. **/
  162. inline void setAppender(Appender& appender) {
  163. addAppender(appender);
  164. };
  165. /**
  166. * Returns the first Appender for this Category, or NULL if no
  167. * Appender has been set.
  168. * @deprecated use getAppender(const std::string&)
  169. * @returns The Appender.
  170. **/
  171. virtual Appender* getAppender() const;
  172. /**
  173. * Returns the specified Appender for this Category, or NULL if
  174. * the Appender is not attached to this Category.
  175. * @since 0.2.7
  176. * @returns The Appender.
  177. **/
  178. virtual Appender* getAppender(const std::string& name) const;
  179. /**
  180. * Returns the set of Appenders currently attached to this Catogory.
  181. * @since 0.3.1
  182. * @returns The set of attached Appenders.
  183. **/
  184. virtual AppenderSet getAllAppenders() const;
  185. /**
  186. * Removes all appenders for this Category.
  187. **/
  188. virtual void removeAllAppenders();
  189. /**
  190. * Removes specified appender for this Category.
  191. * @since 0.2.7
  192. **/
  193. virtual void removeAppender(Appender* appender);
  194. /**
  195. * Returns true if the Category owns the first Appender in its
  196. * Appender set. In that case the Category destructor will delete
  197. * the Appender.
  198. * @deprecated use ownsAppender(Appender*)
  199. **/
  200. virtual bool ownsAppender() const LOG4CPP_NOTHROW {
  201. return ownsAppender(getAppender());
  202. };
  203. /**
  204. * Returns true if the Category owns the Appender. In that case the
  205. * Category destructor will delete the Appender.
  206. * @since 0.2.7
  207. **/
  208. virtual bool ownsAppender(Appender* appender) const LOG4CPP_NOTHROW;
  209. /**
  210. * Call the appenders in the hierarchy starting at
  211. * <code>this</code>. If no appenders could be found, emit a
  212. * warning.
  213. *
  214. * <p>This method always calls all the appenders inherited form the
  215. * hierracy circumventing any evaluation of whether to log or not to
  216. * log the particular log request.
  217. *
  218. * @param event the LogginEvent to log.
  219. **/
  220. virtual void callAppenders(const LoggingEvent& event) LOG4CPP_NOTHROW;
  221. /**
  222. * Set the additivity flag for this Category instance.
  223. **/
  224. virtual void setAdditivity(bool additivity);
  225. /**
  226. * Returns the additivity flag for this Category instance.
  227. **/
  228. virtual bool getAdditivity() const LOG4CPP_NOTHROW;
  229. /**
  230. * Returns the parent category of this category, or NULL
  231. * if the category is the root category.
  232. * @return the parent category.
  233. **/
  234. virtual Category* getParent() LOG4CPP_NOTHROW;
  235. /**
  236. * Returns the parent category of this category, or NULL
  237. * if the category is the root category.
  238. * @return the parent category.
  239. **/
  240. virtual const Category* getParent() const LOG4CPP_NOTHROW;
  241. /**
  242. * Log a message with the specified priority.
  243. * @param priority The priority of this log message.
  244. * @param stringFormat Format specifier for the string to write
  245. * in the log file.
  246. * @param ... The arguments for stringFormat
  247. **/
  248. virtual void log(Priority::Value priority, const char* stringFormat,
  249. ...) LOG4CPP_NOTHROW;
  250. /**
  251. * Log a message with the specified priority.
  252. * @param priority The priority of this log message.
  253. * @param message string to write in the log file
  254. **/
  255. virtual void log(Priority::Value priority,
  256. const std::string& message) LOG4CPP_NOTHROW;
  257. /**
  258. * Log a message with the specified priority.
  259. * @since 0.2.7
  260. * @param priority The priority of this log message.
  261. * @param stringFormat Format specifier for the string to write
  262. * in the log file.
  263. * @param va The arguments for stringFormat.
  264. **/
  265. virtual void logva(Priority::Value priority,
  266. const char* stringFormat,
  267. va_list va) LOG4CPP_NOTHROW;
  268. /**
  269. * Log a message with debug priority.
  270. * @param stringFormat Format specifier for the string to write
  271. * in the log file.
  272. * @param ... The arguments for stringFormat
  273. **/
  274. void debug(const char* stringFormat, ...) LOG4CPP_NOTHROW;
  275. /**
  276. * Log a message with debug priority.
  277. * @param message string to write in the log file
  278. **/
  279. void debug(const std::string& message) LOG4CPP_NOTHROW;
  280. /**
  281. * Return true if the Category will log messages with priority DEBUG.
  282. * @returns Whether the Category will log.
  283. **/
  284. inline bool isDebugEnabled() const LOG4CPP_NOTHROW {
  285. return isPriorityEnabled(Priority::DEBUG);
  286. };
  287. /**
  288. * Return a CategoryStream with priority DEBUG.
  289. * @returns The CategoryStream.
  290. **/
  291. inline CategoryStream debugStream() {
  292. return getStream(Priority::DEBUG);
  293. }
  294. /**
  295. * Log a message with info priority.
  296. * @param stringFormat Format specifier for the string to write
  297. * in the log file.
  298. * @param ... The arguments for stringFormat
  299. **/
  300. void info(const char* stringFormat, ...) LOG4CPP_NOTHROW;
  301. /**
  302. * Log a message with info priority.
  303. * @param message string to write in the log file
  304. **/
  305. void info(const std::string& message) LOG4CPP_NOTHROW;
  306. /**
  307. * Return true if the Category will log messages with priority INFO.
  308. * @returns Whether the Category will log.
  309. **/
  310. inline bool isInfoEnabled() const LOG4CPP_NOTHROW {
  311. return isPriorityEnabled(Priority::INFO);
  312. };
  313. /**
  314. * Return a CategoryStream with priority INFO.
  315. * @returns The CategoryStream.
  316. **/
  317. inline CategoryStream infoStream() {
  318. return getStream(Priority::INFO);
  319. }
  320. /**
  321. * Log a message with notice priority.
  322. * @param stringFormat Format specifier for the string to write
  323. * in the log file.
  324. * @param ... The arguments for stringFormat
  325. **/
  326. void notice(const char* stringFormat, ...) LOG4CPP_NOTHROW;
  327. /**
  328. * Log a message with notice priority.
  329. * @param message string to write in the log file
  330. **/
  331. void notice(const std::string& message) LOG4CPP_NOTHROW;
  332. /**
  333. * Return true if the Category will log messages with priority NOTICE.
  334. * @returns Whether the Category will log.
  335. **/
  336. inline bool isNoticeEnabled() const LOG4CPP_NOTHROW {
  337. return isPriorityEnabled(Priority::NOTICE);
  338. };
  339. /**
  340. * Return a CategoryStream with priority NOTICE.
  341. * @returns The CategoryStream.
  342. **/
  343. inline CategoryStream noticeStream() {
  344. return getStream(Priority::NOTICE);
  345. }
  346. /**
  347. * Log a message with warn priority.
  348. * @param stringFormat Format specifier for the string to write
  349. * in the log file.
  350. * @param ... The arguments for stringFormat
  351. **/
  352. void warn(const char* stringFormat, ...) LOG4CPP_NOTHROW;
  353. /**
  354. * Log a message with warn priority.
  355. * @param message string to write in the log file
  356. **/
  357. void warn(const std::string& message) LOG4CPP_NOTHROW;
  358. /**
  359. * Return true if the Category will log messages with priority WARN.
  360. * @returns Whether the Category will log.
  361. **/
  362. inline bool isWarnEnabled() const LOG4CPP_NOTHROW {
  363. return isPriorityEnabled(Priority::WARN);
  364. };
  365. /**
  366. * Return a CategoryStream with priority WARN.
  367. * @returns The CategoryStream.
  368. **/
  369. inline CategoryStream warnStream() {
  370. return getStream(Priority::WARN);
  371. };
  372. /**
  373. * Log a message with error priority.
  374. * @param stringFormat Format specifier for the string to write
  375. * in the log file.
  376. * @param ... The arguments for stringFormat
  377. **/
  378. void error(const char* stringFormat, ...) LOG4CPP_NOTHROW;
  379. /**
  380. * Log a message with error priority.
  381. * @param message string to write in the log file
  382. **/
  383. void error(const std::string& message) LOG4CPP_NOTHROW;
  384. /**
  385. * Return true if the Category will log messages with priority ERROR.
  386. * @returns Whether the Category will log.
  387. **/
  388. inline bool isErrorEnabled() const LOG4CPP_NOTHROW {
  389. return isPriorityEnabled(Priority::ERROR);
  390. };
  391. /**
  392. * Return a CategoryStream with priority ERROR.
  393. * @returns The CategoryStream.
  394. **/
  395. inline CategoryStream errorStream() {
  396. return getStream(Priority::ERROR);
  397. };
  398. /**
  399. * Log a message with crit priority.
  400. * @param stringFormat Format specifier for the string to write
  401. * in the log file.
  402. * @param ... The arguments for stringFormat
  403. **/
  404. void crit(const char* stringFormat, ...) LOG4CPP_NOTHROW;
  405. /**
  406. * Log a message with crit priority.
  407. * @param message string to write in the log file
  408. **/
  409. void crit(const std::string& message) LOG4CPP_NOTHROW;
  410. /**
  411. * Return true if the Category will log messages with priority CRIT.
  412. * @returns Whether the Category will log.
  413. **/
  414. inline bool isCritEnabled() const LOG4CPP_NOTHROW {
  415. return isPriorityEnabled(Priority::CRIT);
  416. };
  417. /**
  418. * Return a CategoryStream with priority CRIT.
  419. * @returns The CategoryStream.
  420. **/
  421. inline CategoryStream critStream() {
  422. return getStream(Priority::CRIT);
  423. };
  424. /**
  425. * Log a message with alert priority.
  426. * @param stringFormat Format specifier for the string to write
  427. * in the log file.
  428. * @param ... The arguments for stringFormat
  429. **/
  430. void alert(const char* stringFormat, ...) LOG4CPP_NOTHROW;
  431. /**
  432. * Log a message with alert priority.
  433. * @param message string to write in the log file
  434. **/
  435. void alert(const std::string& message) LOG4CPP_NOTHROW;
  436. /**
  437. * Return true if the Category will log messages with priority ALERT.
  438. * @returns Whether the Category will log.
  439. **/
  440. inline bool isAlertEnabled() const LOG4CPP_NOTHROW {
  441. return isPriorityEnabled(Priority::ALERT);
  442. };
  443. /**
  444. * Return a CategoryStream with priority ALERT.
  445. * @returns The CategoryStream.
  446. **/
  447. inline CategoryStream alertStream() LOG4CPP_NOTHROW {
  448. return getStream(Priority::ALERT);
  449. };
  450. /**
  451. * Log a message with emerg priority.
  452. * @param stringFormat Format specifier for the string to write
  453. * in the log file.
  454. * @param ... The arguments for stringFormat
  455. **/
  456. void emerg(const char* stringFormat, ...) LOG4CPP_NOTHROW;
  457. /**
  458. * Log a message with emerg priority.
  459. * @param message string to write in the log file
  460. **/
  461. void emerg(const std::string& message) LOG4CPP_NOTHROW;
  462. /**
  463. * Return true if the Category will log messages with priority EMERG.
  464. * @returns Whether the Category will log.
  465. **/
  466. inline bool isEmergEnabled() const LOG4CPP_NOTHROW {
  467. return isPriorityEnabled(Priority::EMERG);
  468. };
  469. /**
  470. * Return a CategoryStream with priority EMERG.
  471. * @returns The CategoryStream.
  472. **/
  473. inline CategoryStream emergStream() {
  474. return getStream(Priority::EMERG);
  475. };
  476. /**
  477. * Log a message with fatal priority.
  478. * NB. priority 'fatal' is equivalent to 'emerg'.
  479. * @since 0.2.7
  480. * @param stringFormat Format specifier for the string to write
  481. * in the log file.
  482. * @param ... The arguments for stringFormat
  483. **/
  484. void fatal(const char* stringFormat, ...) LOG4CPP_NOTHROW;
  485. /**
  486. * Log a message with fatal priority.
  487. * NB. priority 'fatal' is equivalent to 'emerg'.
  488. * @since 0.2.7
  489. * @param message string to write in the log file
  490. **/
  491. void fatal(const std::string& message) LOG4CPP_NOTHROW;
  492. /**
  493. * Return true if the Category will log messages with priority FATAL.
  494. * NB. priority 'fatal' is equivalent to 'emerg'.
  495. * @since 0.2.7
  496. * @returns Whether the Category will log.
  497. **/
  498. inline bool isFatalEnabled() const LOG4CPP_NOTHROW {
  499. return isPriorityEnabled(Priority::FATAL);
  500. };
  501. /**
  502. * Return a CategoryStream with priority FATAL.
  503. * NB. priority 'fatal' is equivalent to 'emerg'.
  504. * @since 0.2.7
  505. * @returns The CategoryStream.
  506. **/
  507. inline CategoryStream fatalStream() {
  508. return getStream(Priority::FATAL);
  509. };
  510. /**
  511. * Return a CategoryStream with given Priority.
  512. * @param priority The Priority of the CategoryStream.
  513. * @returns The requested CategoryStream.
  514. **/
  515. virtual CategoryStream getStream(Priority::Value priority);
  516. /**
  517. * Return a CategoryStream with given Priority.
  518. * @param priority The Priority of the CategoryStream.
  519. * @returns The requested CategoryStream.
  520. **/
  521. virtual CategoryStream operator<<(Priority::Value priority);
  522. protected:
  523. /**
  524. * Constructor
  525. * @param name the fully qualified name of this Category
  526. * @param parent the parent of this parent, or NULL for the root
  527. * Category
  528. * @param priority the priority for this Category. Defaults to
  529. * Priority::NOTSET
  530. **/
  531. Category(const std::string& name, Category* parent,
  532. Priority::Value priority = Priority::NOTSET);
  533. virtual void _logUnconditionally(Priority::Value priority,
  534. const char* format,
  535. va_list arguments) LOG4CPP_NOTHROW;
  536. /**
  537. * Unconditionally log a message with the specified priority.
  538. * @param priority The priority of this log message.
  539. * @param message string to write in the log file
  540. **/
  541. virtual void _logUnconditionally2(Priority::Value priority,
  542. const std::string& message) LOG4CPP_NOTHROW;
  543. private:
  544. /* prevent copying and assignment */
  545. Category(const Category& other);
  546. Category& operator=(const Category& other);
  547. /** The name of this category. */
  548. const std::string _name;
  549. /**
  550. * The parent of this category. All categories have al least one
  551. * ancestor which is the root category.
  552. **/
  553. Category* _parent;
  554. /**
  555. * The assigned priority of this category.
  556. **/
  557. volatile Priority::Value _priority;
  558. typedef std::map<Appender *, bool> OwnsAppenderMap;
  559. /**
  560. * Returns the iterator to the Appender if the Category owns the
  561. * Appender. In that case the Category destructor will delete the
  562. * Appender.
  563. **/
  564. virtual bool ownsAppender(Appender* appender,
  565. OwnsAppenderMap::iterator& i2) LOG4CPP_NOTHROW;
  566. AppenderSet _appender;
  567. mutable threading::Mutex _appenderSetMutex;
  568. /**
  569. * Whether the category holds the ownership of the appender. If so,
  570. * it deletes the appender in its destructor.
  571. **/
  572. OwnsAppenderMap _ownsAppender;
  573. /**
  574. * Additivity is set to true by default, i.e. a child inherits its
  575. * ancestor's appenders by default.
  576. */
  577. volatile bool _isAdditive;
  578. };
  579. }
  580. #endif // _LOG4CPP_CATEGORY_HH