LogControl.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_BASE_LOGCONTROL_H
00013 #define ZYPP_BASE_LOGCONTROL_H
00014 
00015 #include <iosfwd>
00016 
00017 #include "zypp/base/Logger.h"
00018 #include "zypp/base/PtrTypes.h"
00019 #include "zypp/Pathname.h"
00020 
00022 namespace zypp
00023 { 
00024 
00026   namespace log
00027   { 
00028 
00034     struct LineWriter
00035     {
00036       virtual void writeOut( const std::string & /*formated_r*/ )
00037       {}
00038       virtual ~LineWriter()
00039       {}
00040     };
00041 
00043     struct StreamLineWriter : public LineWriter
00044     {
00045       StreamLineWriter( std::ostream & str_r ) : _str( &str_r ) {}
00046 
00047       virtual void writeOut( const std::string & formated_r )
00048       { (*_str) << formated_r << std::endl; }
00049 
00050       protected:
00051         StreamLineWriter() : _str( 0 ) {}
00052         std::ostream *_str;
00053     };
00054 
00056     struct StdoutLineWriter : public StreamLineWriter
00057     {
00058       StdoutLineWriter();
00059     };
00060 
00062     struct StderrLineWriter : public StreamLineWriter
00063     {
00064       StderrLineWriter();
00065     };
00066 
00071     struct FileLineWriter : public StreamLineWriter
00072     {
00073       FileLineWriter( const Pathname & file_r, mode_t mode_r = 0 );
00074       protected:
00075         shared_ptr<void> _outs;
00076     };
00077 
00079   } // namespace log
00081 
00082 
00084   namespace base
00085   { 
00086 
00088     //
00089     //  CLASS NAME : LogControl
00090     //
00095     class LogControl
00096     {
00097       friend std::ostream & operator<<( std::ostream & str, const LogControl & obj );
00098 
00099     public:
00101       static LogControl instance()
00102       { return LogControl(); }
00103 
00104 
00106       typedef log::LineWriter LineWriter;
00107 
00113       struct LineFormater
00114       {
00115         virtual std::string format( const std::string & /*group_r*/,
00116                                     logger::LogLevel    /*level_r*/,
00117                                     const char *        /*file_r*/,
00118                                     const char *        /*func_r*/,
00119                                     int                 /*line_r*/,
00120                                     const std::string & /*message_r*/ );
00121         virtual ~LineFormater() {}
00122       };
00123 
00124     public:
00129       void setLineFormater( const shared_ptr<LineFormater> & formater_r );
00130 
00131     public:
00138       void logfile( const Pathname & logfile_r );
00139       void logfile( const Pathname & logfile_r, mode_t mode_r );
00140 
00142       void logNothing();
00143 
00145       void logToStdErr();
00146 
00147     public:
00149       shared_ptr<LineWriter> getLineWriter() const;
00150 
00156       void setLineWriter( const shared_ptr<LineWriter> & writer_r );
00157 
00158     public:
00160       struct TmpExcessive
00161       {
00162         TmpExcessive();
00163         ~TmpExcessive();
00164       };
00165 
00169       struct TmpLineWriter
00170       {
00171         TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() )
00172           : _writer( LogControl::instance().getLineWriter() )
00173         { LogControl::instance().setLineWriter( writer_r ); }
00174 
00180         template<class _LineWriter>
00181         TmpLineWriter( _LineWriter * _allocated_r )
00182           : _writer( LogControl::instance().getLineWriter() )
00183         { LogControl::instance().setLineWriter( shared_ptr<LineWriter>( _allocated_r ) ); }
00184 
00185         ~TmpLineWriter()
00186         { LogControl::instance().setLineWriter( _writer ); }
00187 
00188       private:
00189         shared_ptr<LineWriter> _writer;
00190       };
00191 
00192     private:
00194       LogControl()
00195       {}
00196     };
00198 
00200     std::ostream & operator<<( std::ostream & str, const LogControl & obj );
00201 
00203   } // namespace base
00206 } // namespace zypp
00208 #endif // ZYPP_BASE_LOGCONTROL_H

doxygen