Imported Upstream version 17.14.0
[platform/upstream/libzypp.git] / zypp / base / Logger.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Logger.h
10  *
11 */
12 #ifndef ZYPP_BASE_LOGGER_H
13 #define ZYPP_BASE_LOGGER_H
14 #include <cstring>
15 #include <iosfwd>
16 #include <string>
17
18 ///////////////////////////////////////////////////////////////////
19 #ifdef ZYPP_NDEBUG
20 #define OSDLOG( MSG )
21 #define OSMLOG( L, MSG )
22 #define TRACELEAVE
23 #else
24 namespace zypp
25 {
26   namespace debug
27   {
28     void osdlog( const std::string & msg_r, unsigned level_r ); // LogControl.cc
29
30     struct TraceLeave   // LogControl.cc
31     {
32       TraceLeave( const TraceLeave & ) =delete;
33       TraceLeave & operator=( const TraceLeave & ) =delete;
34       TraceLeave( const char * file_r, const char *  fnc_r, int line_r );
35       ~TraceLeave();
36     private:
37       static unsigned _depth;
38       const char *    _file;
39       const char *    _fnc;
40       int             _line;
41     };
42   }
43 }
44 #define OSDLOG( MSG )    ::zypp::debug::osdlog( MSG, 0 )
45 #define OSMLOG( L, MSG ) ::zypp::debug::osdlog( MSG, L )
46 #define TRACELEAVE       ::zypp::debug::TraceLeave _TraceLeave( __FILE__, __FUNCTION__, __LINE__ )
47 #endif // ZYPP_NDEBUG
48 ///////////////////////////////////////////////////////////////////
49
50 /** \defgroup ZYPP_BASE_LOGGER_MACROS ZYPP_BASE_LOGGER_MACROS
51  *  Convenience macros for logging.
52  *
53  * The macros finaly call @ref getStream, providing appropriate arguments,
54  * to return the log stream.
55  *
56  * @code
57  * L_DBG("foo") << ....
58  * @endcode
59  * Logs a debug message for group @a "foo".
60  *
61  * @code
62  * #undef ZYPP_BASE_LOGGER_LOGGROUP
63  * #define ZYPP_BASE_LOGGER_LOGGROUP "foo"
64  *
65  * DBG << ....
66  * @endcode
67  * Defines group @a "foo" as default for log messages and logs a
68  * debug message.
69  */
70 /*@{*/
71
72 #ifndef ZYPP_BASE_LOGGER_LOGGROUP
73 /** Default log group if undefined. */
74 #define ZYPP_BASE_LOGGER_LOGGROUP "DEFINE_LOGGROUP"
75 #endif
76
77 #define XXX L_XXX( ZYPP_BASE_LOGGER_LOGGROUP )
78 #define DBG L_DBG( ZYPP_BASE_LOGGER_LOGGROUP )
79 #define MIL L_MIL( ZYPP_BASE_LOGGER_LOGGROUP )
80 #define WAR L_WAR( ZYPP_BASE_LOGGER_LOGGROUP )
81 #define ERR L_ERR( ZYPP_BASE_LOGGER_LOGGROUP )
82 #define SEC L_SEC( ZYPP_BASE_LOGGER_LOGGROUP )
83 #define INT L_INT( ZYPP_BASE_LOGGER_LOGGROUP )
84 #define USR L_USR( ZYPP_BASE_LOGGER_LOGGROUP )
85
86 #define L_XXX(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_XXX )
87 #define L_DBG(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP"++", zypp::base::logger::E_MIL )
88 #define L_MIL(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_MIL )
89 #define L_WAR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_WAR )
90 #define L_ERR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_ERR )
91 #define L_SEC(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_SEC )
92 #define L_INT(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_INT )
93 #define L_USR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_USR )
94
95 #define L_BASEFILE ( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ )
96
97 /** Actual call to @ref getStream. */
98 #define ZYPP_BASE_LOGGER_LOG(GROUP,LEVEL) \
99         zypp::base::logger::getStream( GROUP, LEVEL, L_BASEFILE, __FUNCTION__, __LINE__ )
100
101 /*@}*/
102
103 ///////////////////////////////////////////////////////////////////
104 namespace zypp
105 { /////////////////////////////////////////////////////////////////
106   ///////////////////////////////////////////////////////////////////
107   namespace base
108   { /////////////////////////////////////////////////////////////////
109
110     ///////////////////////////////////////////////////////////////////
111     namespace logger
112     { /////////////////////////////////////////////////////////////////
113
114       /** Definition of log levels.
115        *
116        * @see getStream
117       */
118       enum LogLevel {
119         E_XXX = 999, /**< Excessive logging. */
120         E_DBG = 0,   /**< Debug or verbose. */
121         E_MIL,       /**< Milestone. */
122         E_WAR,       /**< Warning. */
123         E_ERR,       /**< Error. */
124         E_SEC,       /**< Secutrity related. */
125         E_INT,       /**< Internal error. */
126         E_USR        /**< User log. */
127       };
128
129       /** Return a log stream to write on.
130        *
131        * The returned log stream is determined by @a group_r and
132        * @a level_r. The remaining arguments @a file_r, @a func_r
133        * and @a line_r are expected to denote the location in the
134        * source code that issued the message.
135        *
136        * @note You won't call @ref getStream directly, but use the
137        * @ref ZYPP_BASE_LOGGER_MACROS.
138       */
139       extern std::ostream & getStream( const char * group_r,
140                                        LogLevel     level_r,
141                                        const char * file_r,
142                                        const char * func_r,
143                                        const int    line_r );
144       extern bool isExcessive();
145
146       /////////////////////////////////////////////////////////////////
147     } // namespace logger
148     ///////////////////////////////////////////////////////////////////
149
150     /////////////////////////////////////////////////////////////////
151   } // namespace base
152   ///////////////////////////////////////////////////////////////////
153   /////////////////////////////////////////////////////////////////
154 } // namespace zypp
155 ///////////////////////////////////////////////////////////////////
156 #endif // ZYPP_BASE_LOGGER_H