X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FLogger%2FIOStreamTarget.cc;h=6441cf8581551b54c1d83e2a3c8f42fd61077f28;hb=5746957b0da2bb228b9284e9493158c113b2bbe0;hp=f70fdd770119f0ff1f5da872c1665baea4a137f2;hpb=ae06fe86f16fdabb7ffb219d255444d2eb4f4f79;p=senf.git diff --git a/Utils/Logger/IOStreamTarget.cc b/Utils/Logger/IOStreamTarget.cc index f70fdd7..6441cf8 100644 --- a/Utils/Logger/IOStreamTarget.cc +++ b/Utils/Logger/IOStreamTarget.cc @@ -1,8 +1,8 @@ // $Id$ // -// Copyright (C) 2007 -// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) -// Kompetenzzentrum fuer NETwork research (NET) +// Copyright (C) 2007 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY // Stefan Bund // // This program is free software; you can redistribute it and/or modify @@ -24,10 +24,16 @@ \brief IOStreamTarget non-inline non-template implementation */ #include "IOStreamTarget.hh" -//#include "IOStreamTarget.ih" +#include "IOStreamTarget.ih" // Custom includes +#include #include +#include +#include +#include +#include +#include "../Scheduler/ClockService.hh" //#include "IOStreamTarget.mpp" #define prefix_ @@ -37,25 +43,93 @@ // senf::log::IOStreamTarget prefix_ senf::log::IOStreamTarget::IOStreamTarget(std::ostream & os) - : stream_(os) + : stream_ (os), tag_ (detail::getDefaultTag()), noformat_ (false), showTime_ (true), + showStream_ (false), showLevel_ (true), showArea_ (true), timeBase_ (-1) { - std::locale const & loc (stream_.getloc()); - if (!std::has_facet(loc)) - stream_.imbue( std::locale(loc, new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S.%f-0000")) ); + std::locale const & loc (datestream_.getloc()); + datestream_.imbue( std::locale( + loc, new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S.%f-0000")) ); + +} + +prefix_ void senf::log::IOStreamTarget::timeFormat(std::string const & format) +{ + if (format.empty()) { + noformat_ = true; + timeBase_ = -1; + } else { + noformat_ = false; + std::locale const & loc (datestream_.getloc()); + datestream_.imbue( std::locale( + loc, new boost::posix_time::time_facet(format.c_str())) ); + } } //////////////////////////////////////// // private members -prefix_ void senf::log::IOStreamTarget::v_write(boost::posix_time::ptime timestamp, +prefix_ void senf::log::IOStreamTarget::v_write(time_type timestamp, std::string const & stream, std::string const & area, unsigned level, std::string const & message) { - stream_ << timestamp << " "; - if (! area.empty()) - stream_ << "[" << area << "] "; - stream_ << message << std::endl; + std::string m (message); + boost::trim_right(m); + detail::quoteNonPrintable(m); + + if (tag_.empty() && !showTime_ && !showStream_ && !showLevel_ && !showArea_) + stream_ << m << std::endl; + else { + typedef boost::char_separator Separator; + typedef boost::tokenizer Tokenizer; + Separator separator ("\n"); + Tokenizer tokenizer (m, separator); + Tokenizer::iterator i (tokenizer.begin()); + Tokenizer::iterator const i_end (tokenizer.end()); + + if (showTime_) { + 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] << "] "; + if (showArea_ && area != "senf::log::DefaultArea") + datestream_ << '[' << area << "] "; + + for (; i != i_end; ++i) + stream_ << datestream_.str() << *i << "\n"; + stream_ << std::flush; + datestream_.str(""); + } +} + +/////////////////////////////////////////////////////////////////////////// + +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////////////////////////////////////////