Filter.hh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Filter.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_FILTER_HH
  10. #define _LOG4CPP_FILTER_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <log4cpp/LoggingEvent.hh>
  13. namespace log4cpp {
  14. /**
  15. Users should extend this class to implement customized logging
  16. event filtering. Note that log4cpp::Category and
  17. lof4cpp::Appender have built-in filtering rules. It is suggested
  18. that you first use and understand the built-in rules before rushing
  19. to write your own custom filters.
  20. <p>This abstract class assumes and also imposes that filters be
  21. organized in a linear chain. The <code>decide(LoggingEvent)</code>
  22. method of each filter is called sequentially, in the order of their
  23. addition to the chain.
  24. <p>The <code>decide(LoggingEvent)</code> method must return a
  25. Decision value, either DENY, NEUTRAL or ACCCEPT.
  26. <p>If the value DENY is returned, then the log event is
  27. dropped immediately without consulting with the remaining
  28. filters.
  29. <p>If the value NEUTRAL is returned, then the next filter
  30. in the chain is consulted. If there are no more filters in the
  31. chain, then the log event is logged. Thus, in the presence of no
  32. filters, the default behaviour is to log all logging events.
  33. <p>If the value ACCEPT is returned, then the log
  34. event is logged without consulting the remaining filters.
  35. <p>The philosophy of log4cpp filters is largely inspired from the
  36. Linux ipchains.
  37. **/
  38. class LOG4CPP_EXPORT Filter {
  39. public:
  40. typedef enum { DENY = -1,
  41. NEUTRAL = 0,
  42. ACCEPT = 1
  43. } Decision;
  44. /**
  45. * Default Constructor for Filter
  46. **/
  47. Filter();
  48. /**
  49. * Destructor for Filter
  50. **/
  51. virtual ~Filter();
  52. /**
  53. * Set the next Filter in the Filter chain
  54. * @param filter The filter to chain
  55. **/
  56. virtual void setChainedFilter(Filter* filter);
  57. /**
  58. * Get the next Filter in the Filter chain
  59. * @return The next Filter or NULL if the current filter is the last
  60. * in the chain
  61. **/
  62. virtual Filter* getChainedFilter();
  63. /**
  64. * Get the last Filter in the Filter chain
  65. * @return The last Filter in the Filter chain
  66. **/
  67. virtual Filter* getEndOfChain();
  68. /**
  69. * Add a Filter to the end of the Filter chain. Convience method for
  70. * getEndOfChain()->setChainedFilter(filter).
  71. * @param filter The filter to add to the end of the chain.
  72. **/
  73. virtual void appendChainedFilter(Filter* filter);
  74. /**
  75. * Decide whether to accept or deny a LoggingEvent. This method will
  76. * walk the entire chain until a non neutral decision has been made
  77. * or the end of the chain has been reached.
  78. * @param event The LoggingEvent to decide on.
  79. * @return The Decision
  80. **/
  81. virtual Decision decide(const LoggingEvent& event);
  82. protected:
  83. /**
  84. * Decide whether <b>this</b> Filter accepts or denies the given
  85. * LoggingEvent. Actual implementation of Filter should override this
  86. * method and not <code>decide(LoggingEvent&)</code>.
  87. * @param event The LoggingEvent to decide on.
  88. * @return The Decision
  89. **/
  90. virtual Decision _decide(const LoggingEvent& event) = 0;
  91. private:
  92. Filter* _chainedFilter;
  93. };
  94. }
  95. #endif // _LOG4CPP_FILTER_HH