RemoteSyslogAppender.hh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * SyslogAppender.hh
  3. *
  4. * Copyright 2001, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
  5. * Copyright 2001, Walter Stroebel. All rights reserved.
  6. *
  7. * See the COPYING file for the terms of usage and distribution.
  8. */
  9. #ifndef _LOG4CPP_REMOTESYSLOGAPPENDER_HH
  10. #define _LOG4CPP_REMOTESYSLOGAPPENDER_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <string>
  13. #include <stdarg.h>
  14. #include <log4cpp/LayoutAppender.hh>
  15. #include <log4cpp/Priority.hh>
  16. #ifdef _WIN32
  17. #include <winsock2.h>
  18. #else
  19. #include <netinet/in.h>
  20. #endif
  21. #ifdef LOG4CPP_HAVE_SYSLOG
  22. #include <syslog.h>
  23. #else
  24. /// from syslog.h
  25. typedef enum {
  26. LOG_EMERG = 0, ///< system is unusable
  27. LOG_ALERT = 1, ///< action must be taken immediately
  28. LOG_CRIT = 2, ///< critical conditions
  29. LOG_ERR = 3, ///< error conditions
  30. LOG_WARNING = 4, ///< warning conditions
  31. LOG_NOTICE = 5, ///< normal but significant condition
  32. LOG_INFO = 6, ///< informational
  33. LOG_DEBUG = 7, ///< debug-level messages
  34. } SyslogLevel;
  35. typedef enum {
  36. LOG_KERN = (0 << 3), ///< kernel messages
  37. LOG_USER = (1 << 3), ///< random user-level messages
  38. LOG_MAIL = (2 << 3), ///< mail system
  39. LOG_DAEMON = (3 << 3), ///< system daemons
  40. LOG_AUTH = (4 << 3), ///< security/authorization messages
  41. LOG_SYSLOG = (5 << 3), ///< messages generated internally by syslogd
  42. LOG_LPR = (6 << 3), ///< line printer subsystem
  43. LOG_NEWS = (7 << 3), ///< network news subsystem
  44. LOG_UUCP = (8 << 3), ///< UUCP subsystem
  45. LOG_CRON = (9 << 3), ///< clock daemon
  46. LOG_AUTHPRIV = (10 << 3), ///< security/authorization messages (private)
  47. LOG_FTP = (11 << 3), ///< ftp daemon
  48. /* other codes through 15 reserved for system use */
  49. LOG_LOCAL0 = (16 << 3), ///< reserved for local use
  50. LOG_LOCAL1 = (17 << 3), ///< reserved for local use
  51. LOG_LOCAL2 = (18 << 3), ///< reserved for local use
  52. LOG_LOCAL3 = (19 << 3), ///< reserved for local use
  53. LOG_LOCAL4 = (20 << 3), ///< reserved for local use
  54. LOG_LOCAL5 = (21 << 3), ///< reserved for local use
  55. LOG_LOCAL6 = (22 << 3), ///< reserved for local use
  56. LOG_LOCAL7 = (23 << 3), ///< reserved for local use
  57. } SyslogFacility;
  58. #endif
  59. LOG4CPP_NS_BEGIN
  60. /**
  61. * RemoteSyslogAppender sends LoggingEvents to a remote syslog system.
  62. *
  63. * Also see: draft-ietf-syslog-syslog-12.txt
  64. **/
  65. class LOG4CPP_EXPORT RemoteSyslogAppender : public LayoutAppender {
  66. public:
  67. /**
  68. * Translates a log4cpp priority to a syslog priority
  69. * @param priority The log4cpp priority.
  70. * @returns the syslog priority.
  71. **/
  72. static int toSyslogPriority(Priority::Value priority);
  73. /**
  74. * Instantiate a RemoteSyslogAppender with given name and name and
  75. * facility for syslog.
  76. * @param name The name of the Appender
  77. * @param syslogName The ident parameter in the openlog(3) call.
  78. * @param relayer The IP address or hostname of a standard syslog host.
  79. * @param facility The syslog facility to log to. Defaults to LOG_USER.
  80. * Value '-1' implies to use the default.
  81. * @param portNumber An alternative port number. Defaults to the
  82. * standard syslog port number (514).
  83. * Value '-1' implies to use the default.
  84. **/
  85. RemoteSyslogAppender(const std::string& name,
  86. const std::string& syslogName,
  87. const std::string& relayer,
  88. int facility = LOG_USER,
  89. int portNumber = 514);
  90. virtual ~RemoteSyslogAppender();
  91. /**
  92. * Closes and reopens the socket.
  93. **/
  94. virtual bool reopen();
  95. /**
  96. * Closes the socket
  97. **/
  98. virtual void close();
  99. protected:
  100. /**
  101. * Just creates the socket.
  102. **/
  103. virtual void open();
  104. /**
  105. * Sends a LoggingEvent to the remote syslog.
  106. * @param event the LoggingEvent to log.
  107. **/
  108. virtual void _append(const LoggingEvent& event);
  109. const std::string _syslogName;
  110. const std::string _relayer;
  111. int _facility;
  112. int _portNumber;
  113. #ifdef WIN32
  114. SOCKET _socket;
  115. #else
  116. int _socket;
  117. #endif
  118. in_addr_t _ipAddr;
  119. private:
  120. int _cludge;
  121. };
  122. LOG4CPP_NS_END
  123. #endif // _LOG4CPP_REMOTESYSLOGAPPENDER_HH