ACE有一个跟踪类ACE_Trace,本文为了弄清这个跟踪类,并增强一些功能,自己仿制一个Trace跟踪类,这个类完全模仿ACE_Trace类,只是在开始增加了一个标题。这个标题你可以在#define时进行定制。
以下为头文件:
//Trace.h
// Trace.h:
//
#pragma once
#define ACE_NDEBUG0
#define ACE_NTRACE0
#include " ace/log_msg.h "
#include " ace/Object_Manager.h "
class Trace
{
public :
Trace( const ACE_TCHAR * prefix,
const ACE_TCHAR * name,
int line,
const ACE_TCHAR * file)
{
this -> prefix_ = prefix;
this -> name_ = name;
this -> line_ = line;
this -> file_ = file;
ACE_Log_Msg * lm = ACE_LOG_MSG;
if (lm -> tracing_enabled()
&& lm -> trace_active() == 0 )
{
lm -> trace_active( 1 );
ACE_DEBUG((LM_TRACE,
ACE_TEXT( " %s%*s(%t)calling%sinfile'%s' " )
ACE_TEXT( " online%d " ),
this -> prefix_,
Trace::nesting_indent_ * lm -> inc(),
ACE_TEXT( "" ),
this -> name_,
this -> file_,
this -> line_));
lm -> trace_active( 0 );
}
}
void setLine( int line)
{
this -> line_ = line;
}
~ Trace( void )
{
ACE_Log_Msg * lm = ACE_LOG_MSG;
if (lm -> tracing_enabled()
&& lm -> trace_active() == 0 )
{
lm -> trace_active( 1 );
ACE_DEBUG
((LM_TRACE,
ACE_TEXT( " %s%*s(%t)leaving%sinfile'%s' " )
ACE_TEXT( " online%d " ),
this -> prefix_,
Trace::nesting_indent_ * lm -> dec(),
ACE_TEXT( "" ),
this -> name_,
this -> file_,
this -> line_));
lm -> trace_active( 0 );
}
}
private :
enum {nesting_indent_ = 3 } ;
const ACE_TCHAR * prefix_;
const ACE_TCHAR * name_;
const ACE_TCHAR * file_;
int line_;
} ;
#define TRACE_PREFIXACE_TEXT("TRACE")
#if (ACE_NTRACE==1)
#defineTRACE(X)
#defineTRACE_RETURN(V)
#defineTRACE_RETURN_VOID()
#else
#defineTRACE(X)
Trace____(TRACE_PREFIX,
ACE_TEXT(X),
__LINE__,
ACE_TEXT(__FILE__))
#defineTRACE_RETURN(V)
do {____.setLine(__LINE__); return V;} while ( 0 )
#defineTRACE_RETURN_VOID()
do {____.setLine(__LINE__);} while ( 0 )
#endif
下面为测试程序:
//
testTrace.cpp:Definestheentrypointfortheconsoleapplication.
//
#include " stdafx.h "
#include " trace.h "
int test2()
{
TRACE(ACE_TEXT( " test2 " ));
return 0 ;
}
int test()
{
TRACE(ACE_TEXT( " Test " ));
test2();
return 0 ;
}
int _tmain( int argc,_TCHAR * argv[])
{
TRACE(ACE_TEXT( " main " ));
test();
return 0 ;
}
//
#include " stdafx.h "
#include " trace.h "
int test2()
{
TRACE(ACE_TEXT( " test2 " ));
return 0 ;
}
int test()
{
TRACE(ACE_TEXT( " Test " ));
test2();
return 0 ;
}
int _tmain( int argc,_TCHAR * argv[])
{
TRACE(ACE_TEXT( " main " ));
test();
return 0 ;
}
以上程序皆是在vs2005上测试通过。