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