Move sourcecode into 'senf/' directory
[senf.git] / senf / 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::init()
40 {
41     namespace kw = senf::console::kw;
42
43     consoleDir().remove("format");
44     consoleDir().add("format", senf::membind(&SyslogUDPTarget::consoleFormat, this))
45         .doc("Show the current log message format.");
46     consoleDir().add("syslog", senf::membind(
47                          static_cast<void (SyslogUDPTarget::*)(bool)>(&SyslogUDPTarget::syslog),
48                          this))
49         .arg("flag","new syslog format state",
50              kw::default_value=true)
51         .doc("Change the syslog format flag. By default, syslog formating is enabled. In this\n"
52              "state, the udp target will send out minimal but valid syslog format messages.\n"
53              "\n"
54              "Disabling syslog format will remove the syslog prefix. Log messages will then be\n"
55              "sent using plain UDP.");
56 }
57
58 prefix_ void senf::log::SyslogUDPTarget::v_write(time_type timestamp, std::string const & stream,
59                                                  std::string const & area, unsigned level,
60                                                  std::string const & message)
61 {
62     std::string m (message);
63     boost::trim_right(m);
64     detail::quoteNonPrintable(m);
65
66     std::stringstream prfstream;
67     // The space after the '>' is there on purpose: It ensures, that the prefix (which may be empty)
68     // or message will not inadvertently be interpreted as date or hostname by a receiving syslog
69     // daemon or proxy
70     if (syslogFormat_)
71         prfstream << '<' << (facility_ | senf::log::SyslogTarget::LEVELMAP[level]) << "> ";
72     prfstream << prefix(timestamp, stream, area, level);
73     std::string const & prf (prfstream.str());
74
75     typedef boost::char_separator<char> Separator;
76     typedef boost::tokenizer<Separator> Tokenizer;
77     Separator separator ("\n");
78     Tokenizer tokenizer (m, separator);
79     Tokenizer::iterator i (tokenizer.begin());
80     Tokenizer::iterator const i_end (tokenizer.end());
81
82     std::string line;
83     unsigned sz (896-prf.size());
84     for (; i != i_end; ++i) 
85         for (unsigned j (0); j < i->size(); j += sz) {
86             line = prf;
87             line += std::string(*i, j, sz);
88             handle_.write(line);
89         }
90 }
91
92 prefix_ void senf::log::SyslogUDPTarget::consoleFormat(std::ostream & os)
93 {
94     LogFormat::consoleFormat(os);
95     os << "syslog prefix " << (syslogFormat_ ? "enabled" : "disabled") << "\n";
96 }
97
98 namespace senf {
99 namespace log {
100
101     SENF_CONSOLE_REGISTER_ENUM_MEMBER(SyslogUDPTarget, LogFacility,
102                                       (AUTHPRIV)(CRON)(DAEMON)(FTP)(KERN)(LPR)(MAIL)(NEWS)(SYSLOG)
103                                       (USER)(UUCP)(LOCAL0)(LOCAL1)(LOCAL2)(LOCAL3)(LOCAL4)(LOCAL5)
104                                       (LOCAL6)(LOCAL7));
105
106 }}
107
108 prefix_ senf::log::SyslogUDPTarget::RegisterConsole::RegisterConsole()
109 {
110     namespace kw = senf::console::kw;
111
112     detail::TargetRegistry::instance().consoleDir().add(
113         "udp-target", 
114         static_cast<senf::console::DirectoryNode::ptr (*)(INet4SocketAddress const &, LogFacility)>(
115             &RegisterConsole::create))
116         .arg("address", "target address to send log messages to")
117         .arg("facility", "syslog facility to send messages to. One of\n"
118              "                  AUTHPRIV CRON DAEMON FTP KERN LPR MAIL NEWS SYSLOG USER\n"
119              "                  UUCP LOCAL0 LOCAL1 LOCAL2 LOCAL3 LOCAL4 LOCAL5 LOCAL6 LOCAL7",
120              kw::default_value = USER)
121         .doc("Create new udp target. The {address} can be an IPv4 or IPv6 address. If the port\n"
122              "number is omitted, it defaults to the default syslog port 514. Examples:\n"
123              "\n"
124              "Create new udp target sending messages to the syslog daemon running at localhost\n"
125              "    $ udp-target localhost\n"
126              "    <Directory '/sys/log/udp-127.0.0.1:514'>\n"
127              "\n"
128              "In a configuration file, create new udp target and set some parameters (If\n"
129              "written on one line, this works at the console too:\n"
130              "    /sys/log/udp-target localhost:2345 LOCAL2 {\n"
131              "        route (IMPORTANT);             # route all important messages\n"
132              "        timeFormat \"\";               # use non-formatted time format\n"
133              "        showArea false;                # don't show log area\n"
134              "        syslog false;                  # no syslog format, just plain udp\n"
135              "    }\n");
136     detail::TargetRegistry::instance().consoleDir().add(
137         "udp-target", 
138         static_cast<senf::console::DirectoryNode::ptr (*)(INet4Address const &, LogFacility)>(
139             &RegisterConsole::create))
140         .arg("address")
141         .arg("facility", kw::default_value = USER);
142     detail::TargetRegistry::instance().consoleDir().add(
143         "udp-target", 
144         static_cast<senf::console::DirectoryNode::ptr (*)(INet6SocketAddress const &, LogFacility)>(
145             &RegisterConsole::create))
146         .arg("address")
147         .arg("facility", kw::default_value = USER);
148     detail::TargetRegistry::instance().consoleDir().add(
149         "udp-target", 
150         static_cast<senf::console::DirectoryNode::ptr (*)(INet6Address const &, LogFacility)>(
151             &RegisterConsole::create))
152         .arg("address")
153         .arg("facility", kw::default_value = USER);
154 }
155
156 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
157 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet4SocketAddress const & target,
158                                                     LogFacility facility)
159 {
160     std::auto_ptr<Target> tp (new SyslogUDPTarget(target, facility));
161     Target & tg (*tp.get());
162     detail::TargetRegistry::instance().dynamicTarget(tp);
163     return tg.consoleDir().node().thisptr();
164 }
165
166 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
167 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet4Address const & target,
168                                                     LogFacility facility)
169 {
170     std::auto_ptr<Target> tp (new SyslogUDPTarget(target, facility));
171     Target & tg (*tp.get());
172     detail::TargetRegistry::instance().dynamicTarget(tp);
173     return tg.consoleDir().node().thisptr();
174 }
175
176 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
177 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet6SocketAddress const & target,
178                                                     LogFacility facility)
179 {
180     std::auto_ptr<Target> tp (new SyslogUDPTarget(target, facility));
181     Target & tg (*tp.get());
182     detail::TargetRegistry::instance().dynamicTarget(tp);
183     return tg.consoleDir().node().thisptr();
184 }
185
186 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
187 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet6Address const & target,
188                                                     LogFacility facility)
189 {
190     std::auto_ptr<Target> tp (new SyslogUDPTarget(target, facility));
191     Target & tg (*tp.get());
192     detail::TargetRegistry::instance().dynamicTarget(tp);
193     return tg.consoleDir().node().thisptr();
194 }
195
196 ///////////////////////////////cc.e////////////////////////////////////////
197 #undef prefix_
198 //#include "SyslogUDPTarget.mpp"
199
200 \f
201 // Local Variables:
202 // mode: c++
203 // fill-column: 100
204 // comment-column: 40
205 // c-file-style: "senf"
206 // indent-tabs-mode: nil
207 // ispell-local-dictionary: "american"
208 // compile-command: "scons -u test"
209 // End: