Utils/Console: Implement v_setupComplete() callback and request timeout
[senf.git] / Utils / Logger / IOStreamTarget.cc
index ca26958..6441cf8 100644 (file)
     \brief IOStreamTarget non-inline non-template implementation */
 
 #include "IOStreamTarget.hh"
-//#include "IOStreamTarget.ih"
+#include "IOStreamTarget.ih"
 
 // Custom includes
+#include <errno.h>
 #include <locale>
+#include <sstream>
 #include <boost/algorithm/string/trim.hpp>
 #include <boost/tokenizer.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
+#include "../Scheduler/ClockService.hh"
 
 //#include "IOStreamTarget.mpp"
 #define prefix_
 ///////////////////////////////////////////////////////////////////////////
 // senf::log::IOStreamTarget
 
-char const * const senf::log::IOStreamTarget::LEVELNAMES_[8] = {
-        "NONE", "VERBOSE", "NOTICE", "MESSAGE", "IMPORTANT", "CRITICAL", "FATAL", "DISABLED" };
-
 prefix_ senf::log::IOStreamTarget::IOStreamTarget(std::ostream & os)
-    : stream_ (os), noformat_ (false), showTime_ (true), showStream_ (false), showLevel_ (true),
-      showArea_ (true) 
+    : stream_ (os), tag_ (detail::getDefaultTag()), noformat_ (false), showTime_ (true),
+      showStream_ (false), showLevel_ (true), showArea_ (true), timeBase_ (-1)
 {
     std::locale const & loc (datestream_.getloc());
     datestream_.imbue( std::locale(
@@ -54,9 +54,10 @@ prefix_ senf::log::IOStreamTarget::IOStreamTarget(std::ostream & os)
 
 prefix_ void senf::log::IOStreamTarget::timeFormat(std::string const & format)
 {
-    if (format.empty())
+    if (format.empty()) {
         noformat_ = true;
-    else {
+        timeBase_ = -1;
+    } else {
         noformat_ = false;
         std::locale const & loc (datestream_.getloc());
         datestream_.imbue( std::locale(
@@ -72,9 +73,11 @@ prefix_ void senf::log::IOStreamTarget::v_write(time_type timestamp,
                                                 std::string const & area, unsigned level,
                                                 std::string const & message)
 {
-    std::string m (boost::trim_right_copy(message));
+    std::string m (message);
+    boost::trim_right(m);
+    detail::quoteNonPrintable(m);
 
-    if (!showTime_ && !showStream_ && !showLevel_ && !showArea_)
+    if (tag_.empty() && !showTime_ && !showStream_ && !showLevel_ && !showArea_)
         stream_ << m << std::endl;
     else {
         typedef boost::char_separator<char> Separator;
@@ -85,16 +88,24 @@ prefix_ void senf::log::IOStreamTarget::v_write(time_type timestamp,
         Tokenizer::iterator const i_end (tokenizer.end());
 
         if (showTime_) {
-            if (noformat_)
-                datestream_ << std::setfill('0') << std::setw(19) << timestamp;
+            if (noformat_) {
+                if (timeBase_ == -1) timeBase_ = timestamp;
+                time_type delta (timestamp - timeBase_);
+                datestream_ << std::setfill('0') << std::setw(10)
+                            << (delta / 1000000000ll) << '.'
+                            << std::setfill('0') << std::setw(9)
+                            << (delta % 1000000000ll);
+            }
             else 
                 datestream_ << senf::ClockService::abstime(timestamp);
             datestream_ << ' ';
         }
+        if (!tag_.empty())
+            datestream_ << tag_ << ": ";
         if (showStream_)
             datestream_ << '[' << stream << "] ";
         if (showLevel_)
-            datestream_ << '[' << LEVELNAMES_[level] << "] ";
+            datestream_ << '[' << LEVELNAMES[level] << "] ";
         if (showArea_ && area != "senf::log::DefaultArea")
             datestream_ << '[' << area << "] ";
 
@@ -105,6 +116,22 @@ prefix_ void senf::log::IOStreamTarget::v_write(time_type timestamp,
     }
 }
 
+///////////////////////////////////////////////////////////////////////////
+
+prefix_ void senf::log::detail::quoteNonPrintable(std::string & s)
+{
+    for (std::string::iterator i (s.begin()); i != s.end(); ++i)
+        if (*i < ' ' && *i != '\n')
+            *i = ' ';
+}
+
+prefix_ std::string senf::log::detail::getDefaultTag()
+{
+    std::stringstream ss;
+    ss << ::program_invocation_short_name << '[' << ::getpid() << ']';
+    return ss.str();
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "IOStreamTarget.mpp"