Utils/Logger: Implement direct syslog-via-UDP target
[senf.git] / Utils / Logger / SyslogUDPTarget.hh
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief SyslogUDPTarget public header */
25
26 #ifndef HH_SENF_Utils_Logger_SyslogUDPTarget_
27 #define HH_SENF_Utils_Logger_SyslogUDPTarget_ 1
28
29 // Custom includes
30 #include "SyslogTarget.hh"
31 #include "../../Socket/Protocols/INet/INetAddressing.hh"
32 #include "../../Socket/ClientSocketHandle.hh"
33 #include "../../Socket/FramingPolicy.hh"
34 #include "../../Socket/ReadWritePolicy.hh"
35 #include "../../Socket/CommunicationPolicy.hh"
36
37 //#include "SyslogUDPTarget.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41 namespace log {
42
43     /** \brief Log target writing UDP syslog packets
44
45         The SyslogUDPTarget will send all log messages directly via UDP to a target host. This host
46         should have a syslog daemon or relay running. The protocol is defined in <a
47         href="ttp://tools.ietf.org/html/rfc3164">RFC-3164</a>.
48
49         This log target has some important benefits:
50
51         \li It will never block. It may however lose log messages.
52         \li It does \e not add timestamp information locally.
53
54         These are \e advantages since this makes SyslogUDPTarget a very reliable high-performance
55         logging target.
56
57         Valid facility values are from <tt>man 3 syslog</tt>:
58
59         \par "" 
60            <tt>LOG_AUTHPRIV</tt>, <tt>LOG_CRON</tt>, <tt>LOG_DAEMON</tt>, <tt>LOG_FTP</tt>,
61            <tt>LOG_KERN</tt>, <tt>LOG_LOCAL0</tt>, <tt>LOG_LOCAL1</tt>, <tt>LOG_LOCAL2</tt>,
62            <tt>LOG_LOCAL3</tt>, <tt>LOG_LOCAL4</tt>, <tt>LOG_LOCAL5</tt>, <tt>LOG_LOCAL6</tt>,
63            <tt>LOG_LOCAL7</tt>, <tt>LOG_LPR</tt>, <tt>LOG_MAIL</tt>, <tt>LOG_NEWS</tt>,
64            <tt>LOG_SYSLOG</tt>, <tt>LOG_USER</tt>, <tt>LOG_UUCP</tt>
65
66         the default facility is <tt>LOG_USER</tt>
67
68         The SENF log levels are mapped to syslog levels in the following way:
69
70         <table class="senf fixedcolumn">
71         <tr><td>senf::log::VERBOSE</td>   <td>\c LOG_DEBUG</td></tr>
72         <tr><td>senf::log::NOTICE</td>    <td>\c LOG_INFO</td></tr>
73         <tr><td>senf::log::MESSAGE</td>   <td>\c LOG_NOTICE</td></tr>
74         <tr><td>senf::log::IMPORTANT</td> <td>\c LOG_WARNING</td></tr>
75         <tr><td>senf::log::CRITICAL</td>  <td>\c LOG_CRIT</td></tr>
76         <tr><td>senf::log::FATAL</td>     <td>\c LOG_EMERG</td></tr>
77         </table>
78
79         \note Since the UDP syslog packets are limited to 1024 characters and there must be some
80             space left so a relay may optionally add a timestamp and hostname section, the log
81             messages are split after 896 characters. Additionally the log messages are split at each
82             newline char since non-printable characters are not allowed.
83      */
84     class SyslogUDPTarget
85         : public Target
86     {
87     public:
88         ///////////////////////////////////////////////////////////////////////////
89         // Types
90
91         ///////////////////////////////////////////////////////////////////////////
92         ///\name Structors and default members
93         ///@{
94
95         explicit SyslogUDPTarget(senf::INet4Address const & target, int facility = LOG_USER);
96         explicit SyslogUDPTarget(senf::INet4SocketAddress const & target, int facility = LOG_USER);
97         explicit SyslogUDPTarget(senf::INet6Address const & target, int facility = LOG_USER);
98         explicit SyslogUDPTarget(senf::INet6SocketAddress const & target, int facility = LOG_USER);
99
100         ///@}
101         ///////////////////////////////////////////////////////////////////////////
102
103     private:
104         void v_write(time_type timestamp, std::string const & stream, 
105                      std::string const & area, unsigned level, 
106                      std::string const & message);
107
108         int facility_;
109         typedef senf::ClientSocketHandle< senf::MakeSocketPolicy<
110             senf::DatagramFramingPolicy,
111             senf::ConnectedCommunicationPolicy,
112             senf::WriteablePolicy>::policy > Handle;
113         Handle handle_;
114     };
115
116 }}
117
118 ///////////////////////////////hh.e////////////////////////////////////////
119 //#include "SyslogUDPTarget.cci"
120 //#include "SyslogUDPTarget.ct"
121 //#include "SyslogUDPTarget.cti"
122 #endif
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // comment-column: 40
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // End: