///////////////////////////////////////////////////////////////////
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
{ /////////////////////////////////////////////////////////////////
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 );
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:
void LogControl::logfile( const Pathname & logfile_r, mode_t 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( shared_ptr<LineWriter>() ); }
void LogControl::logToStdErr()
- { LogControlImpl::instance.setLineWriter( shared_ptr<LineWriter>( new logger::StdErrWriter ) ); }
+ { LogControlImpl::instance.setLineWriter( shared_ptr<LineWriter>( new log::StderrLineWriter ) ); }
///////////////////////////////////////////////////////////////////
//
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<<
///////////////////////////////////////////////////////////////////
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
{ /////////////////////////////////////////////////////////////////
{ 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.
/** 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:
~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;
};