Utils/Logger: Move log formatting into LogFromat mixin class
[senf.git] / 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 <errno.h>
31 #include <unistd.h>
32 #include <locale>
33 #include <boost/date_time/posix_time/posix_time.hpp>
34 #include "../Scheduler/ClockService.hh"
35
36 //#include "LogFormat.mpp"
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 prefix_ senf::log::detail::LogFormat::LogFormat()
41     : tag_ (detail::getDefaultTag()), noformat_ (false), showTime_ (true),
42       showStream_ (false), showLevel_ (true), showArea_ (true), timeBase_ (-1)
43 {
44     std::locale const & loc (datestream_.getloc());
45     datestream_.imbue( std::locale(
46                            loc, new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S.%f-0000")) );
47 }
48
49 prefix_ void senf::log::detail::LogFormat::timeFormat(std::string const & format)
50 {
51     if (format.empty()) {
52         noformat_ = true;
53         timeBase_ = -1;
54     } else {
55         noformat_ = false;
56         std::locale const & loc (datestream_.getloc());
57         datestream_.imbue( std::locale(
58                                loc, new boost::posix_time::time_facet(format.c_str())) );
59     }
60 }
61
62 prefix_ std::string senf::log::detail::LogFormat::prefix(time_type timestamp,
63                                                          std::string const & stream,
64                                                          std::string const & area,
65                                                          unsigned level)
66 {
67     datestream_.str("");
68
69     if (showTime_) {
70         if (noformat_) {
71             if (timeBase_ == -1) timeBase_ = timestamp;
72             time_type delta (timestamp - timeBase_);
73             datestream_ << std::setfill('0') << std::setw(10)
74                         << (delta / 1000000000ll) << '.'
75                         << std::setfill('0') << std::setw(9)
76                         << (delta % 1000000000ll);
77         }
78         else 
79             datestream_ << senf::ClockService::abstime(timestamp);
80         datestream_ << ' ';
81     }
82     if (!tag_.empty())
83         datestream_ << tag_ << ": ";
84     if (showStream_)
85         datestream_ << '[' << stream << "] ";
86     if (showLevel_)
87         datestream_ << '[' << LEVELNAMES[level] << "] ";
88     if (showArea_ && area != "senf::log::DefaultArea")
89         datestream_ << '[' << area << "] ";
90
91     return datestream_.str();
92 }
93
94 ///////////////////////////////////////////////////////////////////////////
95
96 prefix_ void senf::log::detail::quoteNonPrintable(std::string & s)
97 {
98     for (std::string::iterator i (s.begin()); i != s.end(); ++i)
99         if (*i < ' ' && *i != '\n')
100             *i = '?';
101 }
102
103 prefix_ std::string senf::log::detail::getDefaultTag()
104 {
105     std::stringstream ss;
106     ss << ::program_invocation_short_name << '[' << ::getpid() << ']';
107     return ss.str();
108 }
109
110 ///////////////////////////////cc.e////////////////////////////////////////
111 #undef prefix_
112 //#include "LogFormat.mpp"
113
114 \f
115 // Local Variables:
116 // mode: c++
117 // fill-column: 100
118 // comment-column: 40
119 // c-file-style: "senf"
120 // indent-tabs-mode: nil
121 // ispell-local-dictionary: "american"
122 // compile-command: "scons -u test"
123 // End: