- Implement microseconds logging when ZYPP_PROFILING env variable is
[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        * Permission for logfiles is set to 0640 unless an explicit mode_t
83        * value is given. An empty pathname turns off logging. <tt>"-"</tt>
84        * logs to std::err.
85        * \throw if \a logfile_r is not usable.
86       */
87       void logfile( const Pathname & logfile_r );
88       void logfile( const Pathname & logfile_r, mode_t mode_r );
89
90       /** Turn off logging. */
91       void logNothing();
92
93       /** Log to std::err. */
94       void logToStdErr();
95
96       /** Assign a LineWriter.
97        * If you want to log the (formated) loglines by yourself.
98        * NULL turns off logging (same as logNothing)
99       */
100       void setLineWriter( const shared_ptr<LineWriter> & writer_r );
101
102     public:
103       /** Turn on excessive logging for the lifetime of this object.*/
104       struct TmpExcessive
105       {
106         TmpExcessive();
107         ~TmpExcessive();
108       };
109
110       /** Exchange LineWriter for the lifetime of this object. */
111       struct TmpLineWriter
112       {
113         TmpLineWriter( const shared_ptr<LineWriter> & writer_r = shared_ptr<LineWriter>() );
114         ~TmpLineWriter();
115       private:
116         shared_ptr<LineWriter> _writer;
117       };
118
119     private:
120       /** Default ctor: Singleton */
121       LogControl()
122       {}
123     };
124     ///////////////////////////////////////////////////////////////////
125
126     /** \relates LogControl Stream output */
127     std::ostream & operator<<( std::ostream & str, const LogControl & obj );
128
129     /////////////////////////////////////////////////////////////////
130   } // namespace base
131   ///////////////////////////////////////////////////////////////////
132   /////////////////////////////////////////////////////////////////
133 } // namespace zypp
134 ///////////////////////////////////////////////////////////////////
135 #endif // ZYPP_BASE_LOGCONTROL_H