ACE有一个跟踪类ACE_Trace,本文为了弄清这个跟踪类,并增强一些功能,自己仿制一个Trace跟踪类,这个类完全模仿ACE_Trace类,只是在开始增加了一个标题。这个标题你可以在#define时进行定制。
以下为头文件:
//Trace.h
// Trace.h :
//
#pragma once
#define ACE_NDEBUG 0
#define ACE_NTRACE 0
#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 %s in file '%s'")
ACE_TEXT("on line %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 %s in file '%s'")
ACE_TEXT (" on line %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_PREFIX ACE_TEXT ("TRACE ")
#if (ACE_NTRACE == 1)
# define TRACE(X)
# define TRACE_RETURN(V)
# define TRACE_RETURN_VOID()
#else
# define TRACE(X)
Trace ____(TRACE_PREFIX,
ACE_TEXT (X),
__LINE__,
ACE_TEXT (__FILE__))
# define TRACE_RETURN(V)
do ...{ ____.setLine(__LINE__);return V;} while(0)
# define TRACE_RETURN_VOID()
do ...{ ____.setLine(__LINE__); } while(0)
#endif
下面为测试程序:
// testTrace.cpp : Defines the entry point for the console application.
//
#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上测试通过。