e19d94ff939a1c23a1bcfdefeab04439f40be93d
[platform/upstream/libzypp.git] / zypp / base / Debug.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/Debug.h
10  *
11  * Debuging tools which should not be used in released code.
12 */
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"
23 #include "zypp/base/ProvideNumericId.h"
24
25 ///////////////////////////////////////////////////////////////////
26 namespace zypp
27 { /////////////////////////////////////////////////////////////////
28   ///////////////////////////////////////////////////////////////////
29   namespace debug
30   { /////////////////////////////////////////////////////////////////
31
32     /** \defgroup DEBUG Debug tools
33     */
34
35 #define TAG INT << __PRETTY_FUNCTION__ << std::endl
36
37     /** 'ps v' */
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
58     ///////////////////////////////////////////////////////////////////
59     /** \defgroup DBG_TRACER Tracer
60      * \ingroup DEBUG
61     */
62     //@{
63     /** Base for a simple tracer. Provides an enum indicating which
64      * traced functions were called.
65     */
66     struct TraceCADBase
67     {
68       enum What { CTOR, COPYCTOR, ASSIGN, DTOR, PING };
69       std::string _ident;
70     };
71
72     /** \relates TraceCADBase Stream output of TraceCADBase::What. */
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
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.
90      *
91      * This tracer logs construction, copy construction, assignment,
92      * destruction and _PING.
93      *
94      * assignment: In case the traced class defines an operator=
95      * it must be altered to call TraceCAD::operator=, otherwise it
96      * won't be triggered.
97      *
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.
102      *
103      * But traceCAD may be overloaded to produce more stats.
104      *
105      * \see \c Example.COW_debug.cc.
106      */
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
115         TraceCAD()
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
135     /** \relates TraceCAD Stream output. */
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
141     /** Drop a log line about the traced method. Overload to
142      * fit your needs.
143     */
144     template<class _Tp>
145       void traceCAD( TraceCADBase::What what_r,
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
157           case TraceCADBase::COPYCTOR:
158           case TraceCADBase::ASSIGN:
159             _DBG("DEBUG") << what_r << self_r << "( " << rhs_r << ")" << " (" << self_r._ident << ")" << std::endl;
160             break;
161           }
162       }
163     //@}
164     ///////////////////////////////////////////////////////////////////
165
166     /////////////////////////////////////////////////////////////////
167   } // namespace debug
168   ///////////////////////////////////////////////////////////////////
169   /////////////////////////////////////////////////////////////////
170 } // namespace zypp
171 ///////////////////////////////////////////////////////////////////
172 #endif // ZYPP_BASE_DEBUG_H