switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Logger / SyslogTarget.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief SyslogTarget non-inline non-template implementation */
30
31 #include "SyslogTarget.hh"
32 //#include "SyslogTarget.ih"
33
34 // Custom includes
35 #include <senf/Utils/Console/ParsedCommand.hh>
36 #include <senf/Utils/Console/Traits.hh>
37 #include <senf/Utils/Console/ScopedDirectory.hh>
38
39 //#include "SyslogTarget.mpp"
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 int const senf::log::SyslogTarget::LEVELMAP[8] = {
44     0, LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING, LOG_CRIT, LOG_EMERG, 0 };
45
46 prefix_ void senf::log::SyslogTarget::v_write(time_type timestamp, std::string const & stream,
47                                               std::string const & area, unsigned level,
48                                               std::string const & message)
49 {
50     if (area != "senf::log::DefaultArea")
51         syslog(facility_ | LEVELMAP[level], "[%s] %s", area.c_str(), message.c_str());
52     else
53         syslog(facility_ | LEVELMAP[level], "%s", message.c_str());
54 }
55
56 namespace senf {
57 namespace log {
58
59     SENF_CONSOLE_REGISTER_ENUM_MEMBER(SyslogTarget, LogFacility,
60                                       (AUTHPRIV)(CRON)(DAEMON)(FTP)(KERN)(LPR)(MAIL)(NEWS)(SYSLOG)
61                                       (USER)(UUCP)(LOCAL0)(LOCAL1)(LOCAL2)(LOCAL3)(LOCAL4)(LOCAL5)
62                                       (LOCAL6)(LOCAL7));
63
64 }}
65
66 prefix_ senf::log::SyslogTarget::RegisterConsole::RegisterConsole()
67 {
68     namespace kw = console::kw;
69     namespace fty = console::factory;
70
71     detail::TargetRegistry::instance().consoleDir()
72         .add("syslog-target",fty::Command(&RegisterConsole::create)
73              .arg("facility", "syslog facility to send messages to. One of\n"
74                   "                  AUTHPRIV CRON DAEMON FTP KERN LPR MAIL NEWS SYSLOG USER\n"
75                   "                  UUCP LOCAL0 LOCAL1 LOCAL2 LOCAL3 LOCAL4 LOCAL5 LOCAL6 LOCAL7",
76                   kw::default_value = USER)
77              .doc("Create new syslog target. Examples:\n"
78                   "\n"
79                   "Create new syslog target\n"
80                   "    $ syslog-target\n"
81                   "    <Directory '/sys/log/syslog'>\n"
82                   "\n"
83                   "In a configuration file, create new syslog target and set some parameters (If\n"
84                   "written on one line, this works at the console too:\n"
85                   "    /sys/log/syslog-target LOCAL2 {\n"
86                   "        route (IMPORTANT);             # route all important messages\n"
87                   "        timeFormat \"\";               # use non-formatted time format\n"
88                   "        showArea false;                # don't show log area\n"
89                   "    }\n") );
90 }
91
92 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
93 senf::log::SyslogTarget::RegisterConsole::create(LogFacility facility)
94 {
95     std::auto_ptr<Target> tp (new SyslogTarget(facility));
96     Target & target (*tp.get());
97     detail::TargetRegistry::instance().dynamicTarget(tp);
98     return target.consoleDir().node().thisptr();
99 }
100
101 //-/////////////////////////////////////////////////////////////////////////////////////////////////
102 #undef prefix_
103 //#include "SyslogTarget.mpp"
104
105 \f
106 // Local Variables:
107 // mode: c++
108 // fill-column: 100
109 // comment-column: 40
110 // c-file-style: "senf"
111 // indent-tabs-mode: nil
112 // ispell-local-dictionary: "american"
113 // compile-command: "scons -u test"
114 // End: