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

Go to the SVN repository for this file.

1 /* $Id: ddumpable.cpp 71273 2016-02-23 19:38:23Z gouriano $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Andrei Gourianov
27  *
28  * File Description:
29  * Debug Dump functionality
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ddumpable.hpp>
35 
37 
38 
39 //---------------------------------------------------------------------------
40 // CDebugDumpable defines DebugDump() functionality (abstract base class)
41 
43 
45 {
46  return;
47 }
48 
49 
51 {
52  sm_DumpEnabled = on;
53 }
54 
55 
57  const string& bundle,
58  unsigned int depth)
59  const
60 {
61  if ( sm_DumpEnabled ) {
63  DebugDumpFormat(ddf, bundle, depth);
64  }
65 }
66 
67 
69  const string& bundle,
70  unsigned int depth)
71  const
72 {
73  if ( sm_DumpEnabled ) {
74  CDebugDumpContext ddc(ddf, bundle);
75  DebugDump(ddc, depth);
76  }
77 }
78 
79 
81 {
82  DebugDumpText(cout, kEmptyStr, 0);
83 }
84 
85 //---------------------------------------------------------------------------
86 // CDebugDumpContext provides client interface in the form [name=value]
87 
89  const string& bundle)
90  : m_Parent(*this),
91  m_Formatter(formatter),
92  m_Title(bundle)
93 {
94  m_Level = 0;
95  m_Start_Bundle = true;
96  m_Started = false;
97 }
98 
99 
101  : m_Parent(ddc),
102  m_Formatter(ddc.m_Formatter)
103 {
105  m_Level = m_Parent.m_Level + 1;
106  m_Start_Bundle = false;
107  m_Started = false;
108 }
109 
110 
112  const string& bundle)
113  : m_Parent(ddc),
114  m_Formatter(ddc.m_Formatter),
115  m_Title(bundle)
116 {
118  m_Level = m_Parent.m_Level + 1;
119  m_Start_Bundle = true;
120  m_Started = false;
121 }
122 
123 
125 {
126  if (&m_Parent == this)
127  return;
128 
131  if (m_Level == 1) {
133  }
134 }
135 
136 
137 void CDebugDumpContext::SetFrame(const string& frame)
138 {
139  if ( m_Started )
140  return;
141 
142  if (m_Start_Bundle) {
144  } else {
145  m_Title = frame;
147  }
148 }
149 
150 
151 void CDebugDumpContext::Log(const string& name,
152  const char* value,
154  const string& comment)
155 {
156  Log(name,value ? string(value) : kEmptyStr,type,comment);
157 }
158 
159 
160 void CDebugDumpContext::Log(const string& name,
161  const string& value,
163  const string& comment)
164 {
166  if ( m_Started ) {
167  m_Formatter.PutValue(m_Level, name, value, type, comment);
168  }
169 }
170 
171 
172 void CDebugDumpContext::Log(const string& name, bool value,
173  const string& comment)
174 {
176 }
177 
178 
179 void CDebugDumpContext::Log(const string& name, short value,
180  const string& comment)
181 {
183 }
184 
185 
186 void CDebugDumpContext::Log(const string& name, unsigned short value,
187  const string& comment)
188 {
189  Log(name, NStr::UIntToString((unsigned int)value), CDebugDumpFormatter::eValue, comment);
190 }
191 
192 
193 void CDebugDumpContext::Log(const string& name, int value,
194  const string& comment)
195 {
197 }
198 
199 
200 void CDebugDumpContext::Log(const string& name, unsigned int value,
201  const string& comment)
202 {
204 }
205 
206 
207 void CDebugDumpContext::Log(const string& name, long value,
208  const string& comment)
209 {
211 }
212 
213 
214 void CDebugDumpContext::Log(const string& name, unsigned long value,
215  const string& comment)
216 {
218 }
219 
220 #ifndef NCBI_INT8_IS_LONG
221 void CDebugDumpContext::Log(const string& name, Int8 value,
222  const string& comment)
223 {
225 }
226 
227 
228 void CDebugDumpContext::Log(const string& name, Uint8 value,
229  const string& comment)
230 {
232 }
233 #endif
234 
235 
236 void CDebugDumpContext::Log(const string& name, double value,
237  const string& comment)
238 {
240  comment);
241 }
242 
243 
244 void CDebugDumpContext::Log(const string& name, const void* value,
245  const string& comment)
246 {
248 }
249 
250 
251 void CDebugDumpContext::Log(const string& name, const CDebugDumpable* value,
252  unsigned int depth)
253 {
254  if (depth != 0 && value) {
255  CDebugDumpContext ddc(*this, name);
256  value->DebugDump(ddc, depth - 1);
257  } else {
258  Log(name, NStr::PtrToString(static_cast<const void*> (value)),
260  }
261 }
262 
263 
265 {
266  SetFrame(m_Title);
267 }
268 
269 
271 {
272  if ( !m_Started )
273  return;
274 
275  if (m_Start_Bundle) {
277  } else {
279  }
280 
281  m_Started = false;
282 }
283 
284 
285 //---------------------------------------------------------------------------
286 // CDebugDumpFormatterText defines text debug dump formatter class
287 
289  : m_Out(out)
290 {
291 }
292 
294 {
295 }
296 
297 bool CDebugDumpFormatterText::StartBundle(unsigned int level,
298  const string& bundle)
299 {
300  if (level == 0) {
301  x_InsertPageBreak(bundle);
302  } else {
303  m_Out << endl;
304  x_IndentLine(level);
305  m_Out << (bundle.empty() ? "?" : bundle.c_str()) << " = {";
306  }
307  return true;
308 }
309 
310 
311 void CDebugDumpFormatterText::EndBundle(unsigned int level,
312  const string& /*bundle*/)
313 {
314  if (level == 0) {
316  m_Out << endl;
317  } else {
318  m_Out << endl;
319  x_IndentLine(level);
320  m_Out << "}";
321  }
322 }
323 
324 
325 bool CDebugDumpFormatterText::StartFrame(unsigned int level,
326  const string& frame)
327 {
328  m_Out << endl;
329  x_IndentLine(level);
330  m_Out << (frame.empty() ? "?" : frame.c_str()) << " {";
331  return true;
332 }
333 
334 
335 void CDebugDumpFormatterText::EndFrame(unsigned int /*level*/,
336  const string& /*frame*/)
337 {
338  m_Out << " }";
339 }
340 
341 
342 void CDebugDumpFormatterText::PutValue(unsigned int level,
343  const string& name, const string& value,
344  EValueType type, const string& comment)
345 {
346  m_Out << endl;
347  x_IndentLine(level + 1);
348 
349  m_Out << name << " = " ;
350  if (type == eString) {
351  m_Out << '"' << value << '"';
352  } else {
353  m_Out << value;
354  }
355 
356  if ( !comment.empty() ) {
357  m_Out << " (" << comment << ")";
358  }
359 }
360 
361 
362 void CDebugDumpFormatterText::x_IndentLine(unsigned int level, char c,
363  unsigned int len)
364 {
365  m_Out << string(level * len, c);
366 }
367 
368 
369 void CDebugDumpFormatterText::x_InsertPageBreak(const string& title, char c,
370  unsigned int len)
371 {
372  m_Out << endl;
373 
374  string tmp;
375  if ( !title.empty() ) {
376  if (len < title.length() + 2) {
377  tmp = title;
378  } else {
379  size_t i1 = (len - title.length() - 2) / 2;
380  tmp.append(i1, c);
381  tmp += " " + title + " ";
382  tmp.append(i1, c);
383  }
384  } else {
385  tmp.append(len, c);
386  }
387 
388  m_Out << tmp;
389 }
390 
391 
void x_VerifyFrameEnded(void)
Definition: ddumpable.cpp:270
unsigned int m_Level
Definition: ddumpable.hpp:128
CDebugDumpContext(CDebugDumpFormatter &formatter, const string &bundle)
Definition: ddumpable.cpp:88
void SetFrame(const string &frame)
Definition: ddumpable.cpp:137
void x_VerifyFrameStarted(void)
Definition: ddumpable.cpp:264
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 ~CDebugDumpContext(void)
Definition: ddumpable.cpp:124
virtual ~CDebugDumpFormatterText(void)
Definition: ddumpable.cpp:293
virtual bool StartBundle(unsigned int level, const string &bundle)
Definition: ddumpable.cpp:297
void x_InsertPageBreak(const string &title=kEmptyStr, char c='=', unsigned int len=78)
Definition: ddumpable.cpp:369
virtual bool StartFrame(unsigned int level, const string &frame)
Definition: ddumpable.cpp:325
virtual void PutValue(unsigned int level, const string &name, const string &value, EValueType type, const string &comment)
Definition: ddumpable.cpp:342
virtual void EndFrame(unsigned int level, const string &frame)
Definition: ddumpable.cpp:335
CDebugDumpFormatterText(ostream &out)
Definition: ddumpable.cpp:288
void x_IndentLine(unsigned int level, char c=' ', unsigned int len=2)
Definition: ddumpable.cpp:362
virtual void EndBundle(unsigned int level, const string &bundle)
Definition: ddumpable.cpp:311
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
void DumpToConsole(void) const
Definition: ddumpable.cpp:80
void DebugDumpText(ostream &out, const string &bundle, unsigned int depth) const
Definition: ddumpable.cpp:56
static bool sm_DumpEnabled
Definition: ddumpable.hpp:161
void DebugDumpFormat(CDebugDumpFormatter &ddf, const string &bundle, unsigned int depth) const
Definition: ddumpable.cpp:68
virtual void DebugDump(CDebugDumpContext ddc, unsigned int depth) const =0
static void EnableDebugDump(bool on)
Definition: ddumpable.cpp:50
virtual ~CDebugDumpable(void)
Definition: ddumpable.cpp:44
static unsigned char depth[2 *(256+1+29)+1]
std::ofstream out("events_result.xml")
main entry point for tests
static char tmp[3200]
Definition: utf8.c:42
string
Definition: cgiapp.hpp:687
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
static string Int8ToString(Int8 value, TNumToStringFlags flags=0, int base=10)
Convert Int8 to string.
Definition: ncbistr.hpp:5161
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5189
#define kEmptyStr
Definition: ncbistr.hpp:123
static string LongToString(long value, TNumToStringFlags flags=0, int base=10)
Convert Int to string.
Definition: ncbistr.hpp:5143
static const string BoolToString(bool value)
Convert bool to string.
Definition: ncbistr.cpp:2806
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5086
static void PtrToString(string &out_str, const void *ptr)
Convert pointer to string.
Definition: ncbistr.cpp:2762
static string UIntToString(unsigned int value, TNumToStringFlags flags=0, int base=10)
Convert UInt to string.
Definition: ncbistr.hpp:5111
static string ULongToString(unsigned long value, TNumToStringFlags flags=0, int base=10)
Convert unsigned long to string.
Definition: ncbistr.hpp:5152
static string UInt8ToString(Uint8 value, TNumToStringFlags flags=0, int base=10)
Convert UInt8 to string.
Definition: ncbistr.hpp:5170
int len
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Definition: type.c:6
Modified on Wed Sep 04 14:58:56 2024 by modify_doxy.py rev. 669887