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