ddumpable.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:12k
- /*
- * ===========================================================================
- * PRODUCTION $Log: ddumpable.cpp,v $
- * PRODUCTION Revision 1000.4 2004/06/01 19:08:28 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.11
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: ddumpable.cpp,v 1000.4 2004/06/01 19:08:28 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Author: Andrei Gourianov
- *
- * File Description:
- * Debug Dump functionality
- *
- */
- #include <ncbi_pch.hpp>
- #include <corelib/ddumpable.hpp>
- BEGIN_NCBI_SCOPE
- //---------------------------------------------------------------------------
- // CDebugDumpable defines DebugDump() functionality (abstract base class)
- bool CDebugDumpable::sm_DumpEnabled = true;
- CDebugDumpable::~CDebugDumpable(void)
- {
- return;
- }
- void CDebugDumpable::EnableDebugDump(bool on)
- {
- sm_DumpEnabled = on;
- }
- void CDebugDumpable::DebugDumpText(ostream& out,
- const string& bundle,
- unsigned int depth)
- const
- {
- if ( sm_DumpEnabled ) {
- CDebugDumpFormatterText ddf(out);
- DebugDumpFormat(ddf, bundle, depth);
- }
- }
- void CDebugDumpable::DebugDumpFormat(CDebugDumpFormatter& ddf,
- const string& bundle,
- unsigned int depth)
- const
- {
- if ( sm_DumpEnabled ) {
- CDebugDumpContext ddc(ddf, bundle);
- DebugDump(ddc, depth);
- }
- }
- //---------------------------------------------------------------------------
- // CDebugDumpContext provides client interface in the form [name=value]
- CDebugDumpContext::CDebugDumpContext(CDebugDumpFormatter& formatter,
- const string& bundle)
- : m_Parent(*this),
- m_Formatter(formatter),
- m_Title(bundle)
- {
- m_Level = 0;
- m_Start_Bundle = true;
- m_Started = false;
- }
- CDebugDumpContext::CDebugDumpContext(CDebugDumpContext& ddc)
- : m_Parent(ddc),
- m_Formatter(ddc.m_Formatter)
- {
- m_Parent.x_VerifyFrameStarted();
- m_Level = m_Parent.m_Level + 1;
- m_Start_Bundle = false;
- m_Started = false;
- }
- CDebugDumpContext::CDebugDumpContext(CDebugDumpContext& ddc,
- const string& bundle)
- : m_Parent(ddc),
- m_Formatter(ddc.m_Formatter),
- m_Title(bundle)
- {
- m_Parent.x_VerifyFrameStarted();
- m_Level = m_Parent.m_Level + 1;
- m_Start_Bundle = true;
- m_Started = false;
- }
- CDebugDumpContext::~CDebugDumpContext(void)
- {
- if (&m_Parent == this)
- return;
- x_VerifyFrameStarted();
- x_VerifyFrameEnded();
- if (m_Level == 1) {
- m_Parent.x_VerifyFrameEnded();
- }
- }
- void CDebugDumpContext::SetFrame(const string& frame)
- {
- if ( m_Started )
- return;
- if (m_Start_Bundle) {
- m_Started = m_Formatter.StartBundle(m_Level, m_Title);
- } else {
- m_Title = frame;
- m_Started = m_Formatter.StartFrame(m_Level, m_Title);
- }
- }
- void CDebugDumpContext::Log(const string& name,
- const char* value,
- CDebugDumpFormatter::EValueType type,
- const string& comment)
- {
- Log(name,value ? string(value) : kEmptyStr,type,comment);
- }
- void CDebugDumpContext::Log(const string& name,
- const string& value,
- CDebugDumpFormatter::EValueType type,
- const string& comment)
- {
- x_VerifyFrameStarted();
- if ( m_Started ) {
- m_Formatter.PutValue(m_Level, name, value, type, comment);
- }
- }
- void CDebugDumpContext::Log(const string& name, bool value,
- const string& comment)
- {
- Log(name, NStr::BoolToString(value), CDebugDumpFormatter::eValue, comment);
- }
- void CDebugDumpContext::Log(const string& name, short value,
- const string& comment)
- {
- Log(name, NStr::IntToString(value), CDebugDumpFormatter::eValue, comment);
- }
- void CDebugDumpContext::Log(const string& name, unsigned short value,
- const string& comment)
- {
- Log(name, NStr::UIntToString(value), CDebugDumpFormatter::eValue, comment);
- }
- void CDebugDumpContext::Log(const string& name, int value,
- const string& comment)
- {
- Log(name, NStr::IntToString(value), CDebugDumpFormatter::eValue, comment);
- }
- void CDebugDumpContext::Log(const string& name, unsigned int value,
- const string& comment)
- {
- Log(name, NStr::UIntToString(value), CDebugDumpFormatter::eValue, comment);
- }
- void CDebugDumpContext::Log(const string& name, long value,
- const string& comment)
- {
- Log(name, NStr::IntToString(value), CDebugDumpFormatter::eValue, comment);
- }
- void CDebugDumpContext::Log(const string& name, unsigned long value,
- const string& comment)
- {
- Log(name, NStr::UIntToString(value), CDebugDumpFormatter::eValue, comment);
- }
- #if (SIZEOF_LONG < 8)
- void CDebugDumpContext::Log(const string& name, Int8 value,
- const string& comment)
- {
- Log(name, NStr::Int8ToString(value), CDebugDumpFormatter::eValue, comment);
- }
- void CDebugDumpContext::Log(const string& name, Uint8 value,
- const string& comment)
- {
- Log(name, NStr::UInt8ToString(value), CDebugDumpFormatter::eValue, comment);
- }
- #endif
- void CDebugDumpContext::Log(const string& name, double value,
- const string& comment)
- {
- Log(name, NStr::DoubleToString(value), CDebugDumpFormatter::eValue,
- comment);
- }
- void CDebugDumpContext::Log(const string& name, const void* value,
- const string& comment)
- {
- Log(name, NStr::PtrToString(value), CDebugDumpFormatter::eValue, comment);
- }
- void CDebugDumpContext::Log(const string& name, const CDebugDumpable* value,
- unsigned int depth)
- {
- if (depth != 0 && value) {
- CDebugDumpContext ddc(*this, name);
- value->DebugDump(ddc, depth - 1);
- } else {
- Log(name, NStr::PtrToString(static_cast<const void*> (value)),
- CDebugDumpFormatter::ePointer);
- }
- }
- void CDebugDumpContext::x_VerifyFrameStarted(void)
- {
- SetFrame(m_Title);
- }
- void CDebugDumpContext::x_VerifyFrameEnded(void)
- {
- if ( !m_Started )
- return;
- if (m_Start_Bundle) {
- m_Formatter.EndBundle(m_Level, m_Title);
- } else {
- m_Formatter.EndFrame(m_Level, m_Title);
- }
- m_Started = false;
- }
- //---------------------------------------------------------------------------
- // CDebugDumpFormatterText defines text debug dump formatter class
- CDebugDumpFormatterText::CDebugDumpFormatterText(ostream& out)
- : m_Out(out)
- {
- }
- CDebugDumpFormatterText::~CDebugDumpFormatterText(void)
- {
- }
- bool CDebugDumpFormatterText::StartBundle(unsigned int level,
- const string& bundle)
- {
- if (level == 0) {
- x_InsertPageBreak(bundle);
- } else {
- m_Out << endl;
- x_IndentLine(level);
- m_Out << (bundle.empty() ? "?" : bundle.c_str()) << " = {";
- }
- return true;
- }
- void CDebugDumpFormatterText::EndBundle(unsigned int level,
- const string& /*bundle*/)
- {
- if (level == 0) {
- x_InsertPageBreak();
- m_Out << endl;
- } else {
- m_Out << endl;
- x_IndentLine(level);
- m_Out << "}";
- }
- }
- bool CDebugDumpFormatterText::StartFrame(unsigned int level,
- const string& frame)
- {
- m_Out << endl;
- x_IndentLine(level);
- m_Out << (frame.empty() ? "?" : frame.c_str()) << " {";
- return true;
- }
- void CDebugDumpFormatterText::EndFrame(unsigned int /*level*/,
- const string& /*frame*/)
- {
- m_Out << " }";
- }
- void CDebugDumpFormatterText::PutValue(unsigned int level,
- const string& name, const string& value,
- EValueType type, const string& comment)
- {
- m_Out << endl;
- x_IndentLine(level + 1);
- m_Out << name << " = " ;
- if (type == eString) {
- m_Out << '"' << value << '"';
- } else {
- m_Out << value;
- }
- if ( !comment.empty() ) {
- m_Out << " (" << comment << ")";
- }
- }
- void CDebugDumpFormatterText::x_IndentLine(unsigned int level, char c,
- unsigned int len)
- {
- m_Out << string(level * len, c);
- }
- void CDebugDumpFormatterText::x_InsertPageBreak(const string& title, char c,
- unsigned int len)
- {
- m_Out << endl;
- string tmp;
- if ( !title.empty() ) {
- if (len < title.length() + 2) {
- tmp = title;
- } else {
- size_t i1 = (len - title.length() - 2) / 2;
- tmp.append(i1, c);
- tmp += " " + title + " ";
- tmp.append(i1, c);
- }
- } else {
- tmp.append(len, c);
- }
- m_Out << tmp;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- * $Log: ddumpable.cpp,v $
- * Revision 1000.4 2004/06/01 19:08:28 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.11
- *
- * Revision 1.11 2004/05/14 13:59:26 gorelenk
- * Added include of ncbi_pch.hpp
- *
- * Revision 1.10 2003/12/09 21:13:49 ucko
- * Re-tweak logging of 64-bit numbers, as the previous revision broke
- * when __int64 and long long were synonymous. (Now uses Int8/Uint8
- * everywhere, but only when sizeof(long) < 8.)
- *
- * Revision 1.9 2003/12/09 17:27:22 gouriano
- * Corrected previously defined Log() functions to avoid conflicts
- * on 64 bit platforms
- *
- * Revision 1.8 2003/12/09 16:55:38 gouriano
- * Added Log() methods for Int8 and Uint8
- *
- * Revision 1.7 2003/11/18 14:13:43 gouriano
- * Corrected odd typecast in CDebugDumpContext::Log method
- *
- * Revision 1.6 2003/11/12 20:16:01 gouriano
- * Added more Log() methods: for short, int, and char* types
- *
- * Revision 1.5 2003/05/19 21:05:41 vakatov
- * x_InsertPageBreak() -- title: "" -> kEmptyStr, len: int -> unsigned int.
- * Fixed code indentation and simplified some code.
- *
- * Revision 1.4 2003/02/21 21:07:46 vakatov
- * Minor cast to get rid of 64-bit compilation warning
- *
- * Revision 1.3 2002/05/29 21:17:32 gouriano
- * changed formatter interface: added value type
- *
- * Revision 1.2 2002/05/28 17:59:24 gouriano
- * all DebugDump-related classes were put in one header
- * templates adjusted and debugged on multiple platforms
- *
- * Revision 1.1 2002/05/17 14:27:11 gouriano
- * added DebugDump base class and function to CObject
- * ===========================================================================
- */