日志系统之ACE日志服务.docx
- 文档编号:5143242
- 上传时间:2022-12-13
- 格式:DOCX
- 页数:12
- 大小:21.26KB
日志系统之ACE日志服务.docx
《日志系统之ACE日志服务.docx》由会员分享,可在线阅读,更多相关《日志系统之ACE日志服务.docx(12页珍藏版)》请在冰豆网上搜索。
日志系统之ACE日志服务
日志系统之ACE日志服务
2007-8-22
目录
1基本使用2
1.1日志输出宏2
1.2日志的等级2
1.3格式化指令3
1.4例1-日志的简单示例4
2日志的输出过滤5
2.1输出过滤原理5
2.2编译时刻设定6
2.3运行时刻设定8
3日志的输出重定向9
3.1输出重定向的涉及函数9
3.2例2-输出重定向的综合示例10
4使用LoggingStrategy进行运行时配置11
4.1配置选项11
4.2例3-运行配置示例12
ACE日志服务是ACE库的一个功能模块,具有线程安全、灵活、分级显示等特点,可以面向程序调试、运行、测试、和维护等全生命周期,可以选择将信息输出到屏幕、文件、系统日志(如Windows下的Eventlog)、甚至是远程服务器。
除此之外,ACE日志系统支持回调函数以及运行时刻动态配置(RuntimeConfiguration)。
1基本使用
1.1日志输出宏
ACE日志的使用主要通过宏ACE_XXX((severity,formatting-args))进行日志输出。
其中severity为日志等级(见1.2小节),formatting-args是要输出的内容,类似printf()函数的可变参数,其中也可以包含以’%’开头的格式化指令(见1.3小节)。
注意日志输出宏包括两层圆括号,外层括号是为了宏展开时指定宏的参数,内层的括号则为了宏展开成函数后指定函数参数。
日志输出宏包括:
●ACE_ERROR((level,string,...))
●ACE_DEBUG((level,string,...))
●ACE_TRACE(string)
●ACE_ASSERT(test)
●ACE_HEX_DUMP((level,buffer,size[,text]))
●ACE_RETURN(value)
●ACE_ERROR_RETURN((level,string,...),value)
●ACE_ERROR_INIT(value,flags)
●ACE_ERROR_BREAK((level,string,...))
1.2日志的等级
SeverityLevel
Meaning
LM_TRACE
Messagesindicatingfunction-callingsequence
LM_DEBUG
Debugginginformation
LM_INFO
Messagesthatcontaininformationnormallyofuseonlywhendebuggingaprogram
LM_NOTICE
Conditionsthatarenoterrorconditionsbutthatmayrequirespecialhandling
LM_WARNING
Warningmessages
LM_ERROR
Errormessages
LM_CRITICAL
Criticalconditions,suchasharddeviceerrors
LM_ALERT
Aconditionthatshouldbecorrectedimmediately,suchasacorruptedsystemdatabase
LM_EMERGENCY
Apaniccondition,normallybroadcasttoallusers
1.3格式化指令
Code
ArgumentType
Displays
A
ACE_timer_t
Floating-pointnumber;longdecimalnumberifplatformdoesn'tsupportfloatingpoint
a
—
Abortstheprogramafterdisplayingoutput
c
char
Singlecharacter
C
char*
Characterstring(narrowcharacters)
i,d
int
Decimalnumber
I
—
Indentsoutputaccordingtothenestingdepth,obtainedfromACE_Trace:
:
get_nesting_indent()
e,E,f,F,g,G
double
Double-precisionfloating-pointnumber
l
—
Linenumberwhereloggingmacroappears
M
—
Textformofthemessageseveritylevel
m
—
Messagecorrespondingtoerrnovalue,asdonebystrerror(),forexample
N
—
Filenamewhereloggingmacroappears
n
—
ProgramnamegiventoACE_Log_Msg:
:
open()
o
int
Octalnumber
P
—
CurrentprocessID
p
ACE_TCHAR*
Specifiedcharacterstring,followedbytheappropriateerrnomessage,thatis,asdonebyperror()
Q
ACE_UINT64
Decimalnumber
r
void(*)()
Nothing;callsthespecifiedfunction
R
int
Decimalnumber
S
int
Signalnameofthenumberedsignal
s
ACE_TCHAR*
Characterstring:
narroworwide,accordingtoACE_TCHARtype
T
—
Currenttimeashour:
minute:
sec.usec
D
—
Timestampasmonth/day/yearhour:
minute:
sec.usec
t
—
Callingthread'sID(1ifsinglethreaded)
u
int
Unsigneddecimalnumber
w
wchar_t
Singlewidecharacter
W
wchar_t*
Wide-characterstring
x,X
int
Hexadecimalnumber
@
void*
Pointervalueinhexadecimal
%
N/A
Singlepercentsign:
"%"
1.4例1-日志的简单示例
#include"ace/Log_Msg.h"
voidfoo(void);
intACE_TMAIN(int,ACE_TCHAR*[])
{
ACE_TRACE(ACE_TEXT("main"));
ACE_DEBUG((LM_INFO,ACE_TEXT("%IHiMom\n")));
foo();
ACE_DEBUG((LM_INFO,ACE_TEXT("%IGoodnight\n")));
return0;
}
voidfoo(void)
{
ACE_TRACE(ACE_TEXT("foo"));
ACE_DEBUG((LM_INFO,ACE_TEXT("%IHowdyPardner\n")));
}
输出结果:
(1024)callingmaininfile`Simple1.cpp'online7
HiMom
(1024)callingfooinfile`Simple1.cpp'online18
HowdyPardner
(1024)leavingfoo
Goodnight
(1024)leavingmain
2日志的输出过滤
2.1输出过滤原理
在ACE日志系统中主要是通过ACE_Log_Msg类实现日志消息的格式化功能,ACE为主线程以及每个创建的线程维护一个线程专有的ACE_Log_Msg单例。
宏ACE_LOG_MSG的作用就是为获得该ACE_Log_Msg单例。
所有的ACE日志输出宏都使用宏ACE_LOG_MSG,所有很少会直接使用ACE_Log_Msg单例。
ACE日志系统通过使用掩码的方式进行输出过滤。
具体来说具有进程一级的掩码和线程一级的掩码控制。
这里所说的线程概念在ACE中被误用了,线程一级和ACE_Log_Msg实例一级的掩码等同,ACE在假定一个线程只有一个ACE_Log_Msg实例才混用了线程的概念。
2.2编译时刻设定
编译时刻可以通过条件编译的方式来设定输出过滤。
例如:
在#include之前定义一些宏,可以在编译时刻Enable/DisableloggingMacros
/*disableACE_DEBUGandACE_ERROR*/#defineACE_NLOGGING
/*enableACE_DEBUGandACE_ERROR(缺省)*/#undefACE_NLOGGING
/*enableACE_ASSERT(缺省)*/#undefACE_NDEBUG
/*disableACE_ASSERT*/#defineACE_NDEBUG
/*enableACE_TRACE*/#defineACE_NTRACE0
/*disableACE_TRACE(缺省)*/#defineACE_NTRACE1
Macro
Function
Disabledby
ACE_ASSERT(test)
Muchliketheassert()librarycall.Ifthetestfails,anassertionmessageincludingthefilenameandlinenumber,alongwiththetestitself,willbeprintedandtheapplicationaborted.
ACE_NDEBUG
ACE_HEX_DUMP
((level,buffer,size[,text]))
Dumpsthebufferasastringofhexdigits.Ifprovided,theoptionaltextparameterwillbeprintedpriortothehexstring.Theop_status[a]issetto0.
ACE_NLOGGING
ACE_RETURN(value)
Nomessageisprinted,thecallingfunctionreturnswithvalue;op_statusissettovalue.
ACE_NLOGGING
ACE_ERROR_RETURN
((level,string,...),value)
Logsthestringattherequestedlevel.Thecallingfunctionthenreturnswithvalue;op_statusissettovalue.
ACE_NLOGGING
ACE_ERROR((level,string,...))
Setstheop_statusto–1andlogsthestringattherequestedlevel.
ACE_NLOGGING
ACE_DEBUG((level,string,...))
Setstheop_statusto0andlogsthestringattherequestedlevel.
ACE_NLOGGING
ACE_ERROR_INIT(value,flags)
Setstheop_statustovalueandthelogger'soptionflagstoflags.ValidflagsvaluesaredefinedinTable3.5.
ACE_NLOGGING
ACE_ERROR_BREAK
((level,string,...))
InvokesACE_ERROR()followedbyabreak.Usethistodisplayanerrormessageandexitawhileorforloop,forinstance.
ACE_NLOGGING
ACE_TRACE(string)
Displaysthefilename,linenumber,andstringwhereACE_TRACEappears.Displays"Leaving'string'"whentheACE_TRACE-enclosingscopeexits.
ACE
2.3运行时刻设定
运行时候可以有两种方式可以设置输出过滤。
一种是通过priority_mask()方法进行设置和获取掩码。
priority_mask()方法的定义为:
//GetthecurrentACE_Log_Prioritymask.
u_longpriority_mask(MASK_TYPE=THREAD);
//SettheACE_Log_Prioritymask,returnsoriginalmask.
u_longpriority_mask(u_long,MASK_TYPE=THREAD);
例如典型的使用:
ACE_LOG_MSG->priority_mask(0,ACE_Log_Msg:
:
PROCESS);
ACE_LOG_MSG->priority_mask(LM_DEBUG|LM_NOTICE,
ACE_Log_Msg:
:
THREAD);
另外一种方法是使用disable_debug_messages()和enable_debug_messages()方法进行掩码设置。
它们主要是利用ACE_Log_Msg单例的默认掩码,在使用ACE_Thread_Manager类创建线程时候可以利用该默认掩码进行统一设置,简化操作。
方法定义为:
staticvoiddisable_debug_messages
(ACE_Log_Prioritypriority=LM_DEBUG);
staticvoidenable_debug_messages
(ACE_Log_Prioritypriority=LM_DEBUG);
例如典型的使用:
ACE_LOG_MSG->priority_mask(0,ACE_Log_Msg:
:
PROCESS);
ACE_Log_Msg:
:
enable_debug_messages();
ACE_Thread_Manager:
:
instance()->spawn(service);
ACE_Log_Msg:
:
disable_debug_messages();
ACE_Thread_Manager:
:
instance()->spawn_n(3,worker);
3日志的输出重定向
ACE日志默认的输出为标准错误,可以他通过重定向将日志输出到输出流例如文件,也可以重定向到系统logger例如Unix的syslog和NT的EventLog。
3.1输出重定向的涉及函数
设置或改变ACE日志输出目标(loggingsink)可以通过ACE_Log_Msg的方法open来完成,另外可通过set_flags和clr_flags来配合实现输出到多个目标。
open方法的定义为:
/**
*InitializetheACEloggingfacility.Suppliestheprogramname
*thatisavailabletoeachloggingmessagecall.Defaultarguments
*setuploggingtoSTDERRonly.
*
*@paramprog_name Thenameofthecallingprogram.
*@paramoptions_flags Abitwise-orofoptionsflagsusedtosetthe
* initialbehaviorandloggingsink(s).(seethe
* enumaboveforthevalidvalues).
*@paramlogger_key ThenameofACE_FIFOrendezvouspointwherethe
* localclientloggerdaemonislisteningforlogging
* messages.OnlymeaningfuliftheLOGGERbitis
* setinthe@aflagsargument.
*/
intopen(constACE_TCHAR*prog_name,
u_longoptions_flags=ACE_Log_Msg:
:
STDERR,
constACE_TCHAR*logger_key=0
●第一个参数prog_name表示程序名称,可以任意设置,可通过ACE_Log_Msg的program_name方法获取,另外当第二个参数设置中有ACE_Log_Msg:
:
VERBOSE时,会在输出信息前加上前缀,前缀中就包含该program_name。
●第二个参数可以是:
参数值
参数说明
STDERR
WritemessagestoSTDERR
LOGGER
Writemessagestothelocalclientloggerdaemon
OSTREAM
Writemessagestotheassignedoutputstream
MSG_CALLBACK
Writemessagestothecallbackobject
VERBOSE
Prependsprogramname,timestamp,hostname,processID,andmessageprioritytoeachmessage
VERBOSE_LITE
Prependstimestampandmessageprioritytoeachmessage
SILENT
Donotprintmessagesatall
SYSLOG
Writemessagestothesystem'seventlog
CUSTOM
Writemessagestotheuser-providedbackend
●第三个参数loggerkey是针对第二个参数为LOGGER(C/S结构)时需要设置的,表示客户端程序端口值,比如:
ACE_DEFAULT_LOGGER_KEY。
一旦调用了该方法,其后的输出将重新定位到第二个参数所指定的sink上。
set_flags、clr_flags的定义为:
//Enablethebitsinthelogger'soptionsflags.
voidset_flags(unsignedlongf);
//Disablethebitsinthelogger'soptionsflags.
voidclr_flags(unsignedlongf);
参数含义同open函数的第二个参数options_flags,其中set_flags调用可在现有输出目标基础上增加新的输出目标(叠加),clr_flags则从中删除指定的输出目标。
3.2例2-输出重定向的综合示例
#include"ace/Log_Msg.h"
#include"ace/streams.h"
intACE_TMAIN(int,ACE_TCHAR*argv[])
{
//Outputtodefaultdestination(stderr)
ACE_LOG_MSG->open(argv[0]);
ACE_TRACE(ACE_TEXT("main"));
ACE_OSTREAM_TYPE*output=
newstd:
:
ofstream("ostream.output.test");
ACE_DEBUG((LM_DEBUG,ACE_TEXT("%IThiswillgotoSTDERR\n")));
ACE_LOG_MSG->open
(argv[0],ACE_Log_Msg:
:
SYSLOG,ACE_TEXT("syslogTest"));
ACE_LOG_MSG->set_flags(ACE_Log_Msg:
:
STDERR);
ACE_DEBUG
((LM_DEBUG,ACE_TEXT("%IThisgoestoSTDERR&syslog\n")));
ACE_LOG_MSG->msg_ostream(output,0);
ACE_LOG_MSG->set_flags(ACE_Log_Msg:
:
OSTREAM);
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("%IThiswillgotoSTDERR,")
ACE_TEXT("syslog&anostream\n")));
ACE_LOG_MSG->clr_flags(ACE_Log_Msg:
:
OSTREAM);
deleteoutput;
return0;
}
4使用LoggingStrategy进行运行时配置
ACE通过ACE_Logging_Strategy类实现运行时配置,涉及到ACEServiceConfigurator机
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 日志 系统 ACE 服务