Imported Upstream version 15.1.0
[platform/upstream/libzypp.git] / zypp / base / LogControl.cc
index a6cb06c..3dda602 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "zypp/base/Logger.h"
 #include "zypp/base/LogControl.h"
+#include "zypp/base/ProfilingFormater.h"
 #include "zypp/base/String.h"
 #include "zypp/Date.h"
 #include "zypp/PathInfo.h"
@@ -24,6 +25,84 @@ using std::endl;
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
+
+#ifndef ZYPP_NDEBUG
+  namespace debug
+  {
+    void osdlog( const std::string & msg_r, unsigned level_r )
+    {
+      // Fg::Black:   30  Bg: 40 Attr::Normal:  22;27
+      // Fg::Red:     31  ...    Attr::Bright:  1
+      // Fg::Green:   32         Attr::Reverse: 7
+      // Fg::Yellow:  33
+      // Fg::Blue:    34
+      // Fg::Magenta: 35
+      // Fg::Cyan:    36
+      // Fg::White:   37
+      // Fg::Default: 39
+      static const char * ansi[] = {
+       "\033[37;40m",          // 0 w
+       "\033[36;40m",          // 1 c
+       "\033[33;1;40m",        // 2 y
+       "\033[32;40m",          // 3 g
+       "\033[31;1;40m",        // 4 r
+       "\033[35;40m",          // 5 m
+      };
+      static const unsigned n = sizeof(ansi)/sizeof(const char *);
+      switch ( level_r )
+      {
+       case 'w': level_r = 0; break;
+       case 'c': level_r = 1; break;
+       case 'y': level_r = 2; break;
+       case 'g': level_r = 3; break;
+       case 'r': level_r = 4; break;
+       case 'm': level_r = 5; break;
+      }
+      std::cerr << ansi[level_r%n] << "OSD[" << msg_r << "]\033[0m" << std::endl;
+    }
+}
+#endif // ZYPP_NDEBUG
+
+  ///////////////////////////////////////////////////////////////////
+  namespace log
+  { /////////////////////////////////////////////////////////////////
+
+    StdoutLineWriter::StdoutLineWriter()
+      : StreamLineWriter( std::cout )
+    {}
+
+    StderrLineWriter::StderrLineWriter()
+      : StreamLineWriter( std::cerr )
+    {}
+
+    FileLineWriter::FileLineWriter( const Pathname & file_r, mode_t mode_r )
+    {
+      if ( file_r == Pathname("-") )
+      {
+        _str = &std::cerr;
+      }
+      else
+      {
+       if ( mode_r )
+       {
+          // not filesystem::assert_file as filesystem:: functions log,
+         // and this FileWriter is not yet in place.
+         int fd = ::open( file_r.c_str(), O_CREAT|O_EXCL, mode_r );
+         if ( fd != -1 )
+           ::close( fd );
+       }
+        // set unbuffered write
+        std::ofstream * fstr = 0;
+        _outs.reset( (fstr = new std::ofstream( file_r.asString().c_str(), std::ios_base::app )) );
+        fstr->rdbuf()->pubsetbuf(0,0);
+        _str = &(*fstr);
+      }
+    }
+
+    /////////////////////////////////////////////////////////////////
+  } // namespace log
+  ///////////////////////////////////////////////////////////////////
+
   ///////////////////////////////////////////////////////////////////
   namespace base
   { /////////////////////////////////////////////////////////////////
@@ -54,37 +133,6 @@ namespace zypp
     namespace logger
     { /////////////////////////////////////////////////////////////////
 
-      ///////////////////////////////////////////////////////////////////
-      // LineWriter
-      ///////////////////////////////////////////////////////////////////
-      struct StdErrWriter : public LogControl::LineWriter
-      {
-        virtual void writeOut( const std::string & formated_r )
-        {
-          std::cerr << formated_r << endl;
-        }
-      };
-      ///////////////////////////////////////////////////////////////////
-      struct FileWriter : public LogControl::LineWriter
-      {
-        FileWriter( const Pathname & logfile_r, mode_t mode_r )
-        {
-          // set unbuffered write
-          _outs.rdbuf()->pubsetbuf(0,0);
-          _outs.open( logfile_r.asString().c_str(), std::ios_base::app );
-          // not filesystem::chmod, as filesystem:: functions log,
-          // and this FileWriter is not yet in place.
-          ::chmod( logfile_r.asString().c_str(), mode_r );
-        }
-        std::ofstream _outs;
-
-        virtual void writeOut( const std::string & formated_r )
-        {
-          _outs << formated_r << endl;
-        }
-      };
-      ///////////////////////////////////////////////////////////////////
-
       inline void putStream( const std::string & group_r, LogLevel level_r,
                              const char * file_r, const char * func_r, int line_r,
                              const std::string & buffer_r );
@@ -136,6 +184,8 @@ namespace zypp
         /** */
         virtual int writeout( const char* s, std::streamsize n )
         {
+         //logger::putStream( _group, _level, _file, _func, _line, _buffer );
+         //return n;
           if ( s && n )
             {
               const char * c = s;
@@ -213,6 +263,9 @@ namespace zypp
       struct LogControlImpl
       {
       public:
+       bool isExcessive()
+       { return _excessive; }
+
         void excessive( bool onOff_r )
         { _excessive = onOff_r; }
 
@@ -237,9 +290,9 @@ namespace zypp
           if ( logfile_r.empty() )
             setLineWriter( shared_ptr<LogControl::LineWriter>() );
           else if ( logfile_r == Pathname( "-" ) )
-            setLineWriter( shared_ptr<LogControl::LineWriter>(new StdErrWriter) );
+            setLineWriter( shared_ptr<LogControl::LineWriter>(new log::StderrLineWriter) );
           else
-            setLineWriter( shared_ptr<LogControl::LineWriter>(new FileWriter(logfile_r, mode_r)) );
+            setLineWriter( shared_ptr<LogControl::LineWriter>(new log::FileLineWriter(logfile_r, mode_r)) );
         }
 
       private:
@@ -301,6 +354,17 @@ namespace zypp
         {
           if ( getenv("ZYPP_LOGFILE") )
             logfile( getenv("ZYPP_LOGFILE") );
+
+          if ( getenv("ZYPP_PROFILING") )
+          {
+            shared_ptr<LogControl::LineFormater> formater(new ProfilingFormater);
+            setLineFormater(formater);
+          }
+        }
+
+        ~LogControlImpl()
+        {
+          _lineWriter.reset();
         }
 
       public:
@@ -310,16 +374,20 @@ namespace zypp
          * destructed. At least destucted after all statics
          * which log from their dtor.
         */
-        static LogControlImpl instance;
+        static LogControlImpl & instance();
       };
       ///////////////////////////////////////////////////////////////////
 
       // 'THE' LogControlImpl singleton
-      LogControlImpl LogControlImpl::instance;
+      inline LogControlImpl & LogControlImpl::instance()
+      {
+        static LogControlImpl _instance;
+        return _instance;
+      }
 
       ///////////////////////////////////////////////////////////////////
 
-      /** \relates LogControl::Impl Stream output */
+      /** \relates LogControlImpl Stream output */
       inline std::ostream & operator<<( std::ostream & str, const LogControlImpl & obj )
       {
         return str << "LogControlImpl";
@@ -331,14 +399,13 @@ namespace zypp
       //
       ///////////////////////////////////////////////////////////////////
 
-      /** That's what logger:: calls.  */
       std::ostream & getStream( const char * group_r,
                                 LogLevel     level_r,
                                 const char * file_r,
                                 const char * func_r,
                                 const int    line_r )
       {
-        return LogControlImpl::instance.getStream( group_r,
+        return LogControlImpl::instance().getStream( group_r,
                                                    level_r,
                                                    file_r,
                                                    func_r,
@@ -350,11 +417,14 @@ namespace zypp
                              const char * file_r, const char * func_r, int line_r,
                              const std::string & buffer_r )
       {
-        LogControlImpl::instance.putStream( group_r, level_r,
+        LogControlImpl::instance().putStream( group_r, level_r,
                                             file_r, func_r, line_r,
                                             buffer_r );
       }
 
+      bool isExcessive()
+      { return LogControlImpl::instance().isExcessive(); }
+
       /////////////////////////////////////////////////////////////////
     } // namespace logger
     ///////////////////////////////////////////////////////////////////
@@ -369,19 +439,25 @@ namespace zypp
     using logger::LogControlImpl;
 
     void LogControl::logfile( const Pathname & logfile_r )
-    { LogControlImpl::instance.logfile( logfile_r ); }
+    { LogControlImpl::instance().logfile( logfile_r ); }
 
     void LogControl::logfile( const Pathname & logfile_r, mode_t mode_r )
-    { LogControlImpl::instance.logfile( logfile_r, mode_r ); }
+    { LogControlImpl::instance().logfile( logfile_r, mode_r ); }
+
+    shared_ptr<LogControl::LineWriter> LogControl::getLineWriter() const
+    { return LogControlImpl::instance().getLineWriter(); }
 
     void LogControl::setLineWriter( const shared_ptr<LineWriter> & writer_r )
-    { LogControlImpl::instance.setLineWriter( writer_r ); }
+    { LogControlImpl::instance().setLineWriter( writer_r ); }
+
+    void LogControl::setLineFormater( const shared_ptr<LineFormater> & formater_r )
+    { LogControlImpl::instance().setLineFormater( formater_r ); }
 
     void LogControl::logNothing()
-    { LogControlImpl::instance.setLineWriter( shared_ptr<LineWriter>() ); }
+    { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>() ); }
 
     void LogControl::logToStdErr()
-    { LogControlImpl::instance.setLineWriter( shared_ptr<LineWriter>( new logger::StdErrWriter ) ); }
+    { LogControlImpl::instance().setLineWriter( shared_ptr<LineWriter>( new log::StderrLineWriter ) ); }
 
     ///////////////////////////////////////////////////////////////////
     //
@@ -389,20 +465,10 @@ namespace zypp
     //
     ///////////////////////////////////////////////////////////////////
     LogControl::TmpExcessive::TmpExcessive()
-    { LogControlImpl::instance.excessive( true ); }
+    { LogControlImpl::instance().excessive( true ); }
     LogControl::TmpExcessive::~TmpExcessive()
-    { LogControlImpl::instance.excessive( false );  }
+    { LogControlImpl::instance().excessive( false );  }
 
-    ///////////////////////////////////////////////////////////////////
-    //
-    // LogControl::TmpLineWriter
-    //
-    ///////////////////////////////////////////////////////////////////
-    LogControl::TmpLineWriter::TmpLineWriter( const shared_ptr<LineWriter> & writer_r )
-    : _writer( LogControlImpl::instance.getLineWriter() )
-    { LogControlImpl::instance.setLineWriter( writer_r ); }
-    LogControl::TmpLineWriter::~TmpLineWriter()
-    { LogControlImpl::instance.setLineWriter( _writer ); }
     /******************************************************************
      **
      **        FUNCTION NAME : operator<<
@@ -410,7 +476,7 @@ namespace zypp
     */
     std::ostream & operator<<( std::ostream & str, const LogControl & obj )
     {
-      return str << LogControlImpl::instance;
+      return str << LogControlImpl::instance();
     }
 
     /////////////////////////////////////////////////////////////////