1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Debug.h
11 * Debuging tools which should not be used in released code.
14 #warning ZYPP_BASE_DEBUG_H included
15 #ifndef ZYPP_BASE_DEBUG_H
16 #define ZYPP_BASE_DEBUG_H
21 #include "zypp/base/Logger.h"
22 #include "zypp/base/String.h"
23 #include "zypp/ExternalProgram.h"
24 #include "zypp/base/ProvideNumericId.h"
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////
31 { /////////////////////////////////////////////////////////////////
33 /** \defgroup DEBUG Debug tools
36 #define TAG INT << __PRETTY_FUNCTION__ << std::endl
39 inline std::ostream & dumpMemOn( std::ostream & str, const std::string & msg = std::string() )
41 static std::string mypid( str::numstring( getpid() ) );
49 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
51 str << "MEMUSAGE " << msg << std::endl;
52 for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
59 ///////////////////////////////////////////////////////////////////
60 /** \defgroup DBG_TRACER Tracer
64 /** Base for a simple tracer. Provides an enum indicating which
65 * traced functions were called.
69 enum What { CTOR, COPYCTOR, ASSIGN, DTOR, PING };
73 /** \relates TraceCADBase Stream output of TraceCADBase::What. */
74 inline std::ostream & operator<<( std::ostream & str, TraceCADBase::What obj )
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";
87 /** A simple tracer for (copy) Construction, Assignment, and
88 * Destruction. To trace class Foo, derive public from
89 * TraceCAD<Foo>. This tracer simply calls traceCAD in each
90 * traced method, and traceCAD simply drops a line in the log.
92 * This tracer logs construction, copy construction, assignment,
93 * destruction and _PING.
95 * assignment: In case the traced class defines an operator=
96 * it must be altered to call TraceCAD::operator=, otherwise it
99 * _PING: Completely up to you. Call _PING somewhere in the traced
100 * class to indicate something. In case you overload traceCAD, do
101 * whatever is appropriate on _PING. It's just an offer to perform
102 * logging or actions here, and not in the traced code.
104 * But traceCAD may be overloaded to produce more stats.
106 * \see \c Example.COW_debug.cc.
109 struct TraceCAD : public base::ProvideNumericId<TraceCAD<_Tp>, unsigned long>
110 , public TraceCADBase
112 static unsigned long & _totalTraceCAD()
113 { static unsigned long _val = 0;
117 { _ident = __PRETTY_FUNCTION__;
119 traceCAD( CTOR, *this, *this ); }
121 TraceCAD( const TraceCAD & rhs )
122 { ++_totalTraceCAD();
123 traceCAD( COPYCTOR, *this, rhs ); }
125 TraceCAD & operator=( const TraceCAD & rhs )
126 { traceCAD( ASSIGN, *this, rhs ); return *this; }
129 { --_totalTraceCAD();
130 traceCAD( DTOR, *this, *this ); }
133 { traceCAD( PING, *this, *this ); }
136 /** \relates TraceCAD Stream output. */
138 inline std::ostream & operator<<( std::ostream & str, const TraceCAD<_Tp> & obj )
139 { return str << "(ID " << obj.numericId() << ", TOTAL " << obj._totalTraceCAD()
140 << ") [" << &obj << "] "; }
142 /** Drop a log line about the traced method. Overload to
146 void traceCAD( TraceCADBase::What what_r,
147 const TraceCAD<_Tp> & self_r,
148 const TraceCAD<_Tp> & rhs_r )
152 case TraceCADBase::CTOR:
153 case TraceCADBase::PING:
154 case TraceCADBase::DTOR:
155 _DBG("DEBUG") << what_r << self_r << " (" << self_r._ident << ")" << std::endl;
158 case TraceCADBase::COPYCTOR:
159 case TraceCADBase::ASSIGN:
160 _DBG("DEBUG") << what_r << self_r << "( " << rhs_r << ")" << " (" << self_r._ident << ")" << std::endl;
165 ///////////////////////////////////////////////////////////////////
167 /////////////////////////////////////////////////////////////////
169 ///////////////////////////////////////////////////////////////////
170 /////////////////////////////////////////////////////////////////
172 ///////////////////////////////////////////////////////////////////
173 #endif // ZYPP_BASE_DEBUG_H
174 #endif // ZYPP_NDEBUG