FileTarget.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "FileTarget.hh"
18 //#include "FileTarget.ih"
19 
20 // Custom includes
24 #include <boost/filesystem/path.hpp>
25 #include <boost/version.hpp>
26 
27 //#include "FileTarget.mpp"
28 #define prefix_
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 namespace {
32 
33  std::string getNodename(std::string const & filename, std::string const & nodename)
34  {
35  if (! nodename.empty())
36  return nodename;
37 #if BOOST_VERSION >= 104600
38  return boost::filesystem::path(filename).leaf().string();
39 #else
40  return boost::filesystem::path(filename).leaf();
41 #endif
42  }
43 
44 }
45 
46 prefix_ senf::log::FileTarget::FileTarget(std::string const & filename,
47  std::string const & nodename)
48  : ofstream_t (filename.c_str(), std::ofstream::app),
49  IOStreamTarget (getNodename(filename, nodename), ofstream_t::member),
50  file_ (filename)
51 {
52  namespace fty = console::factory;
53 
54  if (! ofstream_t::member)
55  SENF_THROW_SYSTEM_EXCEPTION("logfile open") << ": " << filename;
56  consoleDir()
57  .add( "reopen",
58  fty::Command(SENF_MEMBINDFNP(void, FileTarget, reopen, ()))
59  .doc("Reopen logfile") );
60  consoleDir()
61  .add("reopen",
62  fty::Command(SENF_MEMBINDFNP(void, FileTarget, reopen, (std::string const &)))
63  .arg("filename","new filename")
64  .overloadDoc("Reopen logfile under new name") );
65  consoleDir()
66  .add("file", fty::Variable(boost::cref(file_))
67  .doc("Show filename log messages are sent to") );
68 }
69 
71 {
72  ofstream_t::member.close();
73  ofstream_t::member.open(file_.c_str(), std::ofstream::app);
74 }
75 
76 prefix_ void senf::log::FileTarget::reopen(std::string const & file)
77 {
78  file_ = file;
79  reopen();
80  // Rename console directory
81  console::DirectoryNode::ptr parent (consoleDir().node().parent());
82  if (parent)
83  parent->add(file, consoleDir().node().unlink());
84 }
85 
87  const
88 {
89  return file_;
90 }
91 
92 prefix_ senf::log::FileTarget::RegisterConsole::RegisterConsole()
93 {
94  namespace kw = console::kw;
95  namespace fty = console::factory;
96 
97  detail::TargetRegistry::instance().consoleDir()
98  .add("file-target", fty::Command(&RegisterConsole::create)
99  .arg("filename", "name of logfile")
100  .arg("nodename", "name of node in console. Defaults to the files basename",
101  kw::default_value = "")
102  .doc("Create new file target. Examples:\n"
103  "\n"
104  "Create new file target '/var/log/example.log\n"
105  " $ file-target \"/var/log/example.log\"\n"
106  " <Directory '/sys/log/example.log'>\n"
107  "\n"
108  "In a configuration file, create new file target '/var/log/example.log' and set\n"
109  "some parameters (If written on one line, this works at the console too:\n"
110  " /sys/log/file-target \"/var/log/example.log\" mainlog {\n"
111  " route (IMPORTANT); # route all important messages\n"
112  " timeFormat \"\"; # use non-formatted time format\n"
113  " showArea false; # don't show log area\n"
114  " }\n") );
115 }
116 
117 prefix_ boost::shared_ptr<senf::console::DirectoryNode>
118 senf::log::FileTarget::RegisterConsole::create(std::string const & filename,
119  std::string const & nodename)
120 {
121  std::unique_ptr<Target> tp (new FileTarget(filename, nodename));
122  Target & target (*tp.get());
123  detail::TargetRegistry::instance().dynamicTarget(std::move(tp));
124  return target.consoleDir().node().thisptr();
125 }
126 
127 //-/////////////////////////////////////////////////////////////////////////////////////////////////
128 #undef prefix_
129 //#include "FileTarget.mpp"
130 
131 
132 // Local Variables:
133 // mode: c++
134 // fill-column: 100
135 // comment-column: 40
136 // c-file-style: "senf"
137 // indent-tabs-mode: nil
138 // ispell-local-dictionary: "american"
139 // compile-command: "scons -u test"
140 // End:
FileTarget(std::string const &filename, std::string const &nodename="")
Construct FileTarget writing to file.
Definition: FileTarget.cc:46
#define SENF_MEMBINDFNP(ret, cls, fn, args)
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
boost::shared_ptr< DirectoryNode > ptr
STL namespace.
Write log messages to arbitrary std::ostream.
senf::console::ScopedDirectory & consoleDir()
Get console/config directory.
Definition: Target.cc:212
Log target writing to a log file.
Definition: FileTarget.hh:52
FileTarget public header.
void reopen()
Reopen log after log-file rotation.
Definition: FileTarget.cc:70
DirectoryNode & node() const
std::string const & filename() const
Return current log file name.
Definition: FileTarget.cc:86
#define prefix_
Definition: FileTarget.cc:28
Logging target base class.
Definition: Target.hh:133
NodeType & add(std::string const &name, boost::shared_ptr< NodeType > node)