libzypp  17.28.4
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 
19 #ifndef ZYPP_NDEBUG
20 namespace zypp
21 {
22  namespace debug
23  { // impl in LogControl.cc
24 
25  // Log code loacaton and block leave
26  // Indent if nested
27  struct TraceLeave
28  {
29  TraceLeave( const TraceLeave & ) =delete;
30  TraceLeave & operator=( const TraceLeave & ) =delete;
31  TraceLeave( const char * file_r, const char * fnc_r, int line_r );
32  ~TraceLeave();
33  private:
34  static unsigned _depth;
35  const char * _file;
36  const char * _fnc;
37  int _line;
38  };
39 #define TRACE ::zypp::debug::TraceLeave _TraceLeave( __FILE__, __FUNCTION__, __LINE__ )
40 
41  // OnScreenDebug messages colored to stderr
42  struct Osd
43  {
44  Osd( std::ostream &, int = 0 );
45  ~Osd();
46 
47  template<class Tp>
48  Osd & operator<<( Tp && val )
49  {
50  _strout << std::forward<Tp>(val);
51  _strlog << std::forward<Tp>(val);
52  return *this;
53  }
54 
55  Osd & operator<<( std::ostream& (*iomanip)( std::ostream& ) );
56 
57  private:
58  std::ostream & _strout;
59  std::ostream & _strlog;
60  };
61 #define OSD ::zypp::debug::Osd(L_USR("OSD"))
62  }
63 }
64 #endif // ZYPP_NDEBUG
66 
88 
89 #ifndef ZYPP_BASE_LOGGER_LOGGROUP
91 #define ZYPP_BASE_LOGGER_LOGGROUP "DEFINE_LOGGROUP"
92 #endif
93 
94 #define XXX L_XXX( ZYPP_BASE_LOGGER_LOGGROUP )
95 #define DBG L_DBG( ZYPP_BASE_LOGGER_LOGGROUP )
96 #define MIL L_MIL( ZYPP_BASE_LOGGER_LOGGROUP )
97 #define WAR L_WAR( ZYPP_BASE_LOGGER_LOGGROUP )
98 #define ERR L_ERR( ZYPP_BASE_LOGGER_LOGGROUP )
99 #define SEC L_SEC( ZYPP_BASE_LOGGER_LOGGROUP )
100 #define INT L_INT( ZYPP_BASE_LOGGER_LOGGROUP )
101 #define USR L_USR( ZYPP_BASE_LOGGER_LOGGROUP )
102 
103 #define L_XXX(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_XXX )
104 #define L_DBG(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP"++", zypp::base::logger::E_MIL )
105 #define L_MIL(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_MIL )
106 #define L_WAR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_WAR )
107 #define L_ERR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_ERR )
108 #define L_SEC(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_SEC )
109 #define L_INT(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_INT )
110 #define L_USR(GROUP) ZYPP_BASE_LOGGER_LOG( GROUP, zypp::base::logger::E_USR )
111 
112 #define L_ENV_CONSTR_DEFINE_FUNC(ENV) \
113  const char *empty_or_group_if_##ENV ( const char *group ) \
114  { \
115  static bool has_##ENV = (::getenv(#ENV) != NULL); \
116  return has_##ENV ? group : nullptr; \
117  }
118 
119 #define L_ENV_CONSTR_FWD_DECLARE_FUNC(ENV) const char *empty_or_group_if_##ENV ( const char *group )
120 #define L_ENV_CONSTR(ENV,GROUP,LEVEL) ZYPP_BASE_LOGGER_LOG( zypp::empty_or_group_if_##ENV( #GROUP ), LEVEL )
121 
122 #define L_BASEFILE ( *__FILE__ == '/' ? strrchr( __FILE__, '/' ) + 1 : __FILE__ )
123 
125 #define ZYPP_BASE_LOGGER_LOG(GROUP,LEVEL) \
126  zypp::base::logger::getStream( GROUP, LEVEL, L_BASEFILE, __FUNCTION__, __LINE__ )
127 
131 namespace zypp
132 {
134  namespace base
135  {
136 
138  namespace logger
139  {
140 
145  enum LogLevel {
146  E_XXX = 999,
147  E_DBG = 0,
153  E_USR
154  };
155 
166  extern std::ostream & getStream( const char * group_r,
167  LogLevel level_r,
168  const char * file_r,
169  const char * func_r,
170  const int line_r );
171  extern bool isExcessive();
172 
174  } // namespace logger
176 
178  } // namespace base
181 } // namespace zypp
183 #endif // ZYPP_BASE_LOGGER_H
LogLevel
Definition of log levels.
Definition: Logger.h:145
@ E_SEC
Secutrity related.
Definition: Logger.h:151
@ E_ERR
Error.
Definition: Logger.h:150
@ E_XXX
Excessive logging.
Definition: Logger.h:146
@ E_DBG
Debug or verbose.
Definition: Logger.h:147
@ E_USR
User log.
Definition: Logger.h:153
@ E_WAR
Warning.
Definition: Logger.h:149
@ E_MIL
Milestone.
Definition: Logger.h:148
@ E_INT
Internal error.
Definition: Logger.h:152
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:716
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
std::ostream & _strlog
Definition: Logger.h:59
Osd & operator<<(Tp &&val)
Definition: Logger.h:48
Osd(std::ostream &, int=0)
Definition: LogControl.cc:329
std::ostream & _strout
Definition: Logger.h:58
TraceLeave(const TraceLeave &)=delete
static unsigned _depth
Definition: Logger.h:34
TraceLeave & operator=(const TraceLeave &)=delete
const char * _fnc
Definition: Logger.h:36
const char * _file
Definition: Logger.h:35