#define ZYPP_BASE_LOGCONTROL_H
#include <iosfwd>
+#include <ostream> //for std::endl
#include "zypp/base/Logger.h"
#include "zypp/base/PtrTypes.h"
///////////////////////////////////////////////////////////////////
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();
+ };
+
+ /** \ref LineWriter to stderr. */
+ struct StderrLineWriter : public StreamLineWriter
+ {
+ StderrLineWriter();
+ };
+
+ /** \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.
const std::string & /*message_r*/ );
virtual ~LineFormater() {}
};
-
+
public:
/** Assign a LineFormater.
* If you want to format loglines by yourself. NULL installs the
*/
void setLineFormater( const shared_ptr<LineFormater> & formater_r );
+ public:
/** Set path for the logfile.
* Permission for logfiles is set to 0640 unless an explicit mode_t
* value is given. An empty pathname turns off logging. <tt>"-"</tt>
/** 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 TLineWriter>
+ TmpLineWriter( TLineWriter * _allocated_r )
+ : _writer( LogControl::instance().getLineWriter() )
+ { LogControl::instance().setLineWriter( shared_ptr<LineWriter>( _allocated_r ) ); }
+
+ ~TmpLineWriter()
+ { LogControl::instance().setLineWriter( _writer ); }
+
private:
shared_ptr<LineWriter> _writer;
};
/** Default ctor: Singleton */
LogControl()
{}
- bool _log_microseconds;
};
///////////////////////////////////////////////////////////////////