Appender.hh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Appender.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_APPENDER_HH
  10. #define _LOG4CPP_APPENDER_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <string>
  13. #include <map>
  14. #include <set>
  15. #include <stdarg.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <log4cpp/Priority.hh>
  20. #include <log4cpp/Layout.hh>
  21. #include <log4cpp/LoggingEvent.hh>
  22. #include <log4cpp/threading/Threading.hh>
  23. LOG4CPP_NS_BEGIN
  24. class LOG4CPP_EXPORT Filter;
  25. /**
  26. * Implement this interface for your own strategies for printing log
  27. * statements.
  28. **/
  29. class LOG4CPP_EXPORT Appender {
  30. public:
  31. // helps cleaning up
  32. friend class HierarchyMaintainer;
  33. /**
  34. * Get a pointer to an existing Appender.
  35. * @param name The name of the Appender to return.
  36. * @returns a pointer to an existing Appender, or NULL if no appender
  37. * with the specified name exists.
  38. **/
  39. static Appender* getAppender(const std::string& name);
  40. /**
  41. * Call reopen() on all existing Appenders.
  42. * @returns true if all Appenders returned true on their reopen() call.
  43. **/
  44. static bool reopenAll();
  45. /**
  46. * Call reopen() on all existing Appenders.
  47. * @returns true if all Appenders returned true on their reopen() call.
  48. **/
  49. static void closeAll();
  50. protected:
  51. /**
  52. * Constructor for Appender. Will only be used in getAppender() (and
  53. * in derived classes of course).
  54. * @param name The name of this Appender.
  55. **/
  56. Appender(const std::string& name);
  57. public:
  58. /**
  59. * Destructor for Appender.
  60. **/
  61. virtual ~Appender();
  62. /**
  63. * Log in Appender specific way.
  64. * @param event The LoggingEvent to log.
  65. **/
  66. virtual void doAppend(const LoggingEvent& event) = 0;
  67. /**
  68. * Reopens the output destination of this Appender, e.g. the logfile
  69. * or TCP socket.
  70. * @returns false if an error occurred during reopening, true otherwise.
  71. **/
  72. virtual bool reopen() = 0;
  73. /**
  74. * Release any resources allocated within the appender such as file
  75. * handles, network connections, etc.
  76. **/
  77. virtual void close() = 0;
  78. /**
  79. * Check if the appender uses a layout.
  80. *
  81. * @returns true if the appender implementation requires a layout.
  82. **/
  83. virtual bool requiresLayout() const = 0;
  84. /**
  85. * Set the Layout for this appender.
  86. * @param layout The layout to use.
  87. **/
  88. virtual void setLayout(Layout* layout) = 0;
  89. /**
  90. * Get the name of this appender. The name identifies the appender.
  91. * @returns the name of the appender.
  92. **/
  93. inline const std::string& getName() const { return _name; };
  94. /**
  95. * Set the threshold priority of this Appender. The Appender will not
  96. * appender LoggingEvents with a priority lower than the threshold.
  97. * Use Priority::NOTSET to disable threshold checking.
  98. * @param priority The priority to set.
  99. **/
  100. virtual void setThreshold(Priority::Value priority) = 0;
  101. /**
  102. * Get the threshold priority of this Appender.
  103. * @returns the threshold
  104. **/
  105. virtual Priority::Value getThreshold() = 0;
  106. /**
  107. * Set a Filter for this appender.
  108. **/
  109. virtual void setFilter(Filter* filter) = 0;
  110. /**
  111. * Get the Filter for this appender.
  112. * @returns the filter, or NULL if no filter has been set.
  113. **/
  114. virtual Filter* getFilter() = 0;
  115. private:
  116. typedef std::map<std::string, Appender*> AppenderMap;
  117. static AppenderMap* _allAppenders;
  118. static threading::Mutex _appenderMapMutex;
  119. static AppenderMap& _getAllAppenders();
  120. static void _deleteAllAppenders();
  121. static void _addAppender(Appender* appender);
  122. static void _removeAppender(Appender* appender);
  123. const std::string _name;
  124. private:
  125. // suppress assignment operator
  126. Appender & operator=(const Appender &);
  127. };
  128. typedef std::set<Appender *> AppenderSet;
  129. LOG4CPP_NS_END
  130. #endif // _LOG4CPP_APPENDER_HH