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