1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Debug.h
11 * Debuging tools which should not be used in released code.
13 #warning ZYPP_BASE_DEBUG_H included
14 #ifndef ZYPP_BASE_DEBUG_H
15 #define ZYPP_BASE_DEBUG_H
20 #include "zypp/base/Logger.h"
21 #include "zypp/base/String.h"
22 #include "zypp/ExternalProgram.h"
23 #include "zypp/base/ProvideNumericId.h"
25 ///////////////////////////////////////////////////////////////////
27 { /////////////////////////////////////////////////////////////////
28 ///////////////////////////////////////////////////////////////////
30 { /////////////////////////////////////////////////////////////////
32 /** \defgroup DEBUG Debug tools
35 #define TAG INT << __PRETTY_FUNCTION__ << std::endl
38 inline std::ostream & dumpMemOn( std::ostream & str, const std::string & msg = std::string() )
40 static std::string mypid( str::numstring( getpid() ) );
48 ExternalProgram prog(argv,ExternalProgram::Discard_Stderr, false, -1, true);
50 str << "MEMUSAGE " << msg << std::endl;
51 for( std::string line = prog.receiveLine(); ! line.empty(); line = prog.receiveLine() )
58 ///////////////////////////////////////////////////////////////////
59 /** \defgroup DBG_TRACER Tracer
63 /** Base for a simple tracer. Provides an enum indicating which
64 * traced functions were called.
68 enum What { CTOR, COPYCTOR, ASSIGN, DTOR, PING };
72 /** \relates TraceCADBase Stream output of TraceCADBase::What. */
73 inline std::ostream & operator<<( std::ostream & str, TraceCADBase::What obj )
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";
86 /** A simple tracer for (copy) Construction, Assignment, and
87 * Destruction. To trace class Foo, derive public from
88 * TraceCAD<Foo>. This tracer simply calls traceCAD in each
89 * traced method, and traceCAD simply drops a line in the log.
91 * This tracer logs construction, copy construction, assignment,
92 * destruction and _PING.
94 * assignment: In case the traced class defines an operator=
95 * it must be altered to call TraceCAD::operator=, otherwise it
98 * _PING: Completely up to you. Call _PING somewhere in the traced
99 * class to indicate something. In case you overload traceCAD, do
100 * whatever is appropriate on _PING. It's just an offer to perform
101 * logging or actions here, and not in the traced code.
103 * But traceCAD may be overloaded to produce more stats.
105 * \see \c Example.COW_debug.cc.
108 struct TraceCAD : public base::ProvideNumericId<TraceCAD<_Tp>, unsigned long>
109 , public TraceCADBase
111 static unsigned long & _totalTraceCAD()
112 { static unsigned long _val = 0;
116 { _ident = __PRETTY_FUNCTION__;
118 traceCAD( CTOR, *this, *this ); }
120 TraceCAD( const TraceCAD & rhs )
121 { ++_totalTraceCAD();
122 traceCAD( COPYCTOR, *this, rhs ); }
124 TraceCAD & operator=( const TraceCAD & rhs )
125 { traceCAD( ASSIGN, *this, rhs ); return *this; }
128 { --_totalTraceCAD();
129 traceCAD( DTOR, *this, *this ); }
132 { traceCAD( PING, *this, *this ); }
135 /** \relates TraceCAD Stream output. */
137 inline std::ostream & operator<<( std::ostream & str, const TraceCAD<_Tp> & obj )
138 { return str << "(ID " << obj.numericId() << ", TOTAL " << obj._totalTraceCAD()
139 << ") [" << &obj << "] "; }
141 /** Drop a log line about the traced method. Overload to
145 void traceCAD( TraceCADBase::What what_r,
146 const TraceCAD<_Tp> & self_r,
147 const TraceCAD<_Tp> & rhs_r )
151 case TraceCADBase::CTOR:
152 case TraceCADBase::PING:
153 case TraceCADBase::DTOR:
154 _DBG("DEBUG") << what_r << self_r << " (" << self_r._ident << ")" << std::endl;
157 case TraceCADBase::COPYCTOR:
158 case TraceCADBase::ASSIGN:
159 _DBG("DEBUG") << what_r << self_r << "( " << rhs_r << ")" << " (" << self_r._ident << ")" << std::endl;
164 ///////////////////////////////////////////////////////////////////
166 /////////////////////////////////////////////////////////////////
168 ///////////////////////////////////////////////////////////////////
169 /////////////////////////////////////////////////////////////////
171 ///////////////////////////////////////////////////////////////////
172 #endif // ZYPP_BASE_DEBUG_H