4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief FileTarget non-inline non-template implementation */
31 #include "FileTarget.hh"
32 //#include "FileTarget.ih"
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 #include <boost/version.hpp>
41 //#include "FileTarget.mpp"
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47 std::string getNodename(std::string const & filename, std::string const & nodename)
49 if (! nodename.empty())
51 #if BOOST_VERSION >= 104600
52 return boost::filesystem::path(filename).leaf().string();
54 return boost::filesystem::path(filename).leaf();
60 prefix_ senf::log::FileTarget::FileTarget(std::string const & filename,
61 std::string const & nodename)
62 : ofstream_t (filename.c_str(), std::ofstream::app),
63 IOStreamTarget (getNodename(filename, nodename), ofstream_t::member),
66 namespace fty = console::factory;
68 if (! ofstream_t::member)
69 SENF_THROW_SYSTEM_EXCEPTION("logfile open") << ": " << filename;
72 fty::Command(SENF_MEMBINDFNP(void, FileTarget, reopen, ()))
73 .doc("Reopen logfile") );
76 fty::Command(SENF_MEMBINDFNP(void, FileTarget, reopen, (std::string const &)))
77 .arg("filename","new filename")
78 .overloadDoc("Reopen logfile under new name") );
80 .add("file", fty::Variable(boost::cref(file_))
81 .doc("Show filename log messages are sent to") );
84 prefix_ void senf::log::FileTarget::reopen()
86 ofstream_t::member.close();
87 ofstream_t::member.open(file_.c_str(), std::ofstream::app);
90 prefix_ void senf::log::FileTarget::reopen(std::string const & file)
94 // Rename console directory
95 console::DirectoryNode::ptr parent (consoleDir().node().parent());
97 parent->add(file, consoleDir().node().unlink());
100 prefix_ std::string const & senf::log::FileTarget::filename()
106 prefix_ senf::log::FileTarget::RegisterConsole::RegisterConsole()
108 namespace kw = console::kw;
109 namespace fty = console::factory;
111 detail::TargetRegistry::instance().consoleDir()
112 .add("file-target", fty::Command(&RegisterConsole::create)
113 .arg("filename", "name of logfile")
114 .arg("nodename", "name of node in console. Defaults to the files basename",
115 kw::default_value = "")
116 .doc("Create new file target. Examples:\n"
118 "Create new file target '/var/log/example.log\n"
119 " $ file-target \"/var/log/example.log\"\n"
120 " <Directory '/sys/log/example.log'>\n"
122 "In a configuration file, create new file target '/var/log/example.log' and set\n"
123 "some parameters (If written on one line, this works at the console too:\n"
124 " /sys/log/file-target \"/var/log/example.log\" mainlog {\n"
125 " route (IMPORTANT); # route all important messages\n"
126 " timeFormat \"\"; # use non-formatted time format\n"
127 " showArea false; # don't show log area\n"
131 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
132 senf::log::FileTarget::RegisterConsole::create(std::string const & filename,
133 std::string const & nodename)
135 std::auto_ptr<Target> tp (new FileTarget(filename, nodename));
136 Target & target (*tp.get());
137 detail::TargetRegistry::instance().dynamicTarget(tp);
138 return target.consoleDir().node().thisptr();
141 //-/////////////////////////////////////////////////////////////////////////////////////////////////
143 //#include "FileTarget.mpp"
149 // comment-column: 40
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"