libzypp 17.38.15

Logger.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_BASE_LOGGER_H
13#define ZYPP_BASE_LOGGER_H
14#include <cstring>
15#include <iosfwd>
16#include <string>
17
18#include <zypp-core/Globals.h>
19
21 namespace zypp::debug {
22 // impl in LogControl.cc, but not in ZYPP_NDEBUG; BLOCKTRACE logs MIL
23 struct BlockTraceBase
24 {
25 BlockTraceBase( const BlockTraceBase & ) =delete;
26 BlockTraceBase & operator=( const BlockTraceBase & ) =delete;
27 BlockTraceBase( const char * file_r, const char * fnc_r, int line_r, std::string msg_r=std::string() )
28 : _file { file_r }
29 , _fnc { fnc_r }
30 , _line { line_r }
31 , _msg { std::move(msg_r) }
32 {}
33 protected:
34 static unsigned _depth;
35 const char * _file;
36 const char * _fnc;
37 int _line;
38 std::string _msg;
39 };
40
41 // Log code location and block leave as MIL
42 struct BlockTrace : private BlockTraceBase
43 {
44 BlockTrace( const BlockTrace & ) =delete;
45 BlockTrace & operator=( const BlockTrace & ) =delete;
46 BlockTrace( const char * file_r, const char * fnc_r, int line_r, std::string msg_r=std::string() );
47 ~BlockTrace();
48 private:
49 static unsigned _depth;
50 };
51#define BLOCKTRACE(M) ::zypp::debug::BlockTrace _BlockTrace( L_BASEFILE, __FUNCTION__, __LINE__, M )
52} // namespace zypp::debug
53
55#ifndef ZYPP_NDEBUG
56 namespace zypp::debug {
57 // impl in LogControl.cc, and in ZYPP_NDEBUG (due to OSD)
58
59 // Log code location and block leave as \ref Osd
60 // Indent if nested
61 struct ZYPP_API TraceLeave : private BlockTraceBase
62 {
63 TraceLeave( const TraceLeave & ) =delete;
64 TraceLeave & operator=( const TraceLeave & ) =delete;
65 TraceLeave( const char * file_r, const char * fnc_r, int line_r, std::string msg_r=std::string() );
66 ~TraceLeave();
67 private:
68 static unsigned _depth;
69 };
70#define TRACE(M) ::zypp::debug::TraceLeave _TraceLeave( __FILE__, __FUNCTION__, __LINE__, M );
71#define XTRACE ::zypp::debug::TraceLeave _TraceLeave( __FILE__, __FUNCTION__, __LINE__ );
72
73 // OnScreenDebug messages colored to stderr
74 struct ZYPP_API Osd
75 {
76 Osd( std::ostream &, int = 0 );
77 ~Osd();
78
79 template<class Tp>
80 Osd & operator<<( Tp && val )
81 {
82 _strout << std::forward<Tp>(val);
83 _strlog << std::forward<Tp>(val);
84 return *this;
85 }
86
87 Osd & operator<<( std::ostream& (*iomanip)( std::ostream& ) );
88
89 private:
90 std::ostream & _strout;
91 std::ostream & _strlog;
92 };
93
94 Osd & getOSD() ZYPP_API;
95#define OSD ::zypp::debug::getOSD()
96} // namespace zypp::debug
97#endif // ZYPP_NDEBUG
98
100
122
123#ifndef ZYPP_BASE_LOGGER_LOGGROUP
125#define ZYPP_BASE_LOGGER_LOGGROUP "DEFINE_LOGGROUP"
126#endif
127
128#define XXX L_XXX( ZYPP_BASE_LOGGER_LOGGROUP )
129#define DBG L_DBG( ZYPP_BASE_LOGGER_LOGGROUP )
130#define MIL L_MIL( ZYPP_BASE_LOGGER_LOGGROUP )
131#define WAR L_WAR( ZYPP_BASE_LOGGER_LOGGROUP )
132#define ERR L_ERR( ZYPP_BASE_LOGGER_LOGGROUP )
133#define SEC L_SEC( ZYPP_BASE_LOGGER_LOGGROUP )
134#define INT L_INT( ZYPP_BASE_LOGGER_LOGGROUP )
135#define USR L_USR( ZYPP_BASE_LOGGER_LOGGROUP )
136
137#define L_XXX(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_XXX )
138#define L_DBG(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP"++", zypp::base::logger::E_MIL )
139#define L_MIL(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_MIL )
140#define L_WAR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_WAR )
141#define L_ERR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_ERR )
142#define L_SEC(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_SEC )
143#define L_INT(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_INT )
144#define L_USR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_USR )
145
146
147#define L_ENV_CONSTR_DEFINE_FUNC(ENV) \
148 namespace zypp::log { \
149 bool has_env_constr_##ENV () \
150 { \
151 static bool has_##ENV = (::getenv(#ENV) != NULL); \
152 return has_##ENV; \
153 } \
154 const char *empty_or_group_if_##ENV ( const char *group ) \
155 { \
156 return has_env_constr_##ENV() ? group : nullptr; \
157 } \
158 }
159
160#define L_ENV_CONSTR_FWD_DECLARE_FUNC(ENV) namespace zypp::log { bool has_env_constr_##ENV (); const char *empty_or_group_if_##ENV ( const char *group ); }
161#define L_ENV_CONSTR(ENV,GROUP,LEVEL) ZYPP_BASE_LOGGER_LOG( zypp::log::empty_or_group_if_##ENV( GROUP ), LEVEL )
162
163#define L_BASEFILE ( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ )
164
166#define ZYPP_BASE_LOGGER_LOG(GROUP,LEVEL) \
167 zypp::base::logger::getStream( GROUP, LEVEL, L_BASEFILE, __FUNCTION__, __LINE__ )
168
170
172 namespace zypp
173{
175 namespace base
176 {
177
179 namespace logger
180 {
181
186 enum LogLevel {
187 E_XXX = 999,
188 E_DBG = 0,
189 E_MIL,
190 E_WAR,
191 E_ERR,
192 E_SEC,
193 E_INT,
194 E_USR
195 };
196
207 extern std::ostream & getStream( const char * group_r,
208 LogLevel level_r,
209 const char * file_r,
210 const char * func_r,
211 const int line_r ) ZYPP_API;
212 extern bool isExcessive() ZYPP_API;
213
215 } // namespace logger
217
219 } // namespace base
222} // namespace zypp
224#endif // ZYPP_BASE_LOGGER_H
Globals.h
Provides API related macros.
std
Definition ansi.h:855
zypp::base::logger::LogLevel
LogLevel
Definition of log levels.
Definition Logger.h:186
zypp::base::logger::E_SEC
@ E_SEC
Secutrity related.
Definition Logger.h:192
zypp::base::logger::E_ERR
@ E_ERR
Error.
Definition Logger.h:191
zypp::base::logger::E_XXX
@ E_XXX
Excessive logging.
Definition Logger.h:187
zypp::base::logger::E_DBG
@ E_DBG
Debug or verbose.
Definition Logger.h:188
zypp::base::logger::E_USR
@ E_USR
User log.
Definition Logger.h:194
zypp::base::logger::E_WAR
@ E_WAR
Warning.
Definition Logger.h:190
zypp::base::logger::E_MIL
@ E_MIL
Milestone.
Definition Logger.h:189
zypp::base::logger::E_INT
@ E_INT
Internal error.
Definition Logger.h:193
zypp::base::logger::isExcessive
bool isExcessive()
Definition LogControl.cc:822
zypp::base::logger::getStream
std::ostream & getStream(const char *group_r, LogLevel level_r, const char *file_r, const char *func_r, const int line_r)
Return a log stream to write on.
Definition LogControl.cc:787
zypp::debug
Definition Debug.h:31
zypp::debug::getOSD
Osd & getOSD()
Definition LogControl.cc:370
zypp::debug::operator<<
std::ostream & operator<<(std::ostream &str, TraceCADBase::What obj)
relates: TraceCADBase Stream output of TraceCADBase::What.
Definition Debug.h:74
zypp::iomanip
Definition LogTools.h:453
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zypp::debug::BlockTraceBase::_fnc
const char * _fnc
Definition Logger.h:36
zypp::debug::BlockTraceBase::_msg
std::string _msg
Definition Logger.h:38
zypp::debug::BlockTraceBase::BlockTraceBase
BlockTraceBase(const BlockTraceBase &)=delete
zypp::debug::BlockTraceBase::_file
const char * _file
Definition Logger.h:35
zypp::debug::BlockTraceBase::BlockTraceBase
BlockTraceBase(const char *file_r, const char *fnc_r, int line_r, std::string msg_r=std::string())
Definition Logger.h:27
zypp::debug::BlockTraceBase::_line
int _line
Definition Logger.h:37
zypp::debug::BlockTraceBase::operator=
BlockTraceBase & operator=(const BlockTraceBase &)=delete
zypp::debug::BlockTraceBase::_depth
static unsigned _depth
Definition Logger.h:34
zypp::debug::BlockTrace::BlockTrace
BlockTrace(const BlockTrace &)=delete
zypp::debug::BlockTrace::operator=
BlockTrace & operator=(const BlockTrace &)=delete
zypp::debug::BlockTrace::_depth
static unsigned _depth
Definition Logger.h:49
zypp::debug::Osd
Definition Logger.h:75
zypp::debug::Osd::_strlog
std::ostream & _strlog
Definition Logger.h:91
zypp::debug::Osd::Osd
Osd(std::ostream &, int=0)
Definition LogControl.cc:355
zypp::debug::Osd::_strout
std::ostream & _strout
Definition Logger.h:90
zypp::debug::Osd::operator<<
Osd & operator<<(Tp &&val)
Definition Logger.h:80
zypp::debug::TraceLeave::TraceLeave
TraceLeave(const TraceLeave &)=delete
zypp::debug::TraceLeave::_depth
static unsigned _depth
Definition Logger.h:68
zypp::debug::TraceLeave::operator=
TraceLeave & operator=(const TraceLeave &)=delete