added LogControl::TmpLineWriter
[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   namespace base
26   { /////////////////////////////////////////////////////////////////
27
28     ///////////////////////////////////////////////////////////////////
29     //
30     //  CLASS NAME : LogControl
31     //
32     /** Maintain logfile related options.
33      * \note A Singleton using a Singleton implementation class,
34      * that's why there is no _pimpl like in other classes.
35     */
36     class LogControl
37     {
38       friend std::ostream & operator<<( std::ostream & str, const LogControl & obj );
39
40     public:
41       /** Singleton access. */
42       static LogControl instance()
43       { return LogControl(); }
44
45
46       /** If you want to log the (formated) loglines by yourself,
47        *  derive from this, and overload \c writeOut.
48        * Expect \a formated_r to be a formated log line without trailing \c NL.
49        * Ready to be written to the log.
50       */
51       struct LineWriter
52       {
53         virtual void writeOut( const std::string & /*formated_r*/ )
54         {}
55         virtual ~LineWriter() {}
56       };
57
58       /** If you want to format loglines by yourself,
59        *  derive from this, and overload \c format.
60        * Return a formated logline without trailing \c NL.
61        * Ready to be written to the log.
62       */
63       struct LineFormater
64       {
65         virtual std::string format( const std::string & /*group_r*/,
66                                     logger::LogLevel    /*level_r*/,
67                                     const char *        /*file_r*/,
68                                     const char *        /*func_r*/,
69                                     int                 /*line_r*/,
70                                     const std::string & /*message_r*/ );
71         virtual ~LineFormater() {}
72       };
73
74     public:
75       /** Assign a LineFormater.
76        * If you want to format loglines by yourself. NULL installs the
77        * default formater.
78       */
79       void setLineFormater( const shared_ptr<LineFormater> & formater_r );
80
81       /** Set path for the logfile.
82        * An empty pathname turns off logging.
83        * <tt>"-"</tt> logs to std::err.
84        * \throw if \a logfile_r is not usable.
85       */
86       void logfile( const Pathname & logfile_r );
87
88       /** Turn off logging. */
89       void logNothing();
90
91       /** Log to std::err. */
92       void logToStdErr();
93
94       /** Assign a LineWriter.
95        * If you want to log the (formated) loglines by yourself.
96        * NULL turns off logging (same as logNothing)
97       */
98       void setLineWriter( const shared_ptr<LineWriter> & writer_r );
99
100     public:
101       /** Turn on excessive logging for the lifetime of this object.*/
102       struct TmpExcessive
103       {
104         TmpExcessive();
105         ~TmpExcessive();
106       };
107
108       /** Exchange LineWriter for the lifetime of this object. */
109       struct TmpLineWriter
110       {
111         TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() );
112         ~TmpLineWriter();
113       private:
114         shared_ptr<LineWriter> _writer;
115       };
116
117     private:
118       /** Default ctor: Singleton */
119       LogControl()
120       {}
121     };
122     ///////////////////////////////////////////////////////////////////
123
124     /** \relates LogControl Stream output */
125     std::ostream & operator<<( std::ostream & str, const LogControl & obj );
126
127     /////////////////////////////////////////////////////////////////
128   } // namespace base
129   ///////////////////////////////////////////////////////////////////
130   /////////////////////////////////////////////////////////////////
131 } // namespace zypp
132 ///////////////////////////////////////////////////////////////////
133 #endif // ZYPP_BASE_LOGCONTROL_H