NCBI C++ ToolKit
ddumpable.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef DDUMPABLE__HPP
2 #define DDUMPABLE__HPP
3 
4 /* $Id: ddumpable.hpp 92892 2021-02-22 15:32:01Z grichenk $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Andrei Gourianov
30  *
31  * File Description:
32  * Debug Dump functionality
33  *
34  */
35 
36 #include <corelib/ncbistd.hpp>
37 
39 
40 
41 //---------------------------------------------------------------------------
42 // CDebugDumpFormatter defines debug dump formatter interface
43 
45 {
46 public:
47  enum EValueType {
50  ePointer
51  };
52 public:
53  virtual ~CDebugDumpFormatter() { }
54 
55  virtual bool StartBundle(unsigned int level, const string& bundle) = 0;
56  virtual void EndBundle( unsigned int level, const string& bundle) = 0;
57 
58  virtual bool StartFrame( unsigned int level, const string& frame) = 0;
59  virtual void EndFrame( unsigned int level, const string& frame) = 0;
60 
61  virtual void PutValue( unsigned int level, const string& name,
62  const string& value, EValueType type,
63  const string& comment) = 0;
64 };
65 
66 
67 //---------------------------------------------------------------------------
68 // CDebugDumpContext provides client interface in the form [name=value]
69 
70 class CDebugDumpable;
72 {
73 public:
74  CDebugDumpContext(CDebugDumpFormatter& formatter, const string& bundle);
75  // This is not exactly a copy constructor -
76  // this mechanism is used internally to find out
77  // where are we on the Dump tree
79  CDebugDumpContext(CDebugDumpContext& ddc, const string& bundle);
80  virtual ~CDebugDumpContext(void);
81 
82 public:
83  // First thing in DebugDump() function - call this function
84  // providing class type as the frame name
85  void SetFrame(const string& frame);
86  // Log data in the form [name, data, comment]
87  // All data is passed to a formatter as string, still sometimes
88  // it is probably worth to emphasize that the data is REALLY a string
89  void Log(const string& name, const char* value,
91  const string& comment = kEmptyStr);
92  void Log(const string& name, const string& value,
94  const string& comment = kEmptyStr);
95  void Log(const string& name, bool value,
96  const string& comment = kEmptyStr);
97  void Log(const string& name, short value,
98  const string& comment = kEmptyStr);
99  void Log(const string& name, unsigned short value,
100  const string& comment = kEmptyStr);
101  void Log(const string& name, int value,
102  const string& comment = kEmptyStr);
103  void Log(const string& name, unsigned int value,
104  const string& comment = kEmptyStr);
105  void Log(const string& name, long value,
106  const string& comment = kEmptyStr);
107  void Log(const string& name, unsigned long value,
108  const string& comment = kEmptyStr);
109 #ifndef NCBI_INT8_IS_LONG
110  void Log(const string& name, Int8 value,
111  const string& comment = kEmptyStr);
112  void Log(const string& name, Uint8 value,
113  const string& comment = kEmptyStr);
114 #endif
115  void Log(const string& name, double value,
116  const string& comment = kEmptyStr);
117  void Log(const string& name, const void* value,
118  const string& comment = kEmptyStr);
119  void Log(const string& name, const CDebugDumpable* value,
120  unsigned int depth);
121 
122 private:
123  void x_VerifyFrameStarted(void);
124  void x_VerifyFrameEnded(void);
125 
128  unsigned int m_Level;
130  string m_Title;
131  bool m_Started;
132 
133 };
134 
135 
136 //---------------------------------------------------------------------------
137 // CDebugDumpable defines DebugDump() functionality (abstract base class)
138 
140 {
141 public:
142  CDebugDumpable(void) {}
143  virtual ~CDebugDumpable(void);
144 
145  // Enable/disable debug dump
146  static void EnableDebugDump(bool on);
147 
148  // Dump using text formatter
149  void DebugDumpText(ostream& out,
150  const string& bundle, unsigned int depth) const;
151  // Dump using external dump formatter
152  void DebugDumpFormat(CDebugDumpFormatter& ddf,
153  const string& bundle, unsigned int depth) const;
154 
155  // Function that does the dump - to be overloaded
156  virtual void DebugDump(CDebugDumpContext ddc, unsigned int depth) const = 0;
157 
158  void DumpToConsole(void) const;
159 
160 private:
161  static bool sm_DumpEnabled;
162 };
163 
164 
165 //---------------------------------------------------------------------------
166 // CDebugDumpFormatterText defines text debug dump formatter class
167 
169 {
170 public:
171  CDebugDumpFormatterText(ostream& out);
172  virtual ~CDebugDumpFormatterText(void);
173 
174 public:
175  virtual bool StartBundle(unsigned int level, const string& bundle);
176  virtual void EndBundle( unsigned int level, const string& bundle);
177 
178  virtual bool StartFrame( unsigned int level, const string& frame);
179  virtual void EndFrame( unsigned int level, const string& frame);
180 
181  virtual void PutValue( unsigned int level, const string& name,
182  const string& value, EValueType type,
183  const string& comment);
184 
185 private:
186  void x_IndentLine(unsigned int level, char c = ' ', unsigned int len = 2);
187  void x_InsertPageBreak(const string& title = kEmptyStr,
188  char c = '=', unsigned int len = 78);
189 
190  ostream& m_Out;
191 };
192 
193 
194 
195 /****************************************************************************
196  Collection of debug dump function templates
197 ****************************************************************************/
198 
199 //---------------------------------------------------------------------------
200 // Value
201 
202 // Log a "simple" value
203 // "Simple" means that output stream understands how to dump it
204 // (i.e. operator 'os<<value' makes sense)
205 template<class T>
206 void DebugDumpValue( CDebugDumpContext& _this, const string& name,
207  const T& value, const string& comment = kEmptyStr)
208 {
209  ostringstream os;
210  os << value << '\0';
211  _this.Log(name, os.str(), CDebugDumpFormatter::eValue, comment);
212 }
213 
214 
215 //---------------------------------------------------------------------------
216 // non-associative STL containers
217 
218 // Log range of pointers to dumpable objects
219 template<class T>
220 void DebugDumpRangePtr( CDebugDumpContext& _this, const string& name,
221  T it, T it_end, unsigned int depth)
222 {
223  if (depth == 0) {
224  return;
225  }
226  --depth;
227  // container is an object - so,
228  // to start a sub-bundle we create a new CDebugDumpContext
229  CDebugDumpContext ddc2(_this,name);
230  for ( int n=0; it != it_end; ++it, ++n) {
231  string member_name = name + "[ " + NStr::IntToString(n) + " ]";
232  ddc2.Log(member_name, (*it), depth);
233  }
234 }
235 
236 
237 // Log range of dumpable objects
238 template<class T>
239 void DebugDumpRangeObj( CDebugDumpContext& _this, const string& name,
240  T it, T it_end, unsigned int depth)
241 {
242  if (depth == 0) {
243  return;
244  }
245  --depth;
246  CDebugDumpContext ddc2(_this,name);
247  for ( int n=0; it != it_end; ++it, ++n) {
248  string member_name = name + "[ " + NStr::IntToString(n) + " ]";
249  ddc2.Log(member_name, &(*it), depth);
250  }
251 }
252 
253 
254 // Log range of CRefs to dumpable objects
255 template<class T>
256 void DebugDumpRangeCRef( CDebugDumpContext& _this, const string& name,
257  T it, T it_end, unsigned int depth)
258 {
259  if (depth == 0) {
260  return;
261  }
262  --depth;
263  CDebugDumpContext ddc2(_this,name);
264  for ( int n=0; it != it_end; ++it, ++n) {
265  string member_name = name + "[ " + NStr::IntToString(n) + " ]";
266  ddc2.Log(member_name, (*it).GetPointer(), depth);
267  }
268 }
269 
270 //---------------------------------------------------------------------------
271 // associative STL containers
272 
273 // Log range of pairs of pointers (map of ptr to ptr)
274 // "second" is a pointer to a dumpable objects
275 template<class T>
276 void DebugDumpPairsPtrPtr( CDebugDumpContext& _this, const string& name,
277  T it, T it_end, unsigned int depth)
278 {
279  if (depth == 0) {
280  return;
281  }
282  --depth;
283  CDebugDumpContext ddc2(_this,name);
284  for ( int n=0; it != it_end; ++it, ++n) {
285  string member_name = name + "[ " + NStr::PtrToString(
286  dynamic_cast<const CDebugDumpable*>(it->first)) + " ]";
287  ddc2.Log(member_name, it->second, depth);
288  }
289 }
290 
291 
292 // Log range of pairs of pointers (map of ptr to CRef)
293 // "second" is a pointer to a dumpable objects
294 template<class T>
295 void DebugDumpPairsPtrCRef( CDebugDumpContext& _this, const string& name,
296  T it, T it_end, unsigned int depth)
297 {
298  if (depth == 0) {
299  return;
300  }
301  --depth;
302  CDebugDumpContext ddc2(_this,name);
303  for ( int n=0; it != it_end; ++it, ++n) {
304  string member_name = name + "[ " + NStr::PtrToString(
305  dynamic_cast<const CDebugDumpable*>(it->first)) + " ]";
306  ddc2.Log(member_name,(it->second).GetPointer(), depth);
307  }
308 }
309 
310 
311 // Log range of pairs of pointers (map of CRef to CRef)
312 // "second" is a CRef to a dumpable objects
313 template<class T>
314 void DebugDumpPairsCRefCRef( CDebugDumpContext& _this, const string& name,
315  T it, T it_end, unsigned int depth)
316 {
317  if (depth == 0) {
318  return;
319  }
320  --depth;
321  CDebugDumpContext ddc2(_this,name);
322  for ( int n=0; it != it_end; ++it, ++n) {
323  string member_name = name + "[ " + NStr::PtrToString(
324  dynamic_cast<const CDebugDumpable*>((it->first).GetPointer()))
325  + " ]";
326  ddc2.Log(member_name,(it->second).GetPointer(), depth);
327  }
328 }
329 
330 // Log range of pairs of pointers
331 // "first" can be serialized into ostream
332 // "second" is a pointer to a dumpable objects
333 template<class T>
334 void DebugDumpPairsValuePtr( CDebugDumpContext& _this, const string& name,
335  T it, T it_end, unsigned int depth)
336 {
337  if (depth == 0) {
338  return;
339  }
340  --depth;
341  CDebugDumpContext ddc2(_this,name);
342  for ( int n=0; it != it_end; ++it, ++n) {
343  ostringstream os;
344  os << (it->first) << '\0';
345  string member_name = name + "[ " + os.str() + " ]";
346  ddc2.Log(member_name,it->second, depth);
347  }
348 }
349 
351 
352 #endif // DDUMPABLE__HPP
unsigned int m_Level
Definition: ddumpable.hpp:128
CDebugDumpContext & m_Parent
Definition: ddumpable.hpp:126
CDebugDumpFormatter & m_Formatter
Definition: ddumpable.hpp:127
void Log(const string &name, const char *value, CDebugDumpFormatter::EValueType type=CDebugDumpFormatter::eValue, const string &comment=kEmptyStr)
Definition: ddumpable.cpp:151
virtual bool StartBundle(unsigned int level, const string &bundle)=0
virtual void EndBundle(unsigned int level, const string &bundle)=0
virtual void PutValue(unsigned int level, const string &name, const string &value, EValueType type, const string &comment)=0
virtual bool StartFrame(unsigned int level, const string &frame)=0
virtual void EndFrame(unsigned int level, const string &frame)=0
virtual ~CDebugDumpFormatter()
Definition: ddumpable.hpp:53
static bool sm_DumpEnabled
Definition: ddumpable.hpp:161
virtual void DebugDump(CDebugDumpContext ddc, unsigned int depth) const =0
CDebugDumpable(void)
Definition: ddumpable.hpp:142
char value[7]
Definition: config.c:431
Include a standard set of the NCBI C++ Toolkit most basic headers.
static unsigned char depth[2 *(256+1+29)+1]
#define T(s)
Definition: common.h:230
void DebugDumpPairsPtrCRef(CDebugDumpContext &_this, const string &name, T it, T it_end, unsigned int depth)
Definition: ddumpable.hpp:295
void DebugDumpRangePtr(CDebugDumpContext &_this, const string &name, T it, T it_end, unsigned int depth)
Definition: ddumpable.hpp:220
void DebugDumpPairsPtrPtr(CDebugDumpContext &_this, const string &name, T it, T it_end, unsigned int depth)
Definition: ddumpable.hpp:276
void DebugDumpPairsCRefCRef(CDebugDumpContext &_this, const string &name, T it, T it_end, unsigned int depth)
Definition: ddumpable.hpp:314
void DebugDumpPairsValuePtr(CDebugDumpContext &_this, const string &name, T it, T it_end, unsigned int depth)
Definition: ddumpable.hpp:334
void DebugDumpRangeCRef(CDebugDumpContext &_this, const string &name, T it, T it_end, unsigned int depth)
Definition: ddumpable.hpp:256
void DebugDumpRangeObj(CDebugDumpContext &_this, const string &name, T it, T it_end, unsigned int depth)
Definition: ddumpable.hpp:239
void DebugDumpValue(CDebugDumpContext &_this, const string &name, const T &value, const string &comment=kEmptyStr)
Definition: ddumpable.hpp:206
std::ofstream out("events_result.xml")
main entry point for tests
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5083
static void PtrToString(string &out_str, const void *ptr)
Convert pointer to string.
Definition: ncbistr.cpp:2771
#define NCBI_XNCBI_EXPORT
Definition: ncbi_export.h:1283
yy_size_t n
int len
Definition: type.c:6
Modified on Thu Dec 07 10:08:18 2023 by modify_doxy.py rev. 669887