libzypp  13.10.6
Debug.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #warning ZYPP_BASE_DEBUG_H included
14 #ifndef ZYPP_BASE_DEBUG_H
15 #define ZYPP_BASE_DEBUG_H
16 
17 #include <iosfwd>
18 //#include <sstream>
19 //#include <string>
20 #include "zypp/base/Logger.h"
21 #include "zypp/base/String.h"
22 #include "zypp/ExternalProgram.h"
24 
26 namespace zypp
27 {
28  namespace debug
30  {
31 
35 #define TAG INT << __PRETTY_FUNCTION__ << std::endl
36 
38  inline std::ostream & dumpMemOn( std::ostream & str, const std::string & msg = std::string() )
39  {
40  static std::string mypid( str::numstring( getpid() ) );
41  const char* argv[] =
42  {
43  "ps",
44  "v",
45  mypid.c_str(),
46  NULL
47  };
48  ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
49 
50  str << "MEMUSAGE " << msg << std::endl;
51  for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
52  str << line;
53 
54  prog.close();
55  return str;
56  }
57 
59 
66  struct TraceCADBase
67  {
68  enum What { CTOR, COPYCTOR, ASSIGN, DTOR, PING };
69  std::string _ident;
70  };
71 
73  inline std::ostream & operator<<( std::ostream & str, TraceCADBase::What obj )
74  {
75  switch( obj )
76  {
77  case TraceCADBase::CTOR: return str << "CTOR";
78  case TraceCADBase::COPYCTOR: return str << "COPYCTOR";
79  case TraceCADBase::ASSIGN: return str << "ASSIGN";
80  case TraceCADBase::DTOR: return str << "DTOR";
81  case TraceCADBase::PING: return str << "PING";
82  }
83  return str;
84  }
85 
107  template<class _Tp>
108  struct TraceCAD : public base::ProvideNumericId<TraceCAD<_Tp>, unsigned long>
109  , public TraceCADBase
110  {
111  static unsigned long & _totalTraceCAD()
112  { static unsigned long _val = 0;
113  return _val; }
114 
116  { _ident = __PRETTY_FUNCTION__;
117  ++_totalTraceCAD();
118  traceCAD( CTOR, *this, *this ); }
119 
120  TraceCAD( const TraceCAD & rhs )
121  { ++_totalTraceCAD();
122  traceCAD( COPYCTOR, *this, rhs ); }
123 
124  TraceCAD & operator=( const TraceCAD & rhs )
125  { traceCAD( ASSIGN, *this, rhs ); return *this; }
126 
127  virtual ~TraceCAD()
128  { --_totalTraceCAD();
129  traceCAD( DTOR, *this, *this ); }
130 
131  void _PING() const
132  { traceCAD( PING, *this, *this ); }
133  };
134 
136  template<class _Tp>
137  inline std::ostream & operator<<( std::ostream & str, const TraceCAD<_Tp> & obj )
138  { return str << "(ID " << obj.numericId() << ", TOTAL " << obj._totalTraceCAD()
139  << ") [" << &obj << "] "; }
140 
144  template<class _Tp>
146  const TraceCAD<_Tp> & self_r,
147  const TraceCAD<_Tp> & rhs_r )
148  {
149  switch( what_r )
150  {
151  case TraceCADBase::CTOR:
152  case TraceCADBase::PING:
153  case TraceCADBase::DTOR:
154  _DBG("DEBUG") << what_r << self_r << " (" << self_r._ident << ")" << std::endl;
155  break;
156 
159  _DBG("DEBUG") << what_r << self_r << "( " << rhs_r << ")" << " (" << self_r._ident << ")" << std::endl;
160  break;
161  }
162  }
164 
167  } // namespace debug
170 } // namespace zypp
172 #endif // ZYPP_BASE_DEBUG_H
std::ostream & operator<<(std::ostream &str, TraceCADBase::What obj)
Definition: Debug.h:73
A simple tracer for (copy) Construction, Assignment, and Destruction.
Definition: Debug.h:108
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:145
void _PING() const
Definition: Debug.h:131
TraceCAD(const TraceCAD &rhs)
Definition: Debug.h:120
std::string _ident
Definition: Debug.h:69
TraceCAD & operator=(const TraceCAD &rhs)
Definition: Debug.h:124
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:219
int close()
Wait for the progamm to complete.
Base for a simple tracer.
Definition: Debug.h:66
static unsigned long & _totalTraceCAD()
Definition: Debug.h:111
virtual ~TraceCAD()
Definition: Debug.h:127
std::ostream & dumpMemOn(std::ostream &str, const std::string &msg=std::string())
&#39;ps v&#39;
Definition: Debug.h:38
#define _DBG(GROUP)
Definition: Logger.h:55