4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief SyslogUDPTarget non-inline non-template implementation */
26 #include "SyslogUDPTarget.hh"
27 //#include "SyslogUDPTarget.ih"
31 #include <boost/algorithm/string/trim.hpp>
32 #include <boost/tokenizer.hpp>
33 #include "../Console/Console.hh"
35 //#include "SyslogUDPTarget.mpp"
37 ///////////////////////////////cc.p////////////////////////////////////////
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)
43 std::string m (message);
45 detail::quoteNonPrintable(m);
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
51 prfstream << '<' << (facility_ | senf::log::SyslogTarget::LEVELMAP[level]) << "> "
52 << prefix(timestamp, stream, area, level);
53 std::string const & prf (prfstream.str());
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());
63 unsigned sz (896-prf.size());
64 for (; i != i_end; ++i)
65 for (unsigned j (0); j < i->size(); j += sz) {
67 line += std::string(*i, j, sz);
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)
82 prefix_ senf::log::SyslogUDPTarget::RegisterConsole::RegisterConsole()
84 namespace kw = senf::console::kw;
86 detail::TargetRegistry::instance().consoleDir().add(
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(
99 static_cast<senf::console::DirectoryNode::ptr (*)(INet4Address const &, LogFacility)>(
100 &RegisterConsole::create))
102 .arg("facility", kw::default_value = USER);
103 detail::TargetRegistry::instance().consoleDir().add(
105 static_cast<senf::console::DirectoryNode::ptr (*)(INet6SocketAddress const &, LogFacility)>(
106 &RegisterConsole::create))
108 .arg("facility", kw::default_value = USER);
109 detail::TargetRegistry::instance().consoleDir().add(
111 static_cast<senf::console::DirectoryNode::ptr (*)(INet6Address const &, LogFacility)>(
112 &RegisterConsole::create))
114 .arg("facility", kw::default_value = USER);
117 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
118 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet4SocketAddress const & target,
119 LogFacility facility)
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();
127 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
128 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet4Address const & target,
129 LogFacility facility)
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();
137 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
138 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet6SocketAddress const & target,
139 LogFacility facility)
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();
147 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
148 senf::log::SyslogUDPTarget::RegisterConsole::create(senf::INet6Address const & target,
149 LogFacility facility)
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();
157 ///////////////////////////////cc.e////////////////////////////////////////
159 //#include "SyslogUDPTarget.mpp"
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"