minor fixes for clang++
[senf.git] / senf / Utils / Logger / FileTarget.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
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 FileTarget non-inline non-template implementation */
30
31 #include "FileTarget.hh"
32 //#include "FileTarget.ih"
33
34 // Custom includes
35 #include <senf/Utils/Console/ParsedCommand.hh>
36 #include <senf/Utils/Console/Variables.hh>
37 #include <senf/Utils/Console/ScopedDirectory.hh>
38 #include <boost/filesystem/path.hpp>
39 #include <boost/version.hpp>
40
41 //#include "FileTarget.mpp"
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 namespace {
46
47     std::string getNodename(std::string const & filename, std::string const & nodename)
48     {
49         if (! nodename.empty())
50             return nodename;
51 #if BOOST_VERSION >= 104600
52         return boost::filesystem::path(filename).leaf().string();
53 #else
54         return boost::filesystem::path(filename).leaf();
55 #endif
56     }
57
58 }
59
60 prefix_ senf::log::FileTarget::FileTarget(std::string const & filename,
61                                           std::string const & nodename)
62     : ofstream_t (filename.c_str(), std::ofstream::app),
63       IOStreamTarget (getNodename(filename, nodename), ofstream_t::member),
64       file_ (filename)
65 {
66     namespace fty = console::factory;
67
68     if (! ofstream_t::member)
69         SENF_THROW_SYSTEM_EXCEPTION("logfile open") << ": " << filename;
70     consoleDir()
71         .add( "reopen",
72               fty::Command(SENF_MEMBINDFNP(void, FileTarget, reopen, ()))
73               .doc("Reopen logfile") );
74     consoleDir()
75         .add("reopen",
76              fty::Command(SENF_MEMBINDFNP(void, FileTarget, reopen, (std::string const &)))
77              .arg("filename","new filename")
78              .overloadDoc("Reopen logfile under new name") );
79     consoleDir()
80         .add("file", fty::Variable(boost::cref(file_))
81              .doc("Show filename log messages are sent to") );
82 }
83
84 prefix_ void senf::log::FileTarget::reopen()
85 {
86     ofstream_t::member.close();
87     ofstream_t::member.open(file_.c_str(), std::ofstream::app);
88 }
89
90 prefix_ void senf::log::FileTarget::reopen(std::string const & file)
91 {
92     file_ = file;
93     reopen();
94     // Rename console directory
95     console::DirectoryNode::ptr parent (consoleDir().node().parent());
96     if (parent)
97         parent->add(file, consoleDir().node().unlink());
98 }
99
100 prefix_ std::string const & senf::log::FileTarget::filename()
101     const
102 {
103     return file_;
104 }
105
106 prefix_ senf::log::FileTarget::RegisterConsole::RegisterConsole()
107 {
108     namespace kw = console::kw;
109     namespace fty = console::factory;
110
111     detail::TargetRegistry::instance().consoleDir()
112         .add("file-target", fty::Command(&RegisterConsole::create)
113              .arg("filename", "name of logfile")
114              .arg("nodename", "name of node in console. Defaults to the files basename",
115                   kw::default_value = "")
116              .doc("Create new file target. Examples:\n"
117                   "\n"
118                   "Create new file target '/var/log/example.log\n"
119                   "    $ file-target \"/var/log/example.log\"\n"
120                   "    <Directory '/sys/log/example.log'>\n"
121                   "\n"
122                   "In a configuration file, create new file target '/var/log/example.log' and set\n"
123                   "some parameters (If written on one line, this works at the console too:\n"
124                   "    /sys/log/file-target \"/var/log/example.log\" mainlog {\n"
125                   "        route (IMPORTANT);             # route all important messages\n"
126                   "        timeFormat \"\";               # use non-formatted time format\n"
127                   "        showArea false;                # don't show log area\n"
128                   "    }\n") );
129 }
130
131 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
132 senf::log::FileTarget::RegisterConsole::create(std::string const & filename,
133                                                std::string const & nodename)
134 {
135     std::auto_ptr<Target> tp (new FileTarget(filename, nodename));
136     Target & target (*tp.get());
137     detail::TargetRegistry::instance().dynamicTarget(tp);
138     return target.consoleDir().node().thisptr();
139 }
140
141 //-/////////////////////////////////////////////////////////////////////////////////////////////////
142 #undef prefix_
143 //#include "FileTarget.mpp"
144
145 \f
146 // Local Variables:
147 // mode: c++
148 // fill-column: 100
149 // comment-column: 40
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"
154 // End: