Packets: added tests for dump methods
[senf.git] / Utils / Logger / SyslogUDPTarget.cc
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 non-inline non-template implementation */
25
26 #include "SyslogUDPTarget.hh"
27 //#include "SyslogUDPTarget.ih"
28
29 // Custom includes
30 #include <sstream>
31 #include <boost/algorithm/string/trim.hpp>
32 #include <boost/tokenizer.hpp>
33 #include "../Console/Console.hh"
34
35 //#include "SyslogUDPTarget.mpp"
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 prefix_ void senf::log::SyslogUDPTarget::v_write(time_type timestamp, std::string const & stream,
40                                                  std::string const & area, unsigned level,
41                                                  std::string const & message)
42 {
43     std::string m (message);
44     boost::trim_right(m);
45     detail::quoteNonPrintable(m);
46
47     std::stringstream prfstream;
48     // The space after the '>' is there on purpose: It ensures, that the prefix (which may be empty)
49     // or message will not inadvertently be interpreted as date or hostname by a receiving syslog
50     // daemon or proxy
51     prfstream << '<' << (facility_ | senf::log::SyslogTarget::LEVELMAP[level]) << "> "
52               << prefix(timestamp, stream, area, level);
53     std::string const & prf (prfstream.str());
54
55     typedef boost::char_separator<char> Separator;
56     typedef boost::tokenizer<Separator> Tokenizer;
57     Separator separator ("\n");
58     Tokenizer tokenizer (m, separator);
59     Tokenizer::iterator i (tokenizer.begin());
60     Tokenizer::iterator const i_end (tokenizer.end());
61
62     std::string line;
63     unsigned sz (896-prf.size());
64     for (; i != i_end; ++i) 
65         for (unsigned j (0); j < i->size(); j += sz) {
66             line = prf;
67             line += std::string(*i, j, sz);
68             handle_.write(line);
69         }
70 }
71
72 namespace senf {
73 namespace log {
74
75     SENF_CONSOLE_REGISTER_ENUM_MEMBER(SyslogUDPTarget, LogFacility,
76                                       (AUTHPRIV)(CRON)(DAEMON)(FTP)(KERN)(LPR)(MAIL)(NEWS)(SYSLOG)
77                                       (USER)(UUCP)(LOCAL0)(LOCAL1)(LOCAL2)(LOCAL3)(LOCAL4)(LOCAL5)
78                                       (LOCAL6)(LOCAL7));
79
80 }}
81
82 prefix_ senf::log::SyslogUDPTarget::RegisterConsole::RegisterConsole()
83 {
84     namespace kw = senf::console::kw;
85
86     detail::TargetRegistry::instance().consoleDir().add(
87         "udp-target", 
88         static_cast<senf::console::DirectoryNode::ptr (*)(INet4SocketAddress const &, LogFacility)>(
89             &RegisterConsole::create))
90         .arg("address", "target address to send log messages to")
91         .arg("facility", "syslog facility to send messages to. One of\n"
92              "                  AUTHPRIV CRON DAEMON FTP KERN LPR MAIL NEWS SYSLOG USER\n"
93              "                  UUCP LOCAL0 LOCAL1 LOCAL2 LOCAL3 LOCAL4 LOCAL5 LOCAL6 LOCAL7",
94              kw::default_value = USER)
95         .doc("Create new udp target. The {address} can be an IPv4 or IPv6 address. If the port\n"
96              "number is omitted, it defaults to the default syslog port 514.");
97     detail::TargetRegistry::instance().consoleDir().add(
98         "udp-target", 
99         static_cast<senf::console::DirectoryNode::ptr (*)(INet4Address const &, LogFacility)>(
100             &RegisterConsole::create))
101         .arg("address")
102         .arg("facility", kw::default_value = USER);
103     detail::TargetRegistry::instance().consoleDir().add(
104         "udp-target", 
105         static_cast<senf::console::DirectoryNode::ptr (*)(INet6SocketAddress const &, LogFacility)>(
106             &RegisterConsole::create))
107         .arg("address")
108         .arg("facility", kw::default_value = USER);
109     detail::TargetRegistry::instance().consoleDir().add(
110         "udp-target", 
111         static_cast<senf::console::DirectoryNode::ptr (*)(INet6Address const &, LogFacility)>(
112             &RegisterConsole::create))
113         .arg("address")
114         .arg("facility", kw::default_value = USER);
115 }
116
117 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
118 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet4SocketAddress const & target,
119                                                     LogFacility facility)
120 {
121     std::auto_ptr<Target> tp (new SyslogUDPTarget(target, facility));
122     Target & tg (*tp.get());
123     detail::TargetRegistry::instance().dynamicTarget(tp);
124     return tg.consoleDir().node().thisptr();
125 }
126
127 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
128 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet4Address const & target,
129                                                     LogFacility facility)
130 {
131     std::auto_ptr<Target> tp (new SyslogUDPTarget(target, facility));
132     Target & tg (*tp.get());
133     detail::TargetRegistry::instance().dynamicTarget(tp);
134     return tg.consoleDir().node().thisptr();
135 }
136
137 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
138 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet6SocketAddress const & target,
139                                                     LogFacility facility)
140 {
141     std::auto_ptr<Target> tp (new SyslogUDPTarget(target, facility));
142     Target & tg (*tp.get());
143     detail::TargetRegistry::instance().dynamicTarget(tp);
144     return tg.consoleDir().node().thisptr();
145 }
146
147 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
148 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet6Address const & target,
149                                                     LogFacility facility)
150 {
151     std::auto_ptr<Target> tp (new SyslogUDPTarget(target, facility));
152     Target & tg (*tp.get());
153     detail::TargetRegistry::instance().dynamicTarget(tp);
154     return tg.consoleDir().node().thisptr();
155 }
156
157 ///////////////////////////////cc.e////////////////////////////////////////
158 #undef prefix_
159 //#include "SyslogUDPTarget.mpp"
160
161 \f
162 // Local Variables:
163 // mode: c++
164 // fill-column: 100
165 // comment-column: 40
166 // c-file-style: "senf"
167 // indent-tabs-mode: nil
168 // ispell-local-dictionary: "american"
169 // compile-command: "scons -u test"
170 // End: