Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / base / LogControl.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/LogControl.h
10  *
11 */
12 #ifndef ZYPP_BASE_LOGCONTROL_H
13 #define ZYPP_BASE_LOGCONTROL_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/Logger.h"
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/Pathname.h"
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24
25   ///////////////////////////////////////////////////////////////////
26   namespace log
27   { /////////////////////////////////////////////////////////////////
28
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.
33      */
34     struct LineWriter
35     {
36       virtual void writeOut( const std::string & /*formated_r*/ )
37       {}
38       virtual ~LineWriter()
39       {}
40     };
41
42     /** Base class for ostream based \ref LineWriter */
43     struct StreamLineWriter : public LineWriter
44     {
45       StreamLineWriter( std::ostream & str_r ) : _str( &str_r ) {}
46
47       virtual void writeOut( const std::string & formated_r )
48       { (*_str) << formated_r << std::endl; }
49
50       protected:
51         StreamLineWriter() : _str( 0 ) {}
52         std::ostream *_str;
53     };
54
55     /** \ref LineWriter to stdout. */
56     struct StdoutLineWriter : public StreamLineWriter
57     {
58       StdoutLineWriter();
59     };
60
61     /** \ref LineWriter to stderr. */
62     struct StderrLineWriter : public StreamLineWriter
63     {
64       StderrLineWriter();
65     };
66
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.
70     */
71     struct FileLineWriter : public StreamLineWriter
72     {
73       FileLineWriter( const Pathname & file_r, mode_t mode_r = 0 );
74       protected:
75         shared_ptr<void> _outs;
76     };
77
78     /////////////////////////////////////////////////////////////////
79   } // namespace log
80   ///////////////////////////////////////////////////////////////////
81
82
83   ///////////////////////////////////////////////////////////////////
84   namespace base
85   { /////////////////////////////////////////////////////////////////
86
87     ///////////////////////////////////////////////////////////////////
88     //
89     //  CLASS NAME : LogControl
90     //
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.
94     */
95     class LogControl
96     {
97       friend std::ostream & operator<<( std::ostream & str, const LogControl & obj );
98
99     public:
100       /** Singleton access. */
101       static LogControl instance()
102       { return LogControl(); }
103
104
105       /** \see \ref log::LineWriter */
106       typedef log::LineWriter LineWriter;
107
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.
112       */
113       struct LineFormater
114       {
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*/,
119                                     int                 /*line_r*/,
120                                     const std::string & /*message_r*/ );
121         virtual ~LineFormater() {}
122       };
123
124     public:
125       /** Assign a LineFormater.
126        * If you want to format loglines by yourself. NULL installs the
127        * default formater.
128       */
129       void setLineFormater( const shared_ptr<LineFormater> & formater_r );
130
131     public:
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>
135        * logs to std::err.
136        * \throw if \a logfile_r is not usable.
137       */
138       void logfile( const Pathname & logfile_r );
139       void logfile( const Pathname & logfile_r, mode_t mode_r );
140
141       /** Turn off logging. */
142       void logNothing();
143
144       /** Log to std::err. */
145       void logToStdErr();
146
147     public:
148       /** Get the current LineWriter */
149       shared_ptr<LineWriter> getLineWriter() const;
150
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
155        */
156       void setLineWriter( const shared_ptr<LineWriter> & writer_r );
157
158     public:
159       /** Turn on excessive logging for the lifetime of this object.*/
160       struct TmpExcessive
161       {
162         TmpExcessive();
163         ~TmpExcessive();
164       };
165
166       /** Exchange LineWriter for the lifetime of this object.
167        * \see \ref log::LineWriter
168       */
169       struct TmpLineWriter
170       {
171         TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() )
172           : _writer( LogControl::instance().getLineWriter() )
173         { LogControl::instance().setLineWriter( writer_r ); }
174
175         /** Convenience ctor taking over ownership of an allocated LineWriter.
176          *\code
177          * TmpLineWriter mylw( new log::StderrLineWriter );
178          * \endcode
179         */
180         template<class TLineWriter>
181         TmpLineWriter( TLineWriter * _allocated_r )
182           : _writer( LogControl::instance().getLineWriter() )
183         { LogControl::instance().setLineWriter( shared_ptr<LineWriter>( _allocated_r ) ); }
184
185         ~TmpLineWriter()
186         { LogControl::instance().setLineWriter( _writer ); }
187
188       private:
189         shared_ptr<LineWriter> _writer;
190       };
191
192     private:
193       /** Default ctor: Singleton */
194       LogControl()
195       {}
196     };
197     ///////////////////////////////////////////////////////////////////
198
199     /** \relates LogControl Stream output */
200     std::ostream & operator<<( std::ostream & str, const LogControl & obj );
201
202     /////////////////////////////////////////////////////////////////
203   } // namespace base
204   ///////////////////////////////////////////////////////////////////
205   /////////////////////////////////////////////////////////////////
206 } // namespace zypp
207 ///////////////////////////////////////////////////////////////////
208 #endif // ZYPP_BASE_LOGCONTROL_H