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