Imported Upstream version 14.45.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
15 #include <iosfwd>
16 #include <cstring>
17
18 /** \defgroup ZYPP_BASE_LOGGER_MACROS ZYPP_BASE_LOGGER_MACROS
19  *  Convenience macros for logging.
20  *
21  * The macros finaly call @ref getStream, providing appropriate arguments,
22  * to return the log stream.
23  *
24  * @code
25  * _DBG("foo") << ....
26  * @endcode
27  * Logs a debug message for group @a "foo".
28  *
29  * @code
30  * #undef ZYPP_BASE_LOGGER_LOGGROUP
31  * #define ZYPP_BASE_LOGGER_LOGGROUP "foo"
32  *
33  * DBG << ....
34  * @endcode
35  * Defines group @a "foo" as default for log messages and logs a
36  * debug message.
37  */
38 /*@{*/
39
40 #ifndef ZYPP_BASE_LOGGER_LOGGROUP
41 /** Default log group if undefined. */
42 #define ZYPP_BASE_LOGGER_LOGGROUP "DEFINE_LOGGROUP"
43 #endif
44
45 #define XXX _XXX( ZYPP_BASE_LOGGER_LOGGROUP )
46 #define DBG _DBG( ZYPP_BASE_LOGGER_LOGGROUP )
47 #define MIL _MIL( ZYPP_BASE_LOGGER_LOGGROUP )
48 #define WAR _WAR( ZYPP_BASE_LOGGER_LOGGROUP )
49 #define ERR _ERR( ZYPP_BASE_LOGGER_LOGGROUP )
50 #define SEC _SEC( ZYPP_BASE_LOGGER_LOGGROUP )
51 #define INT _INT( ZYPP_BASE_LOGGER_LOGGROUP )
52 #define USR _USR( ZYPP_BASE_LOGGER_LOGGROUP )
53
54 #define _XXX(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_XXX )
55 #define _DBG(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP"++", zypp::base::logger::E_MIL )
56 #define _MIL(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_MIL )
57 #define _WAR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_WAR )
58 #define _ERR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_ERR )
59 #define _SEC(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_SEC )
60 #define _INT(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_INT )
61 #define _USR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_USR )
62
63 #define _BASEFILE ( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ )
64
65 /** Actual call to @ref getStream. */
66 #define ZYPP_BASE_LOGGER_LOG(GROUP,LEVEL) \
67         zypp::base::logger::getStream( GROUP, LEVEL, _BASEFILE, __FUNCTION__, __LINE__ )
68
69 /*@}*/
70
71 ///////////////////////////////////////////////////////////////////
72 namespace zypp
73 { /////////////////////////////////////////////////////////////////
74   ///////////////////////////////////////////////////////////////////
75   namespace base
76   { /////////////////////////////////////////////////////////////////
77
78     ///////////////////////////////////////////////////////////////////
79     namespace logger
80     { /////////////////////////////////////////////////////////////////
81
82       /** Definition of log levels.
83        *
84        * @see getStream
85       */
86       enum LogLevel {
87         E_XXX = 999, /**< Excessive logging. */
88         E_DBG = 0,   /**< Debug or verbose. */
89         E_MIL,       /**< Milestone. */
90         E_WAR,       /**< Warning. */
91         E_ERR,       /**< Error. */
92         E_SEC,       /**< Secutrity related. */
93         E_INT,       /**< Internal error. */
94         E_USR        /**< User log. */
95       };
96
97       /** Return a log stream to write on.
98        *
99        * The returned log stream is determined by @a group_r and
100        * @a level_r. The remaining arguments @a file_r, @a func_r
101        * and @a line_r are expected to denote the location in the
102        * source code that issued the message.
103        *
104        * @note You won't call @ref getStream directly, but use the
105        * @ref ZYPP_BASE_LOGGER_MACROS.
106       */
107       extern std::ostream & getStream( const char * group_r,
108                                        LogLevel     level_r,
109                                        const char * file_r,
110                                        const char * func_r,
111                                        const int    line_r );
112       extern bool isExcessive();
113
114       /////////////////////////////////////////////////////////////////
115     } // namespace logger
116     ///////////////////////////////////////////////////////////////////
117
118     /////////////////////////////////////////////////////////////////
119   } // namespace base
120   ///////////////////////////////////////////////////////////////////
121   /////////////////////////////////////////////////////////////////
122 } // namespace zypp
123 ///////////////////////////////////////////////////////////////////
124 #endif // ZYPP_BASE_LOGGER_H