libzypp  17.38.17
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
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
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
124 
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 
171 namespace zypp
173 {
174  namespace base
176  {
177 
179  namespace logger
180  {
181 
186  enum LogLevel {
187  E_XXX = 999,
188  E_DBG = 0,
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
Internal error.
Definition: Logger.h:193
Osd & operator<<(Tp &&val)
Definition: Logger.h:80
LogLevel
Definition of log levels.
Definition: Logger.h:186
std::ostream & _strlog
Definition: Logger.h:91
const char * _file
Definition: Logger.h:35
Debug or verbose.
Definition: Logger.h:188
BlockTraceBase(const BlockTraceBase &)=delete
std::ostream & _strout
Definition: Logger.h:90
BlockTraceBase & operator=(const BlockTraceBase &)=delete
Secutrity related.
Definition: Logger.h:192
std::ostream & operator<<(std::ostream &str, TraceCADBase::What obj)
relates: TraceCADBase Stream output of TraceCADBase::What.
Definition: Debug.h:74
BlockTraceBase(const char *file_r, const char *fnc_r, int line_r, std::string msg_r=std::string())
Definition: Logger.h:27
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition: Arch.h:173
Provides API related macros.
Osd & getOSD()
Definition: LogControl.cc:370
static unsigned _depth
Definition: Logger.h:34
static unsigned _depth
Definition: Logger.h:68
BlockTrace(const BlockTrace &)=delete
BlockTrace & operator=(const BlockTrace &)=delete
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
Excessive logging.
Definition: Logger.h:187
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
static unsigned _depth
Definition: Logger.h:49