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