syslogappender.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: syslogappender.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2015 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. /** @file */
  22. #ifndef LOG4CPLUS_SYSLOG_APPENDER_HEADER_
  23. #define LOG4CPLUS_SYSLOG_APPENDER_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <log4cplus/appender.h>
  29. #include <log4cplus/helpers/socket.h>
  30. #include <log4cplus/helpers/connectorthread.h>
  31. namespace log4cplus
  32. {
  33. /**
  34. * Appends log events to a file.
  35. *
  36. * <h3>Properties</h3>
  37. * <dl>
  38. * <dt><tt>ident</tt></dt>
  39. * <dd>First argument to <code>openlog()</code>, a string that
  40. * will be prepended to every message.</dd>
  41. *
  42. * <dt><tt>facility</tt></dt>
  43. * <dd>Facility is used in combination with syslog level in first
  44. * argument to syslog(). It can be one of the supported facility
  45. * names (case insensitive), e.g. auth, cron, kern, mail, news
  46. * etc.</dd>
  47. *
  48. * <dt><tt>host</tt></dt>
  49. * <dd>Destination syslog host. When this property is specified,
  50. * messages are sent using UDP to destination host, otherwise
  51. * messages are logged to local syslog.</dd>
  52. *
  53. * <dt><tt>port</tt></dt>
  54. * <dd>Destination port of syslog service on host specified by the
  55. * <tt>host</tt> property. The default value is port 514.</dd>
  56. * </dl>
  57. *
  58. * <dt><tt>udp</tt></dt> <dd>When the syslog is remote, this
  59. * property picks the IP protocol. When the value is true, UDP is
  60. * used. When the value is false, TCP is used. The default value
  61. * is true.</dd> </dl>
  62. *
  63. * \note Messages sent to remote syslog using UDP are conforming
  64. * to RFC5424. Messages sent to remote syslog using TCP are
  65. * using octet counting as described in RFC6587.
  66. */
  67. class LOG4CPLUS_EXPORT SysLogAppender
  68. : public Appender
  69. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  70. , protected virtual helpers::IConnectorThreadClient
  71. #endif
  72. {
  73. public:
  74. //! Remote syslog IP protocol type.
  75. enum RemoteSyslogType
  76. {
  77. RSTUdp,
  78. RSTTcp
  79. };
  80. // Ctors
  81. #if defined (LOG4CPLUS_HAVE_SYSLOG_H)
  82. SysLogAppender(const tstring& ident);
  83. #endif
  84. SysLogAppender(const tstring& ident, const tstring & host,
  85. int port = 514, const tstring & facility = tstring (),
  86. RemoteSyslogType remoteSyslogType = RSTUdp);
  87. SysLogAppender(const log4cplus::helpers::Properties & properties);
  88. // Dtor
  89. virtual ~SysLogAppender();
  90. // Methods
  91. virtual void close();
  92. protected:
  93. virtual int getSysLogLevel(const LogLevel& ll) const;
  94. virtual void append(const spi::InternalLoggingEvent& event);
  95. #if defined (LOG4CPLUS_HAVE_SYSLOG_H)
  96. //! Local syslog (served by `syslog()`) worker function.
  97. void appendLocal(const spi::InternalLoggingEvent& event);
  98. #endif
  99. //! Remote syslog worker function.
  100. void appendRemote(const spi::InternalLoggingEvent& event);
  101. // Data
  102. tstring ident;
  103. int facility;
  104. typedef void (SysLogAppender:: * AppendFuncType) (
  105. const spi::InternalLoggingEvent&);
  106. AppendFuncType appendFunc;
  107. tstring host;
  108. int port;
  109. RemoteSyslogType remoteSyslogType;
  110. helpers::Socket syslogSocket;
  111. bool connected;
  112. static tstring const remoteTimeFormat;
  113. void initConnector ();
  114. void openSocket ();
  115. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  116. virtual thread::Mutex const & ctcGetAccessMutex () const;
  117. virtual helpers::Socket & ctcGetSocket ();
  118. virtual helpers::Socket ctcConnect ();
  119. virtual void ctcSetConnected ();
  120. helpers::SharedObjectPtr<helpers::ConnectorThread> connector;
  121. #endif
  122. private:
  123. // Disallow copying of instances of this class
  124. SysLogAppender(const SysLogAppender&);
  125. SysLogAppender& operator=(const SysLogAppender&);
  126. std::string identStr;
  127. tstring hostname;
  128. };
  129. } // end namespace log4cplus
  130. #endif // LOG4CPLUS_SYSLOG_APPENDER_HEADER_