FixedContextCategory.hh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * FixedContextCategory.hh
  3. *
  4. * Copyright 2001, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
  5. * Copyright 2001, Bastiaan Bakker. All rights reserved.
  6. *
  7. * See the COPYING file for the terms of usage and distribution.
  8. */
  9. #ifndef _LOG4CPP_FIXEDCONTEXTCATEGORY_HH
  10. #define _LOG4CPP_FIXEDCONTEXTCATEGORY_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <log4cpp/Category.hh>
  13. namespace log4cpp {
  14. /**
  15. * This Category subclass replaces the NDC field in LoggingEvents with
  16. * a fixed context string. All handling of Appenders, etc. is delgated
  17. * to the 'normal' Category with the same name. Its intended use is
  18. * for object instances that serve a single client: they contruct a
  19. * FixedContextCategory with the client identifier as context.
  20. * Unlike with regular Category instances one has to explicitly create
  21. * FixedContextCategory instances using the constructor. This also
  22. * implies one has to take cake of destruction of the instance as well.
  23. * @since 0.2.4
  24. **/
  25. class LOG4CPP_EXPORT FixedContextCategory : public Category {
  26. public:
  27. /**
  28. * Constructor
  29. * @param name the fully qualified name of this Categories delegate
  30. * Category.
  31. * @param context the context to fill the NDC field of LoggingEvents
  32. * with.
  33. **/
  34. FixedContextCategory(const std::string& name,
  35. const std::string& context = "");
  36. /**
  37. * Destructor for Category.
  38. **/
  39. virtual ~FixedContextCategory();
  40. /**
  41. * Set the context string used as NDC.
  42. * @param context the context string
  43. **/
  44. virtual void setContext(const std::string& context);
  45. /**
  46. * Return the context string used as NDC.
  47. * @return the context string.
  48. **/
  49. virtual std::string getContext() const;
  50. /**
  51. * Returns the assigned Priority, if any, for this Category.
  52. * @return Priority - the assigned Priority, can be Priority::NOTSET
  53. **/
  54. virtual Priority::Value getPriority() const LOG4CPP_NOTHROW;
  55. /**
  56. * Starting from this Category, search the category hierarchy for a
  57. * set priority and return it. Otherwise, return the priority
  58. * of the root category.
  59. *
  60. * <p>The Category class is designed so that this method executes as
  61. * quickly as possible.
  62. **/
  63. virtual Priority::Value getChainedPriority() const LOG4CPP_NOTHROW;
  64. /**
  65. * For the moment this method does nothing.
  66. **/
  67. virtual void addAppender(Appender* appender) LOG4CPP_NOTHROW;
  68. /**
  69. * For the moment this method does nothing.
  70. **/
  71. virtual void addAppender(Appender& appender);
  72. /**
  73. * Returns the Appender for this Category, or NULL if no Appender has
  74. * been set.
  75. * @returns The Appender.
  76. **/
  77. virtual Appender* getAppender() const;
  78. /**
  79. * Returns the specified Appender for this Category, or NULL if
  80. * the Appender is not attached to this Category.
  81. * @since 0.2.7
  82. * @returns The Appender.
  83. **/
  84. virtual Appender* getAppender(const std::string& name) const;
  85. /**
  86. * Returns the set of Appenders currently attached to this Catogory.
  87. * @since 0.3.1
  88. * @returns The set of attached Appenders.
  89. **/
  90. virtual AppenderSet getAllAppenders() const;
  91. /**
  92. * Removes all appenders set for this Category. Currently a Category
  93. * can have only one appender, but this may change in the future.
  94. **/
  95. virtual void removeAllAppenders();
  96. /**
  97. * FixedContextAppenders cannot own Appenders.
  98. * @returns false
  99. **/
  100. virtual bool ownsAppender() const LOG4CPP_NOTHROW;
  101. /**
  102. * FixedContextAppenders cannot own Appenders.
  103. * @returns false
  104. **/
  105. virtual bool ownsAppender(Appender* appender)
  106. const LOG4CPP_NOTHROW;
  107. /**
  108. * Call the appenders in the hierarchy starting at
  109. * <code>this</code>. If no appenders could be found, emit a
  110. * warning.
  111. *
  112. * <p>This method always calls all the appenders inherited form the
  113. * hierracy circumventing any evaluation of whether to log or not to
  114. * log the particular log request.
  115. *
  116. * @param event The LoggingEvent to log.
  117. **/
  118. virtual void callAppenders(const LoggingEvent& event) LOG4CPP_NOTHROW;
  119. /**
  120. * Set the additivity flag for this Category instance.
  121. **/
  122. virtual void setAdditivity(bool additivity);
  123. /**
  124. * Returns the additivity flag for this Category instance.
  125. **/
  126. virtual bool getAdditivity() const LOG4CPP_NOTHROW;
  127. protected:
  128. /**
  129. * Unconditionally log a message with the specified priority.
  130. * @param priority The priority of this log message.
  131. * @param message string to write in the log file
  132. **/
  133. virtual void _logUnconditionally2(Priority::Value priority,
  134. const std::string& message) LOG4CPP_NOTHROW;
  135. private:
  136. /**
  137. * The delegate category of this FixedContextCategory.
  138. **/
  139. Category& _delegate;
  140. /** The context of this FixedContextCategory. */
  141. std::string _context;
  142. };
  143. }
  144. #endif // _LOG4CPP_FIXEDCONTEXTCATEGORY_HH