Utils/Logger: Add console directory to target
[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
34 //#include "SyslogUDPTarget.mpp"
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 prefix_ void senf::log::SyslogUDPTarget::v_write(time_type timestamp, std::string const & stream,
39                                                  std::string const & area, unsigned level,
40                                                  std::string const & message)
41 {
42     std::string m (message);
43     boost::trim_right(m);
44     detail::quoteNonPrintable(m);
45
46     std::stringstream prfstream;
47     // The space after the '>' is there on purpose: It ensures, that the prefix (which may be empty)
48     // or message will not inadvertently be interpreted as date or hostname by a receiving syslog
49     // daemon or proxy
50     prfstream << '<' << (facility_ | senf::log::SyslogTarget::LEVELMAP[level]) << "> "
51               << prefix(timestamp, stream, area, level);
52     std::string const & prf (prfstream.str());
53
54     typedef boost::char_separator<char> Separator;
55     typedef boost::tokenizer<Separator> Tokenizer;
56     Separator separator ("\n");
57     Tokenizer tokenizer (m, separator);
58     Tokenizer::iterator i (tokenizer.begin());
59     Tokenizer::iterator const i_end (tokenizer.end());
60
61     std::string line;
62     unsigned sz (896-prf.size());
63     for (; i != i_end; ++i) 
64         for (unsigned j (0); j < i->size(); j += sz) {
65             line = prf;
66             line += std::string(*i, j, sz);
67             handle_.write(line);
68         }
69 }
70
71 ///////////////////////////////cc.e////////////////////////////////////////
72 #undef prefix_
73 //#include "SyslogUDPTarget.mpp"
74
75 \f
76 // Local Variables:
77 // mode: c++
78 // fill-column: 100
79 // comment-column: 40
80 // c-file-style: "senf"
81 // indent-tabs-mode: nil
82 // ispell-local-dictionary: "american"
83 // compile-command: "scons -u test"
84 // End: