Author: mlandres Date: Fri Oct 24 12:42:35 2008 New Revision: 11478 URL: http://svn.opensuse.org/viewcvs/zypp?rev=11478&view=rev Log: make log LineWriter public accessible Modified: trunk/libzypp/zypp/base/LogControl.cc trunk/libzypp/zypp/base/LogControl.h Modified: trunk/libzypp/zypp/base/LogControl.cc URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/LogControl.cc?rev=11478&r1=11477&r2=11478&view=diff ============================================================================== --- trunk/libzypp/zypp/base/LogControl.cc (original) +++ trunk/libzypp/zypp/base/LogControl.cc Fri Oct 24 12:42:35 2008 @@ -25,6 +25,37 @@ /////////////////////////////////////////////////////////////////// namespace zypp { ///////////////////////////////////////////////////////////////// + + /////////////////////////////////////////////////////////////////// + namespace log + { ///////////////////////////////////////////////////////////////// + + FileLineWriter::FileLineWriter( const Pathname & file_r, mode_t mode_r ) + { + if ( file_r == Pathname("-") ) + { + _str = &std::cerr; + } + else + { + // 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); + if ( mode_r ) + { + // not filesystem::chmod, as filesystem:: functions log, + // and this FileWriter is not yet in place. + ::chmod( file_r.asString().c_str(), mode_r ); + } + } + } + + ///////////////////////////////////////////////////////////////// + } // namespace log + /////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////// namespace base { ///////////////////////////////////////////////////////////////// @@ -55,37 +86,6 @@ 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 ); @@ -241,9 +241,9 @@ if ( logfile_r.empty() ) setLineWriter( shared_ptrLogControl::LineWriter() ); else if ( logfile_r == Pathname( "-" ) ) - setLineWriter( shared_ptrLogControl::LineWriter(new StdErrWriter) ); + setLineWriter( shared_ptrLogControl::LineWriter(new log::StderrLineWriter) ); else - setLineWriter( shared_ptrLogControl::LineWriter(new FileWriter(logfile_r, mode_r)) ); + setLineWriter( shared_ptrLogControl::LineWriter(new log::FileLineWriter(logfile_r, mode_r)) ); } private: @@ -386,6 +386,9 @@ void LogControl::logfile( const Pathname & logfile_r, mode_t mode_r ) { LogControlImpl::instance.logfile( logfile_r, mode_r ); } + shared_ptrLogControl::LineWriter LogControl::getLineWriter() const + { return LogControlImpl::instance.getLineWriter(); } + void LogControl::setLineWriter( const shared_ptr<LineWriter> & writer_r ) { LogControlImpl::instance.setLineWriter( writer_r ); } @@ -396,7 +399,7 @@ { 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 ) ); } /////////////////////////////////////////////////////////////////// // @@ -408,16 +411,6 @@ LogControl::TmpExcessive::~TmpExcessive() { 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<< Modified: trunk/libzypp/zypp/base/LogControl.h URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/base/LogControl.h?rev=11478&r1=11477&r2=11478&view=diff ============================================================================== --- trunk/libzypp/zypp/base/LogControl.h (original) +++ trunk/libzypp/zypp/base/LogControl.h Fri Oct 24 12:42:35 2008 @@ -21,6 +21,65 @@ /////////////////////////////////////////////////////////////////// namespace zypp { ///////////////////////////////////////////////////////////////// + + /////////////////////////////////////////////////////////////////// + namespace log + { ///////////////////////////////////////////////////////////////// + + /** If you want to log the (formated) loglines by yourself, + * derive from this, and overload \c writeOut. + * Expect \a formated_r to be a formated log line without trailing \c NL. + * Ready to be written to the log. + */ + struct LineWriter + { + virtual void writeOut( const std::string & /*formated_r*/ ) + {} + virtual ~LineWriter() + {} + }; + + /** Base class for ostream based \ref LineWriter */ + struct StreamLineWriter : public LineWriter + { + StreamLineWriter( std::ostream & str_r ) : _str( &str_r ) {} + + virtual void writeOut( const std::string & formated_r ) + { (*_str) << formated_r << std::endl; } + + protected: + StreamLineWriter() : _str( 0 ) {} + std::ostream *_str; + }; + + /** \ref LineWriter to stdout. */ + struct StdoutLineWriter : public StreamLineWriter + { + StdoutLineWriter() : StreamLineWriter( std::cout ) {} + }; + + /** \ref LineWriter to stderr. */ + struct StderrLineWriter : public StreamLineWriter + { + StderrLineWriter() : StreamLineWriter( std::cerr ) {} + }; + + /** \ref LineWriter to file. + * If \c mode_r is not \c 0, \c file_r persissions are changed + * accordingly. \c "-" logs to \c cerr. + */ + struct FileLineWriter : public StreamLineWriter + { + FileLineWriter( const Pathname & file_r, mode_t mode_r = 0 ); + protected: + shared_ptr<void> _outs; + }; + + ///////////////////////////////////////////////////////////////// + } // namespace log + /////////////////////////////////////////////////////////////////// + + /////////////////////////////////////////////////////////////////// namespace base { ///////////////////////////////////////////////////////////////// @@ -43,17 +102,8 @@ { return LogControl(); } - /** If you want to log the (formated) loglines by yourself, - * derive from this, and overload \c writeOut. - * Expect \a formated_r to be a formated log line without trailing \c NL. - * Ready to be written to the log. - */ - struct LineWriter - { - virtual void writeOut( const std::string & /*formated_r*/ ) - {} - virtual ~LineWriter() {} - }; + /** \see \ref log::LineWriter */ + typedef log::LineWriter LineWriter; /** If you want to format loglines by yourself, * derive from this, and overload \c format. @@ -94,10 +144,15 @@ /** Log to std::err. */ void logToStdErr(); + public: + /** Get the current LineWriter */ + shared_ptr<LineWriter> getLineWriter() const; + /** Assign a LineWriter. * If you want to log the (formated) loglines by yourself. * NULL turns off logging (same as logNothing) - */ + * \see \ref log::LineWriter + */ void setLineWriter( const shared_ptr<LineWriter> & writer_r ); public: @@ -108,11 +163,28 @@ ~TmpExcessive(); }; - /** Exchange LineWriter for the lifetime of this object. */ + /** Exchange LineWriter for the lifetime of this object. + * \see \ref log::LineWriter + */ struct TmpLineWriter { - TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() ); - ~TmpLineWriter(); + TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() ) + : _writer( LogControl::instance().getLineWriter() ) + { LogControl::instance().setLineWriter( writer_r ); } + + /** Convenience ctor taking over ownership of an allocated LineWriter. + *\code + * TmpLineWriter mylw( new log::StderrLineWriter ); + * \endcode + */ + template<class _LineWriter> + TmpLineWriter( _LineWriter * _allocated_r ) + : _writer( LogControl::instance().getLineWriter() ) + { LogControl::instance().setLineWriter( shared_ptr<LineWriter>( _allocated_r ) ); } + + ~TmpLineWriter() + { LogControl::instance().setLineWriter( _writer ); } + private: shared_ptr<LineWriter> _writer; }; -- To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org For additional commands, e-mail: zypp-commit+help@opensuse.org