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>
40 //#include "FileTarget.mpp"
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 std::string getNodename(std::string const & filename, std::string const & nodename)
48 if (! nodename.empty())
50 return boost::filesystem::path(filename).leaf();
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),
61 namespace fty = console::factory;
63 if (! ofstream_t::member)
64 SENF_THROW_SYSTEM_EXCEPTION("logfile open") << ": " << filename;
67 fty::Command(SENF_MEMBINDFNP(void, FileTarget, reopen, ()))
68 .doc("Reopen logfile") );
71 fty::Command(SENF_MEMBINDFNP(void, FileTarget, reopen, (std::string const &)))
72 .arg("filename","new filename")
73 .overloadDoc("Reopen logfile under new name") );
75 .add("file", fty::Variable(boost::cref(file_))
76 .doc("Show filename log messages are sent to") );
79 prefix_ void senf::log::FileTarget::reopen()
81 ofstream_t::member.close();
82 ofstream_t::member.open(file_.c_str(), std::ofstream::app);
85 prefix_ void senf::log::FileTarget::reopen(std::string const & file)
89 // Rename console directory
90 console::DirectoryNode::ptr parent (consoleDir().node().parent());
92 parent->add(file, consoleDir().node().unlink());
95 prefix_ std::string const & senf::log::FileTarget::filename()
101 prefix_ senf::log::FileTarget::RegisterConsole::RegisterConsole()
103 namespace kw = console::kw;
104 namespace fty = console::factory;
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"
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"
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"
126 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
127 senf::log::FileTarget::RegisterConsole::create(std::string const & filename,
128 std::string const & nodename)
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();
136 //-/////////////////////////////////////////////////////////////////////////////////////////////////
138 //#include "FileTarget.mpp"
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"