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