1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/LogControl.h
12 #ifndef ZYPP_BASE_LOGCONTROL_H
13 #define ZYPP_BASE_LOGCONTROL_H
16 #include <ostream> //for std::endl
18 #include "zypp/base/Logger.h"
19 #include "zypp/base/PtrTypes.h"
20 #include "zypp/Pathname.h"
22 ///////////////////////////////////////////////////////////////////
24 { /////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
30 /** If you want to log the (formated) loglines by yourself,
31 * derive from this, and overload \c writeOut.
32 * Expect \a formated_r to be a formated log line without trailing \c NL.
33 * Ready to be written to the log.
37 virtual void writeOut( const std::string & /*formated_r*/ )
43 /** Base class for ostream based \ref LineWriter */
44 struct StreamLineWriter : public LineWriter
46 StreamLineWriter( std::ostream & str_r ) : _str( &str_r ) {}
48 virtual void writeOut( const std::string & formated_r )
49 { (*_str) << formated_r << std::endl; }
52 StreamLineWriter() : _str( 0 ) {}
56 /** \ref LineWriter to stdout. */
57 struct StdoutLineWriter : public StreamLineWriter
62 /** \ref LineWriter to stderr. */
63 struct StderrLineWriter : public StreamLineWriter
68 /** \ref LineWriter to file.
69 * If \c mode_r is not \c 0, \c file_r persissions are changed
70 * accordingly. \c "-" logs to \c cerr.
72 struct FileLineWriter : public StreamLineWriter
74 FileLineWriter( const Pathname & file_r, mode_t mode_r = 0 );
76 shared_ptr<void> _outs;
79 /////////////////////////////////////////////////////////////////
81 ///////////////////////////////////////////////////////////////////
84 ///////////////////////////////////////////////////////////////////
86 { /////////////////////////////////////////////////////////////////
88 ///////////////////////////////////////////////////////////////////
90 // CLASS NAME : LogControl
92 /** Maintain logfile related options.
93 * \note A Singleton using a Singleton implementation class,
94 * that's why there is no _pimpl like in other classes.
98 friend std::ostream & operator<<( std::ostream & str, const LogControl & obj );
101 /** Singleton access. */
102 static LogControl instance()
103 { return LogControl(); }
106 /** \see \ref log::LineWriter */
107 typedef log::LineWriter LineWriter;
109 /** If you want to format loglines by yourself,
110 * derive from this, and overload \c format.
111 * Return a formated logline without trailing \c NL.
112 * Ready to be written to the log.
116 virtual std::string format( const std::string & /*group_r*/,
117 logger::LogLevel /*level_r*/,
118 const char * /*file_r*/,
119 const char * /*func_r*/,
121 const std::string & /*message_r*/ );
122 virtual ~LineFormater() {}
126 /** Assign a LineFormater.
127 * If you want to format loglines by yourself. NULL installs the
130 void setLineFormater( const shared_ptr<LineFormater> & formater_r );
133 /** Set path for the logfile.
134 * Permission for logfiles is set to 0640 unless an explicit mode_t
135 * value is given. An empty pathname turns off logging. <tt>"-"</tt>
137 * \throw if \a logfile_r is not usable.
139 void logfile( const Pathname & logfile_r );
140 void logfile( const Pathname & logfile_r, mode_t mode_r );
142 /** Turn off logging. */
145 /** Log to std::err. */
149 /** Get the current LineWriter */
150 shared_ptr<LineWriter> getLineWriter() const;
152 /** Assign a LineWriter.
153 * If you want to log the (formated) loglines by yourself.
154 * NULL turns off logging (same as logNothing)
155 * \see \ref log::LineWriter
157 void setLineWriter( const shared_ptr<LineWriter> & writer_r );
160 /** Turn on excessive logging for the lifetime of this object.*/
167 /** Exchange LineWriter for the lifetime of this object.
168 * \see \ref log::LineWriter
172 TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() )
173 : _writer( LogControl::instance().getLineWriter() )
174 { LogControl::instance().setLineWriter( writer_r ); }
176 /** Convenience ctor taking over ownership of an allocated LineWriter.
178 * TmpLineWriter mylw( new log::StderrLineWriter );
181 template<class TLineWriter>
182 TmpLineWriter( TLineWriter * _allocated_r )
183 : _writer( LogControl::instance().getLineWriter() )
184 { LogControl::instance().setLineWriter( shared_ptr<LineWriter>( _allocated_r ) ); }
187 { LogControl::instance().setLineWriter( _writer ); }
190 shared_ptr<LineWriter> _writer;
194 /** Default ctor: Singleton */
198 ///////////////////////////////////////////////////////////////////
200 /** \relates LogControl Stream output */
201 std::ostream & operator<<( std::ostream & str, const LogControl & obj );
203 /////////////////////////////////////////////////////////////////
205 ///////////////////////////////////////////////////////////////////
206 /////////////////////////////////////////////////////////////////
208 ///////////////////////////////////////////////////////////////////
209 #endif // ZYPP_BASE_LOGCONTROL_H