1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/LogControl.h
12 #ifndef ZYPP_BASE_LOGCONTROL_H
13 #define ZYPP_BASE_LOGCONTROL_H
17 #include "zypp/base/Logger.h"
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/Pathname.h"
21 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
29 /** If you want to log the (formated) loglines by yourself,
30 * derive from this, and overload \c writeOut.
31 * Expect \a formated_r to be a formated log line without trailing \c NL.
32 * Ready to be written to the log.
36 virtual void writeOut( const std::string & /*formated_r*/ )
42 /** Base class for ostream based \ref LineWriter */
43 struct StreamLineWriter : public LineWriter
45 StreamLineWriter( std::ostream & str_r ) : _str( &str_r ) {}
47 virtual void writeOut( const std::string & formated_r )
48 { (*_str) << formated_r << std::endl; }
51 StreamLineWriter() : _str( 0 ) {}
55 /** \ref LineWriter to stdout. */
56 struct StdoutLineWriter : public StreamLineWriter
61 /** \ref LineWriter to stderr. */
62 struct StderrLineWriter : public StreamLineWriter
67 /** \ref LineWriter to file.
68 * If \c mode_r is not \c 0, \c file_r persissions are changed
69 * accordingly. \c "-" logs to \c cerr.
71 struct FileLineWriter : public StreamLineWriter
73 FileLineWriter( const Pathname & file_r, mode_t mode_r = 0 );
75 shared_ptr<void> _outs;
78 /////////////////////////////////////////////////////////////////
80 ///////////////////////////////////////////////////////////////////
83 ///////////////////////////////////////////////////////////////////
85 { /////////////////////////////////////////////////////////////////
87 ///////////////////////////////////////////////////////////////////
89 // CLASS NAME : LogControl
91 /** Maintain logfile related options.
92 * \note A Singleton using a Singleton implementation class,
93 * that's why there is no _pimpl like in other classes.
97 friend std::ostream & operator<<( std::ostream & str, const LogControl & obj );
100 /** Singleton access. */
101 static LogControl instance()
102 { return LogControl(); }
105 /** \see \ref log::LineWriter */
106 typedef log::LineWriter LineWriter;
108 /** If you want to format loglines by yourself,
109 * derive from this, and overload \c format.
110 * Return a formated logline without trailing \c NL.
111 * Ready to be written to the log.
115 virtual std::string format( const std::string & /*group_r*/,
116 logger::LogLevel /*level_r*/,
117 const char * /*file_r*/,
118 const char * /*func_r*/,
120 const std::string & /*message_r*/ );
121 virtual ~LineFormater() {}
125 /** Assign a LineFormater.
126 * If you want to format loglines by yourself. NULL installs the
129 void setLineFormater( const shared_ptr<LineFormater> & formater_r );
132 /** Set path for the logfile.
133 * Permission for logfiles is set to 0640 unless an explicit mode_t
134 * value is given. An empty pathname turns off logging. <tt>"-"</tt>
136 * \throw if \a logfile_r is not usable.
138 void logfile( const Pathname & logfile_r );
139 void logfile( const Pathname & logfile_r, mode_t mode_r );
141 /** Turn off logging. */
144 /** Log to std::err. */
148 /** Get the current LineWriter */
149 shared_ptr<LineWriter> getLineWriter() const;
151 /** Assign a LineWriter.
152 * If you want to log the (formated) loglines by yourself.
153 * NULL turns off logging (same as logNothing)
154 * \see \ref log::LineWriter
156 void setLineWriter( const shared_ptr<LineWriter> & writer_r );
159 /** Turn on excessive logging for the lifetime of this object.*/
166 /** Exchange LineWriter for the lifetime of this object.
167 * \see \ref log::LineWriter
171 TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() )
172 : _writer( LogControl::instance().getLineWriter() )
173 { LogControl::instance().setLineWriter( writer_r ); }
175 /** Convenience ctor taking over ownership of an allocated LineWriter.
177 * TmpLineWriter mylw( new log::StderrLineWriter );
180 template<class TLineWriter>
181 TmpLineWriter( TLineWriter * _allocated_r )
182 : _writer( LogControl::instance().getLineWriter() )
183 { LogControl::instance().setLineWriter( shared_ptr<LineWriter>( _allocated_r ) ); }
186 { LogControl::instance().setLineWriter( _writer ); }
189 shared_ptr<LineWriter> _writer;
193 /** Default ctor: Singleton */
197 ///////////////////////////////////////////////////////////////////
199 /** \relates LogControl Stream output */
200 std::ostream & operator<<( std::ostream & str, const LogControl & obj );
202 /////////////////////////////////////////////////////////////////
204 ///////////////////////////////////////////////////////////////////
205 /////////////////////////////////////////////////////////////////
207 ///////////////////////////////////////////////////////////////////
208 #endif // ZYPP_BASE_LOGCONTROL_H