魔兽世界服务器Trinitycore分析三:日志模块

一:日志接口

TrinityCore的日志有六个级别:TraceDebugInfoWarnErrorFatal

它们分别对应以下这六个接口,(filterType__也是在配置文件中指定的)

#define TC_LOG_TRACE(filterType__, ...)
#define TC_LOG_DEBUG(filterType__, ...)
#define TC_LOG_INFO(filterType__, ...)
#define TC_LOG_WARN(filterType__, ...)
#define TC_LOG_ERROR(filterType__, ...)
#define TC_LOG_FATAL(filterType__, ...)

二:日志类型与Appender

说明:这个在配置文件的注释中说得很详细,直接帖两段吧

Appender配置:

#  Appender config values: Given a appender "name"
#    Appender.name
#        Description: Defines 'where to log'
#        Format:      Type,LogLevel,Flags,optional1,optional2,optional3
#
#                     Type
#                         0 - (None)
#                         1 - (Console)
#                         2 - (File)
#                         3 - (DB)
#
#                     LogLevel
#                         0 - (Disabled)
#                         1 - (Trace)
#                         2 - (Debug)
#                         3 - (Info)
#                         4 - (Warn)
#                         5 - (Error)
#                         6 - (Fatal)
#
#                     Flags:
#                         0 - None
#                         1 - Prefix Timestamp to the text
#                         2 - Prefix Log Level to the text
#                         4 - Prefix Log Filter type to the text
#                         8 - Append timestamp to the log file name. Format: YYYY-MM-DD_HH-MM-SS (Only used with Type = 2)
#                        16 - Make a backup of existing file before overwrite (Only used with Mode = w)
#
#                     Colors (read as optional1 if Type = Console)
#                         Format: "fatal error warn info debug trace"
#                         0 - BLACK
#                         1 - RED
#                         2 - GREEN
#                         3 - BROWN
#                         4 - BLUE
#                         5 - MAGENTA
#                         6 - CYAN
#                         7 - GREY
#                         8 - YELLOW
#                         9 - LRED
#                        10 - LGREEN
#                        11 - LBLUE
#                        12 - LMAGENTA
#                        13 - LCYAN
#                        14 - WHITE
#
#                     File: Name of the file (read as optional1 if Type = File)
#                         Allows to use one "%s" to create dynamic files
#
#                     Mode: Mode to open the file (read as optional2 if Type = File)
#                          a - (Append)
#                          w - (Overwrite)
#
#                     MaxFileSize: Maximum file size of the log file before creating a new log file

日志配置:

#  Appender config values: Given a appender "name"
#    Appender.name
#        Description: Defines 'where to log'
#        Format:      Type,LogLevel,Flags,optional1,optional2,optional3
#
#                     Type
#                         0 - (None)
#                         1 - (Console)
#                         2 - (File)
#                         3 - (DB)
#
#                     LogLevel
#                         0 - (Disabled)
#                         1 - (Trace)
#                         2 - (Debug)
#                         3 - (Info)
#                         4 - (Warn)
#                         5 - (Error)
#                         6 - (Fatal)
#
#                     Flags:
#                         0 - None
#                         1 - Prefix Timestamp to the text
#                         2 - Prefix Log Level to the text
#                         4 - Prefix Log Filter type to the text
#                         8 - Append timestamp to the log file name. Format: YYYY-MM-DD_HH-MM-SS (Only used with Type = 2)
#                        16 - Make a backup of existing file before overwrite (Only used with Mode = w)
#
#                     Colors (read as optional1 if Type = Console)
#                         Format: "fatal error warn info debug trace"
#                         0 - BLACK
#                         1 - RED
#                         2 - GREEN
#                         3 - BROWN
#                         4 - BLUE
#                         5 - MAGENTA
#                         6 - CYAN
#                         7 - GREY
#                         8 - YELLOW
#                         9 - LRED
#                        10 - LGREEN
#                        11 - LBLUE
#                        12 - LMAGENTA
#                        13 - LCYAN
#                        14 - WHITE
#
#                     File: Name of the file (read as optional1 if Type = File)
#                         Allows to use one "%s" to create dynamic files
#
#                     Mode: Mode to open the file (read as optional2 if Type = File)
#                          a - (Append)
#                          w - (Overwrite)
#
#                     MaxFileSize: Maximum file size of the log file before creating a new log file

三:配置例子:

Appender.Console=1,3,0
Appender.Server=2,2,0,Server.log,w
Logger.root=2,Console Server

以上这三句,定义了两个Appender:Console, Server,一个log type:root

Appender.Console说明将日志输出到终端上,最低输出级别是Info,输出字体颜色是黑色(因终端的不同而不同)

Appender.Server说明将日志输出到文件,最低输出级别是Debug,输出文件是(Server.log),以重写(非添加)的方式写入

Logger.root最低输出级别是Info,使用ConsoleServer的配置


四,底层实现

这里简单地说一下,就不太深入了

 TC_LOG_INFO("qch", "Hello, world"); 为例子

这句实现上是调用了

if(Log::instance()::ShouldLog("qch", LOG_LEVEL_INFO) )
        Log::instance()::outMessage ("qch", LOG_LEVEL_INFO, "Hello,world");

ShouldLog比较好理解,直接将配置文件中的最低日志输出级别和LOG_LEVEL_INFO一比较就搞定。

outMessage就比较麻烦了,层层调用,有兴趣的可以去看看,这里就不多作解释了。

 

如果把AppenderType设为3DB),那每写一条日志,就是在auth库的logs表中插入一条记录:

INSERT INTO logs (time, realm, type, level, string) VALUES (1409544332, 0, 'qch', 3,'Hello, world\n')

展开阅读全文

没有更多推荐了,返回首页