Utils/Logger: Add IOSteamTarget log format configuration options
g0dil [Wed, 10 Dec 2008 13:56:03 +0000 (13:56 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1012 270642c3-0616-0410-b53a-bc976706d245

Utils/Logger/IOStreamTarget.cc
Utils/Logger/IOStreamTarget.cci [new file with mode: 0644]
Utils/Logger/IOStreamTarget.hh

index 7394455..ca26958 100644 (file)
@@ -43,12 +43,25 @@ 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)
+    : stream_ (os), noformat_ (false), showTime_ (true), showStream_ (false), showLevel_ (true),
+      showArea_ (true) 
 {
-    std::locale const & loc (stream_.getloc());
-    if (!std::has_facet<boost::posix_time::time_facet>(loc))
-        stream_.imbue( std::locale(
+    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;
+    else {
+        noformat_ = false;
+        std::locale const & loc (datestream_.getloc());
+        datestream_.imbue( std::locale(
+                               loc, new boost::posix_time::time_facet(format.c_str())) );
+    }
 }
 
 ////////////////////////////////////////
@@ -61,24 +74,35 @@ prefix_ void senf::log::IOStreamTarget::v_write(time_type timestamp,
 {
     std::string m (boost::trim_right_copy(message));
 
-    typedef boost::char_separator<char> Separator;
-    typedef boost::tokenizer<Separator> Tokenizer;
-    Separator separator ("\n");
-    Tokenizer tokenizer (m, separator);
-    Tokenizer::iterator i (tokenizer.begin());
-    Tokenizer::iterator const i_end (tokenizer.end());
+    if (!showTime_ && !showStream_ && !showLevel_ && !showArea_)
+        stream_ << m << std::endl;
+    else {
+        typedef boost::char_separator<char> Separator;
+        typedef boost::tokenizer<Separator> Tokenizer;
+        Separator separator ("\n");
+        Tokenizer tokenizer (m, separator);
+        Tokenizer::iterator i (tokenizer.begin());
+        Tokenizer::iterator const i_end (tokenizer.end());
 
-    char sep (' ');
+        if (showTime_) {
+            if (noformat_)
+                datestream_ << std::setfill('0') << std::setw(19) << timestamp;
+            else 
+                datestream_ << senf::ClockService::abstime(timestamp);
+            datestream_ << ' ';
+        }
+        if (showStream_)
+            datestream_ << '[' << stream << "] ";
+        if (showLevel_)
+            datestream_ << '[' << LEVELNAMES_[level] << "] ";
+        if (showArea_ && area != "senf::log::DefaultArea")
+            datestream_ << '[' << area << "] ";
 
-    for (; i != i_end; ++i) {
-        stream_ << senf::ClockService::abstime(timestamp) << sep;
-        stream_ << "[" << LEVELNAMES_[level] << "]";
-        if (area != "senf::log::DefaultArea")
-            stream_ << " [" << area << "]";
-        stream_ << " " << *i << "\n";
-        sep = '-';
+        for (; i != i_end; ++i)
+            stream_ << datestream_.str() << *i << "\n";
+        stream_ << std::flush;
+        datestream_.str("");
     }
-    stream_ << std::flush;
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////
diff --git a/Utils/Logger/IOStreamTarget.cci b/Utils/Logger/IOStreamTarget.cci
new file mode 100644 (file)
index 0000000..ffa8289
--- /dev/null
@@ -0,0 +1,65 @@
+// $Id$
+//
+// Copyright (C) 2008 
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief IOStreamTarget inline non-template implementation */
+
+//#include "IOStreamTarget.ih"
+
+// Custom includes
+
+#define prefix_ inline
+///////////////////////////////cci.p///////////////////////////////////////
+
+prefix_ void senf::log::IOStreamTarget::showTime(bool flag)
+{
+    showTime_ = flag;
+}
+
+prefix_ void senf::log::IOStreamTarget::showStream(bool flag)
+{
+    showStream_ = flag;
+}
+
+prefix_ void senf::log::IOStreamTarget::showLevel(bool flag)
+{
+    showLevel_ = flag;
+}
+
+prefix_ void senf::log::IOStreamTarget::showArea(bool flag)
+{
+    showArea_ = flag;
+}
+
+///////////////////////////////cci.e///////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// comment-column: 40
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// End:
index 18003c0..11935be 100644 (file)
@@ -44,19 +44,8 @@ namespace log {
 
         The \e area will be omitted if it is \c senf::log::DefaultArea.
         
-        The date formatting is set using the Boost.DateTime date_facet, e.g.:
-        \code
-        stream.imbue( std::locale(stream.getloc(), 
-                                  new boost::posix_time::time_facet("%Y%m%d %H:%M:%S")) );
-        \endcode
-
-        By default, the date-time will be written in extended ISO format.
-
         \warning The class keeps a reference to the passed stream.
         
-        \note This class will permanently and globally change the date formating of the given
-            stream if no \c boost::posix_time::time_facet has been set.
-
         \ingroup targets
       */
     class IOStreamTarget
@@ -72,6 +61,25 @@ namespace log {
         ///@}
         ///////////////////////////////////////////////////////////////////////////
 
+        void showTime(bool flag = true); ///< Enable or disable output of time field
+        void showStream(bool flag = true); ///< Enable or disable output of stream field
+        void showLevel(bool flag = true); ///< Enable or disable output of log level
+        void showArea(bool flag = true); ///< Enable or disable output of log area
+
+        void timeFormat(std::string const & format);
+                                        ///< Set time format
+                                        /**< The date formatting is set using the Boost.DateTime
+                                             date_facet, e.g.
+                                             \code
+                                                 target.timeFormat("%Y%m%d %H:%M:%S");
+                                             \endcode
+                                             If the \c timeFormat is set to the empty string, the
+                                             time is written out as unformatted ClockService value.
+
+                                             By default, the date-time will be written in extended
+                                             ISO format.
+                                             \param[in] format Date/Time format string */
+        
     protected:
         void v_write(time_type timestamp, std::string const & stream, 
                      std::string const & area, unsigned level, 
@@ -79,13 +87,21 @@ namespace log {
 
     private:
         std::ostream & stream_;
+
+        std::stringstream datestream_;
+        bool noformat_;
+        bool showTime_;
+        bool showStream_;
+        bool showLevel_;
+        bool showArea_;
+
         static char const * const LEVELNAMES_[8];
     };
 
 }}
 
 ///////////////////////////////hh.e////////////////////////////////////////
-//#include "IOStreamTarget.cci"
+#include "IOStreamTarget.cci"
 //#include "IOStreamTarget.ct"
 //#include "IOStreamTarget.cti"
 #endif