syslogappender.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: syslogappender.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2017 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. *
  57. * <dt><tt>udp</tt></dt> <dd>When the syslog is remote, this
  58. * property picks the IP protocol. When the value is true, UDP is
  59. * used. When the value is false, TCP is used. The default value
  60. * is true.</dd>
  61. *
  62. * <dt><tt>IPv6</tt></dt>
  63. * <dd>Boolean value specifying whether to use IPv6 (true) or IPv4
  64. * (false). Default value is false.</dd>
  65. *
  66. * <dt><tt>fqdn</tt></dt>
  67. * <dd>Boolean value specifying whether to use FQDN for hostname field.
  68. * Default value is true.</dd>
  69. *
  70. * </dl>
  71. *
  72. * \note Messages sent to remote syslog using UDP are conforming
  73. * to RFC5424. Messages sent to remote syslog using TCP are
  74. * using octet counting as described in RFC6587.
  75. */
  76. class LOG4CPLUS_EXPORT SysLogAppender
  77. : public Appender
  78. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  79. , protected virtual helpers::IConnectorThreadClient
  80. #endif
  81. {
  82. public:
  83. //! Remote syslog IP protocol type.
  84. enum RemoteSyslogType
  85. {
  86. RSTUdp,
  87. RSTTcp
  88. };
  89. // Ctors
  90. #if defined (LOG4CPLUS_HAVE_SYSLOG_H)
  91. SysLogAppender(const tstring& ident);
  92. #endif
  93. SysLogAppender(const tstring& ident, const tstring & host,
  94. int port = 514, const tstring & facility = tstring (),
  95. RemoteSyslogType remoteSyslogType = RSTUdp, bool ipv6 = false);
  96. SysLogAppender(const tstring& ident, const tstring & host,
  97. int port, const tstring & facility,
  98. RemoteSyslogType remoteSyslogType, bool ipv6, bool fqdn);
  99. SysLogAppender(const log4cplus::helpers::Properties & properties);
  100. // Dtor
  101. virtual ~SysLogAppender();
  102. // Methods
  103. virtual void close();
  104. protected:
  105. virtual int getSysLogLevel(const LogLevel& ll) const;
  106. virtual void append(const spi::InternalLoggingEvent& event);
  107. #if defined (LOG4CPLUS_HAVE_SYSLOG_H)
  108. //! Local syslog (served by `syslog()`) worker function.
  109. void appendLocal(const spi::InternalLoggingEvent& event);
  110. #endif
  111. //! Remote syslog worker function.
  112. void appendRemote(const spi::InternalLoggingEvent& event);
  113. // Data
  114. tstring ident;
  115. int facility;
  116. typedef void (SysLogAppender:: * AppendFuncType) (
  117. const spi::InternalLoggingEvent&);
  118. AppendFuncType appendFunc;
  119. tstring host;
  120. int port;
  121. RemoteSyslogType remoteSyslogType;
  122. helpers::Socket syslogSocket;
  123. bool connected;
  124. bool ipv6 = false;
  125. static tstring const remoteTimeFormat;
  126. void initConnector ();
  127. void openSocket ();
  128. #if ! defined (LOG4CPLUS_SINGLE_THREADED)
  129. virtual thread::Mutex const & ctcGetAccessMutex () const;
  130. virtual helpers::Socket & ctcGetSocket ();
  131. virtual helpers::Socket ctcConnect ();
  132. virtual void ctcSetConnected ();
  133. helpers::SharedObjectPtr<helpers::ConnectorThread> connector;
  134. #endif
  135. private:
  136. // Disallow copying of instances of this class
  137. SysLogAppender(const SysLogAppender&);
  138. SysLogAppender& operator=(const SysLogAppender&);
  139. std::string identStr;
  140. tstring hostname;
  141. };
  142. } // end namespace log4cplus
  143. #endif // LOG4CPLUS_SYSLOG_APPENDER_HEADER_