Is there a “built in way” to log everything with g

2019-04-30 07:13发布

I'm interested in using some logging to help me detect potential problems in my code, and to detect where my program crashed. My problem is that lib that I used (google glog) doesn't log the stuff if they occur just before the program crash. So I tried to do something like this (this is template for 3 arguments):

mutex logMtx;
template<class T, class U, class V>
void mutexLOG_INFO(T t, U u, V v)
{
    stringstream ss;
    ss<<t;
    ss<<u;
    ss<<v;
    LOG(INFO)<<ss.str();
    mutex::scoped_lock sl(logMtx);
    google::FlushLogFiles(0);
}

It works AFAIK (from my testing), but as you can see it is not very nice, because I need to do my own function for every level (INFO,WARNING..) and for every number of parameters. Also I hate reinventing the wheel.

So is there a way to tell glog to flush every time after LOG?

P.S. I know that this is g-log, not g-db (oops, name taken :) but I prefer to have the option to print nicely my STL data, I use gdb only to detect where things died.

EDIT: SO proves again that it is a great source of info:

#include <glog/logging.h>
#include <**gflags**/gflags.h>
...
FLAGS_logbuflevel=-1;

标签: c++ logging
1条回答
萌系小妹纸
2楼-- · 2019-04-30 07:40

I use gflags with glog and if you run --help the option logbuflevel might be what you are looking for.

    -logbuflevel (Buffer log messages logged at this level or lower (-1 means
  don't buffer; 0 means buffer INFO only; ...)) type: int32 default: 0
查看更多
登录 后发表回答