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