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