Utils/Logger: cleaned up some #includes
[senf.git] / senf / 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 <syslog.h>
31 #include "LogFormat.hh"
32 #include "Target.hh"
33 #include <senf/Socket/Protocols/INet/INetAddressing.hh>
34 #include <senf/Socket/ClientSocketHandle.hh>
35 #include <senf/Socket/FramingPolicy.hh>
36 #include <senf/Socket/ReadWritePolicy.hh>
37 #include <senf/Socket/CommunicationPolicy.hh>
38
39 //#include "SyslogUDPTarget.mpp"
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 namespace senf {
43 namespace log {
44
45     /** \brief Log target writing UDP syslog packets
46
47         The SyslogUDPTarget will send all %log messages directly via UDP to a target host. This
48         host should have a syslog daemon or relay running. The protocol is defined in <a
49         href="http://tools.ietf.org/html/rfc3164">RFC-3164</a>.
50
51         This %log target has some important benefits:
52
53         \li It will never block. It may however lose %log messages.
54         \li It does \e not add timestamp information locally.
55
56         These are \e advantages since this makes SyslogUDPTarget a very reliable high-performance
57         logging target.
58
59         Valid facility values are from <tt>man 3 syslog</tt>:
60
61         \par ""
62            <tt>LOG_AUTHPRIV</tt>, <tt>LOG_CRON</tt>, <tt>LOG_DAEMON</tt>, <tt>LOG_FTP</tt>,
63            <tt>LOG_KERN</tt>, <tt>LOG_LOCAL0</tt>, <tt>LOG_LOCAL1</tt>, <tt>LOG_LOCAL2</tt>,
64            <tt>LOG_LOCAL3</tt>, <tt>LOG_LOCAL4</tt>, <tt>LOG_LOCAL5</tt>, <tt>LOG_LOCAL6</tt>,
65            <tt>LOG_LOCAL7</tt>, <tt>LOG_LPR</tt>, <tt>LOG_MAIL</tt>, <tt>LOG_NEWS</tt>,
66            <tt>LOG_SYSLOG</tt>, <tt>LOG_USER</tt>, <tt>LOG_UUCP</tt>
67
68         the default facility is <tt>LOG_USER</tt>
69
70         The SENF %log levels are mapped to syslog levels in the following way:
71
72         <table class="senf fixedcolumn">
73         <tr><td>senf::log::VERBOSE</td>   <td>\c LOG_DEBUG</td></tr>
74         <tr><td>senf::log::NOTICE</td>    <td>\c LOG_INFO</td></tr>
75         <tr><td>senf::log::MESSAGE</td>   <td>\c LOG_NOTICE</td></tr>
76         <tr><td>senf::log::IMPORTANT</td> <td>\c LOG_WARNING</td></tr>
77         <tr><td>senf::log::CRITICAL</td>  <td>\c LOG_CRIT</td></tr>
78         <tr><td>senf::log::FATAL</td>     <td>\c LOG_EMERG</td></tr>
79         </table>
80
81         \note Since the UDP syslog packets are limited to 1024 characters and there must be some
82             space left so a relay may optionally add a timestamp and hostname section, the %log
83             messages are split after 896 characters. Additionally the %log messages are split at each
84             newline char since non-printable characters are not allowed.
85
86         \implementation The RFC only \e recommends the exact message format. This allows us to
87             include the \c PRI part but skip the \c HEADER part (which includes the timestamp and
88             hostname) for better performance. We add a space after the \c PRI to force the syslog
89             daemon to skip the \c HEADER part.
90
91         \ingroup targets
92      */
93     class SyslogUDPTarget
94         : public Target, private detail::LogFormat
95     {
96     public:
97         //-////////////////////////////////////////////////////////////////////////
98         // Types
99
100         //-////////////////////////////////////////////////////////////////////////
101         ///\name Structors and default members
102         //\{
103
104         explicit SyslogUDPTarget(senf::INet4Address const & target, int facility = LOG_USER);
105         explicit SyslogUDPTarget(senf::INet4SocketAddress const & target, int facility = LOG_USER);
106         explicit SyslogUDPTarget(senf::INet6Address const & target, int facility = LOG_USER);
107         explicit SyslogUDPTarget(senf::INet6SocketAddress const & target, int facility = LOG_USER);
108
109         //\}
110         //-////////////////////////////////////////////////////////////////////////
111
112         using detail::LogFormat::showTime;
113         using detail::LogFormat::showStream;
114         using detail::LogFormat::showLevel;
115         using detail::LogFormat::showArea;
116         using detail::LogFormat::timeFormat;
117         using detail::LogFormat::tag;
118
119         bool syslog() const;            ///< \c true, if using syslog format, \c false otherwise
120                                         /**< When syslog format is disabled, messages are not
121                                              formated as valid syslog messages but sent using plain
122                                              UDP. */
123         void syslog(bool enabled=true); ///< Set syslog format
124
125     private:
126         void init();
127         void v_write(time_type timestamp, std::string const & stream,
128                      std::string const & area, unsigned level,
129                      std::string const & message);
130
131         void consoleFormat(std::ostream & os);
132
133         int facility_;
134         typedef senf::ClientSocketHandle< senf::MakeSocketPolicy<
135             senf::DatagramFramingPolicy,
136             senf::ConnectedCommunicationPolicy,
137             senf::WriteablePolicy>::policy > Handle;
138         Handle handle_;
139         bool syslogFormat_;
140
141     public:
142         enum LogFacility {
143             AUTHPRIV = LOG_AUTHPRIV,
144             CRON = LOG_CRON,
145             DAEMON = LOG_DAEMON,
146             FTP = LOG_FTP,
147             KERN = LOG_KERN,
148             LOCAL0 = LOG_LOCAL0,
149             LOCAL1 = LOG_LOCAL1,
150             LOCAL2 = LOG_LOCAL2,
151             LOCAL3 = LOG_LOCAL3,
152             LOCAL4 = LOG_LOCAL4,
153             LOCAL5 = LOG_LOCAL5,
154             LOCAL6 = LOG_LOCAL6,
155             LOCAL7 = LOG_LOCAL7,
156             LPR = LOG_LPR,
157             MAIL = LOG_MAIL,
158             NEWS = LOG_NEWS,
159             SYSLOG = LOG_SYSLOG,
160             USER = LOG_USER,
161             UUCP = LOG_UUCP
162         };
163
164     private:
165
166         struct RegisterConsole {
167             RegisterConsole();
168             static boost::shared_ptr<senf::console::DirectoryNode> create(
169                 senf::INet4SocketAddress const & target, LogFacility facility = USER);
170             static boost::shared_ptr<senf::console::DirectoryNode> create(
171                 senf::INet4Address const & target, LogFacility facility = USER);
172             static boost::shared_ptr<senf::console::DirectoryNode> create(
173                 senf::INet6SocketAddress const & target, LogFacility facility = USER);
174             static boost::shared_ptr<senf::console::DirectoryNode> create(
175                 senf::INet6Address const & target, LogFacility facility = USER);
176             static RegisterConsole instance;
177         };
178     };
179
180 }}
181
182 //-/////////////////////////////////////////////////////////////////////////////////////////////////
183 #include "SyslogUDPTarget.cci"
184 //#include "SyslogUDPTarget.ct"
185 //#include "SyslogUDPTarget.cti"
186 #endif
187
188 \f
189 // Local Variables:
190 // mode: c++
191 // fill-column: 100
192 // comment-column: 40
193 // c-file-style: "senf"
194 // indent-tabs-mode: nil
195 // ispell-local-dictionary: "american"
196 // compile-command: "scons -u test"
197 // End: