4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief SyslogUDPTarget public header */
31 #ifndef HH_SENF_Utils_Logger_SyslogUDPTarget_
32 #define HH_SENF_Utils_Logger_SyslogUDPTarget_ 1
36 #include "LogFormat.hh"
38 #include <senf/Socket/Protocols/INet/INetAddressing.hh>
39 #include <senf/Socket/ClientSocketHandle.hh>
40 #include <senf/Socket/FramingPolicy.hh>
41 #include <senf/Socket/ReadWritePolicy.hh>
42 #include <senf/Socket/CommunicationPolicy.hh>
44 //#include "SyslogUDPTarget.mpp"
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
50 /** \brief Log target writing UDP syslog packets
52 The SyslogUDPTarget will send all %log messages directly via UDP to a target host. This
53 host should have a syslog daemon or relay running. The protocol is defined in <a
54 href="http://tools.ietf.org/html/rfc3164">RFC-3164</a>.
56 This %log target has some important benefits:
58 \li It will never block. It may however lose %log messages.
59 \li It does \e not add timestamp information locally.
61 These are \e advantages since this makes SyslogUDPTarget a very reliable high-performance
64 Valid facility values are from <tt>man 3 syslog</tt>:
67 <tt>LOG_AUTHPRIV</tt>, <tt>LOG_CRON</tt>, <tt>LOG_DAEMON</tt>, <tt>LOG_FTP</tt>,
68 <tt>LOG_KERN</tt>, <tt>LOG_LOCAL0</tt>, <tt>LOG_LOCAL1</tt>, <tt>LOG_LOCAL2</tt>,
69 <tt>LOG_LOCAL3</tt>, <tt>LOG_LOCAL4</tt>, <tt>LOG_LOCAL5</tt>, <tt>LOG_LOCAL6</tt>,
70 <tt>LOG_LOCAL7</tt>, <tt>LOG_LPR</tt>, <tt>LOG_MAIL</tt>, <tt>LOG_NEWS</tt>,
71 <tt>LOG_SYSLOG</tt>, <tt>LOG_USER</tt>, <tt>LOG_UUCP</tt>
73 the default facility is <tt>LOG_USER</tt>
75 The SENF %log levels are mapped to syslog levels in the following way:
77 <table class="senf fixedcolumn">
78 <tr><td>senf::log::VERBOSE</td> <td>\c LOG_DEBUG</td></tr>
79 <tr><td>senf::log::NOTICE</td> <td>\c LOG_INFO</td></tr>
80 <tr><td>senf::log::MESSAGE</td> <td>\c LOG_NOTICE</td></tr>
81 <tr><td>senf::log::IMPORTANT</td> <td>\c LOG_WARNING</td></tr>
82 <tr><td>senf::log::CRITICAL</td> <td>\c LOG_CRIT</td></tr>
83 <tr><td>senf::log::FATAL</td> <td>\c LOG_EMERG</td></tr>
86 \note Since the UDP syslog packets are limited to 1024 characters and there must be some
87 space left so a relay may optionally add a timestamp and hostname section, the %log
88 messages are split after 896 characters. Additionally the %log messages are split at each
89 newline char since non-printable characters are not allowed.
91 \implementation The RFC only \e recommends the exact message format. This allows us to
92 include the \c PRI part but skip the \c HEADER part (which includes the timestamp and
93 hostname) for better performance. We add a space after the \c PRI to force the syslog
94 daemon to skip the \c HEADER part.
99 : public Target, private detail::LogFormat
102 //-////////////////////////////////////////////////////////////////////////
105 //-////////////////////////////////////////////////////////////////////////
106 ///\name Structors and default members
109 explicit SyslogUDPTarget(INet4Address const & target, int facility = LOG_USER);
110 explicit SyslogUDPTarget(INet4SocketAddress const & target, int facility = LOG_USER);
111 explicit SyslogUDPTarget(INet6Address const & target, int facility = LOG_USER);
112 explicit SyslogUDPTarget(INet6SocketAddress const & target, int facility = LOG_USER);
115 //-////////////////////////////////////////////////////////////////////////
117 using detail::LogFormat::showTime;
118 using detail::LogFormat::showStream;
119 using detail::LogFormat::showLevel;
120 using detail::LogFormat::showArea;
121 using detail::LogFormat::timeFormat;
122 using detail::LogFormat::tag;
124 bool syslog() const; ///< \c true, if using syslog format, \c false otherwise
125 /**< When syslog format is disabled, messages are not
126 formated as valid syslog messages but sent using plain
128 void syslog(bool enabled=true); ///< Set syslog format
132 void v_write(time_type timestamp, std::string const & stream,
133 std::string const & area, unsigned level,
134 std::string const & message);
136 void consoleFormat(std::ostream & os);
139 typedef senf::ClientSocketHandle< senf::MakeSocketPolicy<
140 senf::DatagramFramingPolicy,
141 senf::ConnectedCommunicationPolicy,
142 senf::WriteablePolicy>::policy > Handle;
148 AUTHPRIV = LOG_AUTHPRIV,
171 struct RegisterConsole {
173 static boost::shared_ptr<console::DirectoryNode> create(
174 INet4SocketAddress const & target, LogFacility facility = USER);
175 static boost::shared_ptr<console::DirectoryNode> create(
176 INet4Address const & target, LogFacility facility = USER);
177 static boost::shared_ptr<console::DirectoryNode> create(
178 INet6SocketAddress const & target, LogFacility facility = USER);
179 static boost::shared_ptr<console::DirectoryNode> create(
180 INet6Address const & target, LogFacility facility = USER);
181 static RegisterConsole instance;
187 //-/////////////////////////////////////////////////////////////////////////////////////////////////
188 #include "SyslogUDPTarget.cci"
189 //#include "SyslogUDPTarget.ct"
190 //#include "SyslogUDPTarget.cti"
197 // comment-column: 40
198 // c-file-style: "senf"
199 // indent-tabs-mode: nil
200 // ispell-local-dictionary: "american"
201 // compile-command: "scons -u test"