LogFormat.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 "LogFormat.hh"
18 //#include "LogFormat.ih"
19 
20 // Custom includes
21 #include <unistd.h>
22 #include <locale>
23 #include <boost/date_time/posix_time/posix_time.hpp>
27 
28 //#include "LogFormat.mpp"
29 #define prefix_
30 //-/////////////////////////////////////////////////////////////////////////////////////////////////
31 
33  : tag_ (detail::getDefaultTag()), timeBase_ (-1), noformat_ (false), showTime_ (true),
34  showStream_ (false), showLevel_ (true), showArea_ (true)
35 {
36  timeFormat("%Y-%m-%d %H:%M:%S.%f-0000");
37 }
38 
40  : tag_ (detail::getDefaultTag()), timeBase_ (-1), noformat_ (false), showTime_ (true),
41  showStream_ (false), showLevel_ (true), showArea_ (true)
42 {
43  namespace kw = console::kw;
44  namespace fty = console::factory;
45 
46  timeFormat("%Y-%m-%d %H:%M:%S.%f-0000");
47 
48  dir.add("showTime", fty::Command(&LogFormat::showTime, this)
49  .arg("flag","whether to display the time in log messages",
50  kw::default_value = true)
51  .doc("Set time display in log messages. If time display is enabled, see the 'timeFormat'\n"
52  "command to set the time format.") );
53  dir.add("showStream", fty::Command(&LogFormat::showStream, this)
54  .arg("flag","whether to display the stream in log messages",
55  kw::default_value = true)
56  .doc("Set stream display in log messages.") );
57  dir.add("showLevel", fty::Command(&LogFormat::showLevel, this)
58  .arg("flag","whether to display the log level in log messages",
59  kw::default_value = true)
60  .doc("Set log level display in log messages.") );
61  dir.add("showArea", fty::Command(&LogFormat::showArea, this)
62  .arg("flag","whether to display the area in log messages",
63  kw::default_value = true)
64  .doc("Set area display in log messages.") );
65  dir.add("timeFormat", fty::Command(&LogFormat::timeFormat, this)
66  .arg("format","time format")
67  .doc("Set time format. The time format is specified using a format string. This format\n"
68  "string follows the strftime format.\n"
69  "\n"
70  "As additional option, the format string may be set to the empty string. In this\n"
71  "case the time will be displayed as 'second.nanosecond' value. IN this case, the\n"
72  "time is displayed relative to the first message after changing the format.") );
73  dir.add("tag", fty::Command(&LogFormat::tag, this)
74  .arg("tag","log message tag prefix")
75  .doc("Every log message is optionally prefixed with a tag value. This value defaults to\n"
76  "the executable name and pid.") );
77  dir.add("format", fty::Command(&LogFormat::consoleFormat, this)
78  .doc("Show the current log message format.") );
79 }
80 
82 {
83  if (showTime_) os << "showTime ";
84  if (showStream_) os << "showStream ";
85  if (showLevel_) os << "showLevel ";
86  if (showArea_) os << "showArea ";
87  if (showTime_ || showStream_ || showLevel_ || showArea_) os << "\n";
88  else os << "(all flags disabled)\n";
89 
90  os << "timeFormat \"" << timeFormat_ << "\"\n";
91  os << "tag \"" << tag_ << "\"\n";
92 }
93 
94 prefix_ void senf::log::detail::LogFormat::timeFormat(std::string const & format)
95 {
96  timeFormat_ = format;
97  if (format.empty()) {
98  noformat_ = true;
100  } else {
101  noformat_ = false;
102  std::locale const & loc (datestream_.getloc());
103  datestream_.imbue( std::locale(
104  loc, new boost::posix_time::time_facet(format.c_str())) );
105  }
106 }
107 
109  std::string const & stream,
110  std::string const & area,
111  unsigned level)
112 {
113  datestream_.str("");
114 
115  if (showTime_) {
116  if (noformat_) {
117  time_type delta (timestamp - timeBase_);
118  datestream_ << std::setfill('0') << std::right
119  << std::setw(10) << (delta / 1000000000ll) << '.'
120  << std::setw(9) << (delta % 1000000000ll);
121  }
122  else
124  datestream_ << ' ';
125  }
126  if (!tag_.empty())
127  datestream_ << tag_ << ": ";
128  if (showStream_)
129  datestream_ << '[' << stream << "] ";
130  if (showLevel_)
131  datestream_ << '[' << LEVELNAMES[level] << "] ";
132  if (showArea_ && area != "senf::log::DefaultArea")
133  datestream_ << '[' << area << "] ";
134 
135  return datestream_.str();
136 }
137 
138 //-/////////////////////////////////////////////////////////////////////////////////////////////////
139 
141 {
142  for (std::string::iterator i (s.begin()); i != s.end(); ++i)
143  if (*i < ' ' && *i != '\n')
144  *i = '?';
145 }
146 
148 {
149  std::stringstream ss;
150  ss << ::program_invocation_short_name << '[' << ::getpid() << ']';
151  return ss.str();
152 }
153 
154 //-/////////////////////////////////////////////////////////////////////////////////////////////////
155 #undef prefix_
156 //#include "LogFormat.mpp"
157 
158 
159 // Local Variables:
160 // mode: c++
161 // fill-column: 100
162 // comment-column: 40
163 // c-file-style: "senf"
164 // indent-tabs-mode: nil
165 // ispell-local-dictionary: "american"
166 // compile-command: "scons -u test"
167 // End:
void showStream(bool flag=true)
Enable or disable output of stream field.
#define prefix_
Definition: LogFormat.cc:29
void showLevel(bool flag=true)
Enable or disable output of log level.
void quoteNonPrintable(std::string &s)
Definition: LogFormat.cc:140
void timeFormat(std::string const &format)
Set time format.
Definition: LogFormat.cc:94
void showTime(bool flag=true)
Enable or disable output of time field.
static SENF_CLOCKSERVICE_CONSTEXPR int64_type in_nanoseconds(clock_type const &v)
LogFormat public header.
static abstime_type abstime(clock_type const &clock)
void showArea(bool flag=true)
Enable or disable output of log area.
std::string prefix(time_type timestamp, std::string const &stream, std::string const &area, unsigned level)
Definition: LogFormat.cc:108
void consoleFormat(std::ostream &os)
Definition: LogFormat.cc:81
std::string getDefaultTag()
Definition: LogFormat.cc:147
config::time_type time_type
Definition: TimeSource.hh:31
static clock_type now()
void tag(std::string const &tag)
Set tag (log line prefix)
__s8 delta
NodeType & add(std::string const &name, boost::shared_ptr< NodeType > node)
static SENF_CLOCKSERVICE_CONSTEXPR clock_type nanoseconds(int64_type const &v)