libzypp  15.3.0
Debug.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #ifndef ZYPP_NDEBUG
14 #warning ZYPP_BASE_DEBUG_H included
15 #ifndef ZYPP_BASE_DEBUG_H
16 #define ZYPP_BASE_DEBUG_H
17 
18 #include <iosfwd>
19 //#include <sstream>
20 //#include <string>
21 #include "zypp/base/Logger.h"
22 #include "zypp/base/String.h"
23 #include "zypp/ExternalProgram.h"
25 
27 namespace zypp
28 {
29  namespace debug
31  {
32 
36 #define TAG INT << __PRETTY_FUNCTION__ << std::endl
37 
39  inline std::ostream & dumpMemOn( std::ostream & str, const std::string & msg = std::string() )
40  {
41  static std::string mypid( str::numstring( getpid() ) );
42  const char* argv[] =
43  {
44  "ps",
45  "v",
46  mypid.c_str(),
47  NULL
48  };
49  ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
50 
51  str << "MEMUSAGE " << msg << std::endl;
52  for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
53  str << line;
54 
55  prog.close();
56  return str;
57  }
58 
60 
67  struct TraceCADBase
68  {
69  enum What { CTOR, COPYCTOR, ASSIGN, DTOR, PING };
70  std::string _ident;
71  };
72 
74  inline std::ostream & operator<<( std::ostream & str, TraceCADBase::What obj )
75  {
76  switch( obj )
77  {
78  case TraceCADBase::CTOR: return str << "CTOR";
79  case TraceCADBase::COPYCTOR: return str << "COPYCTOR";
80  case TraceCADBase::ASSIGN: return str << "ASSIGN";
81  case TraceCADBase::DTOR: return str << "DTOR";
82  case TraceCADBase::PING: return str << "PING";
83  }
84  return str;
85  }
86 
108  template<class _Tp>
109  struct TraceCAD : public base::ProvideNumericId<TraceCAD<_Tp>, unsigned long>
110  , public TraceCADBase
111  {
112  static unsigned long & _totalTraceCAD()
113  { static unsigned long _val = 0;
114  return _val; }
115 
117  { _ident = __PRETTY_FUNCTION__;
118  ++_totalTraceCAD();
119  traceCAD( CTOR, *this, *this ); }
120 
121  TraceCAD( const TraceCAD & rhs )
122  { ++_totalTraceCAD();
123  traceCAD( COPYCTOR, *this, rhs ); }
124 
125  TraceCAD & operator=( const TraceCAD & rhs )
126  { traceCAD( ASSIGN, *this, rhs ); return *this; }
127 
128  virtual ~TraceCAD()
129  { --_totalTraceCAD();
130  traceCAD( DTOR, *this, *this ); }
131 
132  void _PING() const
133  { traceCAD( PING, *this, *this ); }
134  };
135 
137  template<class _Tp>
138  inline std::ostream & operator<<( std::ostream & str, const TraceCAD<_Tp> & obj )
139  { return str << "(ID " << obj.numericId() << ", TOTAL " << obj._totalTraceCAD()
140  << ") [" << &obj << "] "; }
141 
145  template<class _Tp>
147  const TraceCAD<_Tp> & self_r,
148  const TraceCAD<_Tp> & rhs_r )
149  {
150  switch( what_r )
151  {
152  case TraceCADBase::CTOR:
153  case TraceCADBase::PING:
154  case TraceCADBase::DTOR:
155  _DBG("DEBUG") << what_r << self_r << " (" << self_r._ident << ")" << std::endl;
156  break;
157 
160  _DBG("DEBUG") << what_r << self_r << "( " << rhs_r << ")" << " (" << self_r._ident << ")" << std::endl;
161  break;
162  }
163  }
165 
168  } // namespace debug
171 } // namespace zypp
173 #endif // ZYPP_BASE_DEBUG_H
174 #endif // ZYPP_NDEBUG
std::ostream & operator<<(std::ostream &str, TraceCADBase::What obj)
Definition: Debug.h:74
A simple tracer for (copy) Construction, Assignment, and Destruction.
Definition: Debug.h:109
void traceCAD(TraceCADBase::What what_r, const TraceCAD< _Tp > &self_r, const TraceCAD< _Tp > &rhs_r)
Drop a log line about the traced method.
Definition: Debug.h:146
void _PING() const
Definition: Debug.h:132
String related utilities and Regular expression matching.
TraceCAD(const TraceCAD &rhs)
Definition: Debug.h:121
std::string _ident
Definition: Debug.h:70
TraceCAD & operator=(const TraceCAD &rhs)
Definition: Debug.h:125
Base class for objects providing a numeric Id.
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
std::string receiveLine()
Read one line from the input stream.
std::string numstring(char n, int w=0)
Definition: String.h:271
int close()
Wait for the progamm to complete.
Base for a simple tracer.
Definition: Debug.h:67
static unsigned long & _totalTraceCAD()
Definition: Debug.h:112
virtual ~TraceCAD()
Definition: Debug.h:128
std::ostream & dumpMemOn(std::ostream &str, const std::string &msg=std::string())
'ps v'
Definition: Debug.h:39
#define _DBG(GROUP)
Definition: Logger.h:72
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1