Utils/Console: Add position information to Tokens
[senf.git] / 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 "../Console/Console.hh"
31
32 //#include "FileTarget.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 namespace {
37     
38     std::string quoteFilename(std::string filename)
39     {
40         for (std::string::iterator i (filename.begin()); i != filename.end(); ++i)
41             if (! senf::console::CommandParser::isWordChar(*i))
42                 *i = '_';
43         return filename;
44     }
45
46 }
47
48 prefix_ senf::log::FileTarget::FileTarget(std::string const & file)
49     : ofstream_t(file.c_str(), std::ofstream::app), 
50       IOStreamTarget(quoteFilename(file), ofstream_t::member), 
51       file_(file)
52 {
53     consoleDir().add( "reopen", senf::membind(
54                           static_cast<void (FileTarget::*)()>(&FileTarget::reopen),
55                           this))
56         .doc("Reopen logfile");
57     consoleDir().add("reopen", senf::membind(
58                          static_cast<void (FileTarget::*)(std::string const &)>(&FileTarget::reopen),
59                          this))
60         .arg("filename","new filename")
61         .overloadDoc("Reopen logfile under new name");
62 }
63
64 prefix_ void senf::log::FileTarget::reopen()
65 {
66     ofstream_t::member.close();
67     ofstream_t::member.open(file_.c_str(), std::ofstream::app);
68 }
69
70 prefix_ void senf::log::FileTarget::reopen(std::string const & file)
71 {
72     file_ = file;
73     reopen();
74     // Rename console directory
75     console::DirectoryNode::ptr parent (consoleDir().node().parent());
76     if (parent)
77         parent->add(file, consoleDir().node().unlink());
78 }
79
80 prefix_ senf::log::FileTarget::RegisterConsole::RegisterConsole()
81 {
82     detail::TargetRegistry::instance().consoleDir().add("file-target",&RegisterConsole::create)
83         .arg("filename", "name of logfile")
84         .doc("Create new file target. Examples:\n"
85              "\n"
86              "Create new file target '/var/log/example.log\n"
87              "    $ file-target \"/var/log/example.log\"\n"
88              "    <Directory '/sys/log/_var_log_example.log'>\n"
89              "\n"
90              "In a configuration file, create new file target '/var/log/example.log' and set\n"
91              "some parameters (If written on one line, this works at the console too:\n"
92              "    /sys/log/file-target \"/var/log/example.log\" {\n"
93              "        route (IMPORTANT);             # route all important messages\n"
94              "        timeFormat \"\";               # use non-formatted time format\n"
95              "        showArea false;                # don't show log area\n"
96              "    }\n");
97 }
98
99 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
100 senf::log::FileTarget::RegisterConsole::create(std::string const & filename)
101 {
102     std::auto_ptr<Target> tp (new FileTarget(filename));
103     Target & target (*tp.get());
104     detail::TargetRegistry::instance().dynamicTarget(tp);
105     return target.consoleDir().node().thisptr();
106 }
107
108 ///////////////////////////////cc.e////////////////////////////////////////
109 #undef prefix_
110 //#include "FileTarget.mpp"
111
112 \f
113 // Local Variables:
114 // mode: c++
115 // fill-column: 100
116 // comment-column: 40
117 // c-file-style: "senf"
118 // indent-tabs-mode: nil
119 // ispell-local-dictionary: "american"
120 // compile-command: "scons -u test"
121 // End: