4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief FileTarget non-inline non-template implementation */
26 #include "FileTarget.hh"
27 //#include "FileTarget.ih"
30 #include <senf/Utils/Console/Console.hh>
31 #include <senf/Utils/Exception.hh>
32 #include <boost/filesystem/path.hpp>
34 //#include "FileTarget.mpp"
36 ///////////////////////////////cc.p////////////////////////////////////////
40 std::string getNodename(std::string const & filename, std::string const & nodename)
42 if (! nodename.empty())
44 return boost::filesystem::path(filename).leaf();
49 prefix_ senf::log::FileTarget::FileTarget(std::string const & filename,
50 std::string const & nodename)
51 : ofstream_t (filename.c_str(), std::ofstream::app),
52 IOStreamTarget (getNodename(filename, nodename), ofstream_t::member),
55 if (! ofstream_t::member)
56 SENF_THROW_SYSTEM_EXCEPTION("logfile open") << ": " << filename;
57 consoleDir().add( "reopen", senf::membind(
58 static_cast<void (FileTarget::*)()>(&FileTarget::reopen),
60 .doc("Reopen logfile");
61 consoleDir().add("reopen", senf::membind(
62 static_cast<void (FileTarget::*)(std::string const &)>(&FileTarget::reopen),
64 .arg("filename","new filename")
65 .overloadDoc("Reopen logfile under new name");
66 consoleDir().add("file", boost::cref(file_))
67 .doc("Show filename log messages are sent to");
70 prefix_ void senf::log::FileTarget::reopen()
72 ofstream_t::member.close();
73 ofstream_t::member.open(file_.c_str(), std::ofstream::app);
76 prefix_ void senf::log::FileTarget::reopen(std::string const & file)
80 // Rename console directory
81 console::DirectoryNode::ptr parent (consoleDir().node().parent());
83 parent->add(file, consoleDir().node().unlink());
86 prefix_ std::string const & senf::log::FileTarget::filename()
92 prefix_ senf::log::FileTarget::RegisterConsole::RegisterConsole()
94 namespace kw = senf::console::kw;
96 detail::TargetRegistry::instance().consoleDir().add("file-target",&RegisterConsole::create)
97 .arg("filename", "name of logfile")
98 .arg("nodename", "name of node in console. Defaults to the files basename",
99 kw::default_value = "")
100 .doc("Create new file target. Examples:\n"
102 "Create new file target '/var/log/example.log\n"
103 " $ file-target \"/var/log/example.log\"\n"
104 " <Directory '/sys/log/example.log'>\n"
106 "In a configuration file, create new file target '/var/log/example.log' and set\n"
107 "some parameters (If written on one line, this works at the console too:\n"
108 " /sys/log/file-target \"/var/log/example.log\" mainlog {\n"
109 " route (IMPORTANT); # route all important messages\n"
110 " timeFormat \"\"; # use non-formatted time format\n"
111 " showArea false; # don't show log area\n"
115 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
116 senf::log::FileTarget::RegisterConsole::create(std::string const & filename,
117 std::string const & nodename)
119 std::auto_ptr<Target> tp (new FileTarget(filename, nodename));
120 Target & target (*tp.get());
121 detail::TargetRegistry::instance().dynamicTarget(tp);
122 return target.consoleDir().node().thisptr();
125 ///////////////////////////////cc.e////////////////////////////////////////
127 //#include "FileTarget.mpp"
133 // comment-column: 40
134 // c-file-style: "senf"
135 // indent-tabs-mode: nil
136 // ispell-local-dictionary: "american"
137 // compile-command: "scons -u test"