Utils/Console: Parser based character classifiers
[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.");
85 }
86
87 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
88 senf::log::FileTarget::RegisterConsole::create(std::string const & filename)
89 {
90     std::auto_ptr<Target> tp (new FileTarget(filename));
91     Target & target (*tp.get());
92     detail::TargetRegistry::instance().dynamicTarget(tp);
93     return target.consoleDir().node().thisptr();
94 }
95
96 ///////////////////////////////cc.e////////////////////////////////////////
97 #undef prefix_
98 //#include "FileTarget.mpp"
99
100 \f
101 // Local Variables:
102 // mode: c++
103 // fill-column: 100
104 // comment-column: 40
105 // c-file-style: "senf"
106 // indent-tabs-mode: nil
107 // ispell-local-dictionary: "american"
108 // compile-command: "scons -u test"
109 // End: