LogFileObject::Write
LogMessage::Init 三个函数
具体怎么修改看你需要的日志伏埋格式来 三个函数里面与
<<"["
<<1900+tm_time.tm_year <<'-'
<<setw(2) <<1+tm_time.tm_mon <<'-'
<<setw(2) <<tm_time.tm_mday
<<' '
<<setw(2) <<tm_time.tm_hour <<':'
<<setw(2) <<tm_time.tm_min <<':'
<<setw(2) <<缺饥蚂 tm_time.tm_sec
<<","
<<static_cast<unsigned int>肢团(GetTID()) <<setfill('0')
<<"] "
类似的就是日志文件的结构
Google glog是一个应用层的库. 它提供基于C++风格的流和多种宏接口.例如:#include <glog/logging.h>
int main(int argc, char* argv[]) {
// Initialize Google's logging library.
google::InitGoogleLogging(argv[0])
// ...
LOG(INFO) <<"Found " <<num_cookies <<" cookies"
}
Google glog定义了一系列的宏处理普通的日志工作. 你可以分级记录, control loggingbehavior from the command line, 按条件记录, abort theprogram when expected conditions are not met, introduce your ownverbose logging levels, 及其它. 文本只是简单的介绍了常用的glog功能而不是所有的细节闷拆.没介绍到的,自己读代码去吧.
严重等级
严重等级包括(按严重程度排序): INFO, WARNING,ERROR,和FATAL.记录一个FATAL级的错误后会自动终止程序执行(其实就是_asm int 3).一个严重等级高的记录不但记录在它自己的文件,也记录在比它低等的文件里.例如, 一个FATAL级的记录将会记在FATAL, ERROR,WARNING, 和INFO等级的日志里.
在debug状态下FATAL级记录也可以用DFATAL(当没定义NDEBUG宏的时候),发给客户的程序最好不要用FATAL小心客户对你使用_asm int 3,这时ERROR是个不错的选择.
glog默认把日志文件命名为"/tmp/...log...."(例如, "/tmp/hello_world.example.com.hamaji.log.INFO.20080709-222411.10474").glog默认把ERROR 和 FATAL 级的记录发送一份到stderr.
设置标志
标志可以改变glog的输出行为.如果你安装了Googlegflags library, configure 脚本 (具体请参考它的INSTALL文件)会自动检测并使用标志,这时你可以通过命令行使用标志.例如, 若你想发送 --logtostderr 标志,像下面这样:
./your_application --logtostderr=1
如果你没装Google gflags library,你就只能用带GLOG_的环境变量实现,例如.
GLOG_logtostderr=1 ./your_application(貌似在Windows里不能这样用吧?)
常用的标志有:
logtostderr (bool, default=false)
写日志到stderr而不是日志文件.
Note: you can set binary flags to true by specifying1, true, or yes (caseinsensitive).Also, you can set binary flags to false by specifying0, false, or no (again, caseinsensitive).
stderrthreshold (int, default=2, whichis ERROR)
将某级及以上级别的蚂前枣记录同时发送到stderr和日志文件. 严重等级INFO, WARNING, ERROR, FATAL 分别对应 0, 1, 2, 3.
minloglevel (int, default=0, whichis INFO)
只记录某级及以上级别的记录.
log_dir (string, default="")
指定日志悔含保存的目录.
v (int, default=0)
Show all VLOG(m) messages for m less orequal the value of this flag. Overridable by --vmodule.See the section about verbose logging for moredetail.
vmodule (string, default="")
Per-module verbose level. The argument has to contain acomma-separated list of =.is a glob pattern (e.g., gfs* for all modules whose namestarts with "gfs"), matched against the filename base(that is, name ignoring .cc/.h./-inl.h). overrides any value given by --v.See also the section about verbose logging.
还有一些其它的标志,自己去logging.cc里面搜索"DEFINE_",爷就不多说了.
你也可以在程序里修改FLAGS_* 开头的全局变量. 通常设置FLAGS_*开头的变量会立即生效,和日志文件名或路径相关的变量例外.例如FLAGS_log_dir就不能放在google::InitGoogleLogging后面.例子代码:
LOG(INFO) <<"file"
// Most flags work immediately after updating values.
FLAGS_logtostderr = 1
LOG(INFO) <<"stderr"
FLAGS_logtostderr = 0
// This won't change the log destination. If you want to set this
// value, you should do this before google::InitGoogleLogging .
FLAGS_log_dir = "/some/log/directory"
LOG(INFO) <<"the same file"条件/ Occasional Logging下面的宏用于根据条件写日志: LOG_IF(INFO, num_cookies >10) <<"Got lots of cookies"只有当num_cookies >10的时候才会记录"Got lots of cookies"如果有的代码执行非常频繁,你肯定不会希望每次都写入日志.这时,你可以使用下面的方法: LOG_EVERY_N(INFO, 10) < Got the googleCOUNTER th cookiepre>上面的代码只有当第1次,第11次,第21次....执行时才写入日志.Note that the specialgoogle::COUNTER value is used to identify which repetition ishappening.条件和计次可以混合使用,例如: LOG_IF_EVERY_N(INFO, (size >1024), 10) <<"Got the " <<google::COUNTER
<<"th big cookie"除了间隔N次输出,还可以只输出前M次,例如: LOG_FIRST_N(INFO, 20) < Got the googleCOUNTER th cookiepre>只输出前20次.
Debug mode支持"debug mode"的宏只在调试模式有效,其他模式不生效.上栗子: DLOG(INFO) <<"Found cookies"
DLOG_IF(INFO, num_cookies >10) <<"Got lots of cookies"
DLOG_EVERY_N(INFO, 10) <<"Got the " <<google::COUNTER <<"th cookie"CHECK宏经常检查是否会出错而不是等着程序执行出错是个不错的习惯.CHECK宏就是干这个滴,它的功能和assert一样,条件不符合时终止程序.与assert不同的是它*不*受NDEBUG的限制,无论是不是debug编译它都执行.所以下面的fp-<Write(x)会被执行: CHECK(fp->Write(x) == 4) <<"Write failed!"有各种各样的宏用于CHECK相等/不相等,包括: CHECK_EQ,CHECK_NE, CHECK_LE, CHECK_LT,CHECK_GE,和CHECK_GT.它们会记录一个FATAL级的记录到日志,牛X的是他们还会把对比的两个值也一起记录.(那两个值必须定义了 operator<ostreamcode>).例如: CHECK_NE(1, 2) <<": The world must be ending!"We are very careful to ensure that each argument is evaluated exactlyonce, and that anything which is legal to pass as a function argument islegal here. In particular, the arguments may be temporary expressionswhich will end up being destroyed at the end of the apparent statement,for example: CHECK_EQ(string("abc")[1], 'b')The compiler reports an error if one of the arguments is apointer and the other is NULL. To work around this, simply static_castNULL to the type of the desired pointer. CHECK_EQ(some_ptr, static_cast(NULL))还有个好办法:用CHECK_NOTNULL宏: CHECK_NOTNULL(some_ptr) some_ptr-<DoSomething()这宏也常用于构造函数中. struct S {
S(Something* ptr) : ptr_(CHECK_NOTNULL(ptr)) {}
Something* ptr_
}这宏的缺点是不能跟C++流一起用.这时就得用CHECK_EQ了.如果你要对比一个C字符串(char *)可以用:CHECK_STREQ, CHECK_STRNE,CHECK_STRCASEEQ,和CHECK_STRCASENE.带CASE的区分大小写. 这个宏参数可以用NULL指针.NULL与non-NULL比不等.两个NULL是相等的.Note that both arguments may be temporary strings which aredestructed at the end of the current "full expression"(e.g., CHECK_STREQ(Foo().c_str(), Bar().c_str()) whereFoo and Bar return C++'sstd::string).CHECK_DOUBLE_EQ 用于对比浮点数,会有小小的误差.CHECK_NEAR 可以接受一个浮点数作为误差.详细日志用VLOG宏,你还可以定义自己的严重等级
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)