标签:
voreen的日志文件系统分为日志类和日志管理类,log和logManage类;
log是整个不同日志类的基类,在此类的基础之上,根据需要定义了文本log类(TextLog)、HTML log类(HtmlLog)、以及console log类(consoleLog);
LogManager类负责统一管理整个日志类,并通过该类将消息分发给所有的日志类;
具体而言:
voreen定义了日志的级别
/** * Specifies the severity of the log event. * Debug messages are not logged in release builds! */ enum LogLevel { Debug, Info, Warning, Error, Fatal };
voreen定义了每一log类所能处理的日志信息过滤器
/** * Holds the information for filtering messages. */ struct TGT_API LogFilter { std::string cat_; bool children_; LogLevel level_; };
基类log提供了两个保护函数分别为:
virtual bool testFilter(const std::string &cat, LogLevel level); virtual void logFiltered(const std::string &cat, LogLevel level, const std::string &msg, const std::string &extendedInfo = "") = 0;
当有信息需要log时,testFilter根据该log对应的filter的信息,判断信息是否可以被处理,然后调用logFiltered函数向日志中添加信息
log类整体关系如下:
logManager类用于管理所有的日志类,主要负责添加删除log类,处理所有日志信息等;
应用程序调用logManager中的log函数,该函数将所有的信息统一分发给各个log类,然后由每个log类的log函数根据自己的实际情况处理(即基类的两个保护函数)对应的日志信息。
logManager利用voreen定义的宏,来使用singleton模式,保证全局只有一个logManger实例。该singleton宏定义如下:
#define SINGLETON_CLASS_HEADER(T) public:static void init() {tgtAssert(!singletonClass_, "singletonClass_ has already been initialized." );singletonClass_ = new T;tgtAssert(singletonClass_, "singletonClass_ has not been created.");}static void deinit() {tgtAssert(singletonClass_, "singletonClass_ has already been deinitialized." );delete singletonClass_;singletonClass_ = 0;}static T* getPtr() {tgtAssert(singletonClass_, "singletonClass_ has not been intitialized." );return singletonClass_;}static T& getRef() {tgtAssert(singletonClass_ , "singletonClass_ has not been intitialized." );return *singletonClass_;}static bool isInited() {return (singletonClass_ != 0);}protected:static T* singletonClass_; #define SINGLETON_CLASS_SOURCE(T) T* T::singletonClass_ = 0;
宏参数T为logManager类;
并通过宏获得该全局实例:
#define LogMgr tgt::LogManager::getRef()
voreen 通过do.....while(0)的应用来定义不同的宏,用于处理不用级别的信息,该宏定义如下所示:
#define LDEBUG(msg) do { std::ostringstream _tmp, _tmp2; _tmp2 << __FUNCTION__ << " File: " << __FILE__ << "@" << __LINE__; _tmp << msg; LogMgr.log(loggerCat_, tgt::Debug, _tmp.str(), _tmp2.str()); } while (0)
其中do......while(0)的妙用在下面中有讲到http://www.cnblogs.com/flying_bat/archive/2008/01/18/1044693.html
其中注意到loggerCat_变量的定义如下:
static const std::string loggerCat_;
对于每一个需要使用日志功能的模块都需要定义该静态变量;从而保证在通过宏LDEBUG调用时,可以根据cat类别进行日志信息的输出;
标签:
原文地址:http://www.cnblogs.com/shouce/p/5065502.html