b1cdd4cf95a58d66bdfa74404fb8d0259c125d0d
[senf.git] / senf / Utils / Logger / LogFormat.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief LogFormat non-inline non-template implementation */
25
26 #include "LogFormat.hh"
27 //#include "LogFormat.ih"
28
29 // Custom includes
30 #include <unistd.h>
31 #include <locale>
32 #include <boost/date_time/posix_time/posix_time.hpp>
33 #include <senf/Scheduler/ClockService.hh>
34 #include <senf/Utils/Console/ScopedDirectory.hh>
35 #include <senf/Utils/Console/ParsedCommand.hh>
36
37 //#include "LogFormat.mpp"
38 #define prefix_
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 prefix_ senf::log::detail::LogFormat::LogFormat()
42     : tag_ (detail::getDefaultTag()), noformat_ (false), showTime_ (true),
43       showStream_ (false), showLevel_ (true), showArea_ (true), timeBase_ (-1)
44 {
45     timeFormat("%Y-%m-%d %H:%M:%S.%f-0000");
46 }
47
48 prefix_ senf::log::detail::LogFormat::LogFormat(console::ScopedDirectory<> & dir)
49     : tag_ (detail::getDefaultTag()), noformat_ (false), showTime_ (true),
50       showStream_ (false), showLevel_ (true), showArea_ (true), timeBase_ (-1)
51 {
52     namespace kw = console::kw;
53     namespace fty = console::factory;
54
55     timeFormat("%Y-%m-%d %H:%M:%S.%f-0000");
56
57     dir.add("showTime", fty::Command(&LogFormat::showTime, this)
58             .arg("flag","whether to display the time in log messages",
59                  kw::default_value = true)
60             .doc("Set time display in log messages. If time display is enabled, see the 'timeFormat'\n"
61                  "command to set the time format.") );
62     dir.add("showStream", fty::Command(&LogFormat::showStream, this)
63             .arg("flag","whether to display the stream in log messages",
64                  kw::default_value = true)
65             .doc("Set stream display in log messages.") );
66     dir.add("showLevel", fty::Command(&LogFormat::showLevel, this)
67             .arg("flag","whether to display the log level in log messages",
68                  kw::default_value = true)
69             .doc("Set log level display in log messages.") );
70     dir.add("showArea", fty::Command(&LogFormat::showArea, this)
71             .arg("flag","whether to display the area in log messages",
72                  kw::default_value = true)
73             .doc("Set area display in log messages.") );
74     dir.add("timeFormat", fty::Command(&LogFormat::timeFormat, this)
75             .arg("format","time format")
76             .doc("Set time format. The time format is specified using a format string. This format\n"
77                  "string follows the strftime format.\n"
78                  "\n"
79                  "As additional option, the format string may be set to the empty string. In this\n"
80                  "case the time will be displayed as 'second.nanosecond' value. IN this case, the\n"
81                  "time is displayed relative to the first message after changing the format.") );
82     dir.add("tag", fty::Command(&LogFormat::tag, this)
83             .arg("tag","log message tag prefix")
84             .doc("Every log message is optionally prefixed with a tag value. This value defaults to\n"
85                  "the executable name and pid.") );
86     dir.add("format", fty::Command(&LogFormat::consoleFormat, this)
87             .doc("Show the current log message format.") );
88 }
89
90 prefix_ void senf::log::detail::LogFormat::consoleFormat(std::ostream & os)
91 {
92     if (showTime_)                                           os << "showTime ";
93     if (showStream_)                                         os << "showStream ";
94     if (showLevel_)                                          os << "showLevel ";
95     if (showArea_)                                           os << "showArea ";
96     if (showTime_ || showStream_ || showLevel_ || showArea_) os << "\n";
97     else                                                     os << "(all flags disabled)\n";
98
99     os << "timeFormat \"" << timeFormat_ << "\"\n";
100     os << "tag \""        << tag_        << "\"\n";
101 }
102
103 prefix_ void senf::log::detail::LogFormat::timeFormat(std::string const & format)
104 {
105     timeFormat_ = format;
106     if (format.empty()) {
107         noformat_ = true;
108         timeBase_ = ClockService::now();
109     } else {
110         noformat_ = false;
111         std::locale const & loc (datestream_.getloc());
112         datestream_.imbue( std::locale(
113                                loc, new boost::posix_time::time_facet(format.c_str())) );
114     }
115 }
116
117 prefix_ std::string senf::log::detail::LogFormat::prefix(time_type timestamp,
118                                                          std::string const & stream,
119                                                          std::string const & area,
120                                                          unsigned level)
121 {
122     datestream_.str("");
123
124     if (showTime_) {
125         if (noformat_) {
126             time_type delta (timestamp - timeBase_);
127             datestream_ << std::setfill('0')  << std::right
128                         << std::setw(10) << (delta / 1000000000ll) << '.'
129                         << std::setw(9) << (delta % 1000000000ll);
130         }
131         else
132             datestream_ << senf::ClockService::abstime(timestamp);
133         datestream_ << ' ';
134     }
135     if (!tag_.empty())
136         datestream_ << tag_ << ": ";
137     if (showStream_)
138         datestream_ << '[' << stream << "] ";
139     if (showLevel_)
140         datestream_ << '[' << LEVELNAMES[level] << "] ";
141     if (showArea_ && area != "senf::log::DefaultArea")
142         datestream_ << '[' << area << "] ";
143
144     return datestream_.str();
145 }
146
147 //-/////////////////////////////////////////////////////////////////////////////////////////////////
148
149 prefix_ void senf::log::detail::quoteNonPrintable(std::string & s)
150 {
151     for (std::string::iterator i (s.begin()); i != s.end(); ++i)
152         if (*i < ' ' && *i != '\n')
153             *i = '?';
154 }
155
156 prefix_ std::string senf::log::detail::getDefaultTag()
157 {
158     std::stringstream ss;
159     ss << ::program_invocation_short_name << '[' << ::getpid() << ']';
160     return ss.str();
161 }
162
163 //-/////////////////////////////////////////////////////////////////////////////////////////////////
164 #undef prefix_
165 //#include "LogFormat.mpp"
166
167 \f
168 // Local Variables:
169 // mode: c++
170 // fill-column: 100
171 // comment-column: 40
172 // c-file-style: "senf"
173 // indent-tabs-mode: nil
174 // ispell-local-dictionary: "american"
175 // compile-command: "scons -u test"
176 // End: