Socket/Protocols/INet: Implement address input streaming
[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", static_cast<void (*)(INet4SocketAddress const &, LogFacility)>(
88             &RegisterConsole::create))
89         .arg("address", "target address to send log messages to")
90         .arg("facility", "syslog facility to send messages to. One of\n"
91              "                  AUTHPRIV, CRON, DAEMON, FTP, KERN, LPR, MAIL, NEWS, SYSLOG, USER,\n"
92              "                  UUCP, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7",
93              kw::default_value = USER)
94         .doc("Create new udp target. The {address} can be an IPv4 or IPv6 address. If the port\n"
95              "number is omitted, it defaults to the default syslog port 514.");
96     detail::TargetRegistry::instance().consoleDir().add(
97         "udp-target", static_cast<void (*)(INet4Address const &, LogFacility)>(
98             &RegisterConsole::create))
99         .arg("address")
100         .arg("facility", kw::default_value = USER);
101     detail::TargetRegistry::instance().consoleDir().add(
102         "udp-target", static_cast<void (*)(INet6SocketAddress const &, LogFacility)>(
103             &RegisterConsole::create))
104         .arg("address")
105         .arg("facility", kw::default_value = USER);
106     detail::TargetRegistry::instance().consoleDir().add(
107         "udp-target", static_cast<void (*)(INet6Address const &, LogFacility)>(
108             &RegisterConsole::create))
109         .arg("address")
110         .arg("facility", kw::default_value = USER);
111 }
112
113 prefix_ void
114 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet4SocketAddress const & target,
115                                                     LogFacility facility)
116 {
117     detail::TargetRegistry::instance().dynamicTarget(
118         std::auto_ptr<Target>(new SyslogUDPTarget(target, facility)));
119 }
120
121 prefix_ void
122 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet4Address const & target,
123                                                     LogFacility facility)
124 {
125     detail::TargetRegistry::instance().dynamicTarget(
126         std::auto_ptr<Target>(new SyslogUDPTarget(target, facility)));
127 }
128
129 prefix_ void
130 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet6SocketAddress const & target,
131                                                     LogFacility facility)
132 {
133     detail::TargetRegistry::instance().dynamicTarget(
134         std::auto_ptr<Target>(new SyslogUDPTarget(target, facility)));
135 }
136
137 prefix_ void
138 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet6Address const & target,
139                                                     LogFacility facility)
140 {
141     detail::TargetRegistry::instance().dynamicTarget(
142         std::auto_ptr<Target>(new SyslogUDPTarget(target, facility)));
143 }
144
145 ///////////////////////////////cc.e////////////////////////////////////////
146 #undef prefix_
147 //#include "SyslogUDPTarget.mpp"
148
149 \f
150 // Local Variables:
151 // mode: c++
152 // fill-column: 100
153 // comment-column: 40
154 // c-file-style: "senf"
155 // indent-tabs-mode: nil
156 // ispell-local-dictionary: "american"
157 // compile-command: "scons -u test"
158 // End: