我怎么能手动刷新提振日志? [英] How can I manually flush a boost log?

查看:201
本文介绍了我怎么能手动刷新提振日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Boost.Log打在升压1.54.0,看它是否是我的应用程序一个可行的选择。一般情况下,我没有与缓冲的问题,所以我不希望打开auto_flush或任何东西......但我注意到,我称之为叉之前所记录的消息()是重复的,我想知道,如果那是因为他们被缓冲,缓冲被复制过程映像时,被复制,然后两个进程最终会写自己的缓冲区拷贝到日志文件...

I'm playing with Boost.Log in boost 1.54.0 to see if it is a viable option for my application. In general, I don't have a problem with the buffering, so I'm not looking to turn on auto_flush or anything... but I noticed that messages that are logged before I call fork() are duplicated, and I'm wondering if it's because they are buffered, the buffer gets duplicated when the process image is copied, and then both processes eventually write their buffer copies to the log file...

所以基本上,我想只是做对日志手动清除,只有一次,我马上打电话之前叉(),以确保没有消息仍然坐在内存。换句话说,我在寻找到 fflush() .flush(),<$ C类似的东西$ C>&LT;&LT;冲洗等等,我可以提振日志使用。

So basically, I'd like to just do a manual flush on the log, one time only, immediately before I call fork() in order to be sure no messages are still sitting in memory. In other words, I'm looking for something akin to fflush(), .flush(), << flush, etc. that I can use on a boost log.

我也尝试使用&LT;&LT;冲洗的日志,但我仍然得到重复的邮件,所以我不是100%肯定它是否是冲洗和重复是由其他一些问题引起的,或者如果它在某种程度上悄悄地忽略&LT;&LT;冲洗 ...

I did try using << flush with the log, but I still get the duplicated messages, so I'm not 100% sure whether it's flushing and the duplicates are caused by some other problem, or if it somehow silently ignores the << flush...

编辑:

我环顾四周,发现升压日志未叉安全。所以,我要补充一点,我的的试图使用相同的日志在父母子女的过程。我有两个分叉的情景 - 在一个,父母立即终止,并子contineus(所以应该是安全的),而在另一方面,孩子应该开设自己单独的日志文件,所以应该是安全的还...但我倒是需要弄清楚如何的关闭的日志文件接收器,然后打开一个新的(在不同的文件)。我猜想关闭片也可能是一种强制刷新...?

I was looking around and found that boost log is not fork-safe. So I should add that I am not trying to use the same log in parent and child processes. I have two forking scenarios - in one, the parent terminates immediately and child contineus (so that should be safe), and in the other, the child should open its own separate log file, so that should be safe also... but I'd need to figure out how to close a log file sink and then open a new one (on a different file). I suppose closing the sink may also be a way to force a flush...?

推荐答案

Okies ......我不得不通过升压codeA位挖(但是不要太多),我发现这一点,这似乎工作:

Okies... I had to dig through the boost code a bit (but not too much), and I found this, which seems to work:

当你调用 add_file_log(strLogFilename)它返回一个 shared_ptr的&lt;信宿&GT; ,其中是下沉的类型(例如,的shared_ptr&LT; synchronous_sink&LT; text_file_backend&GT;&GT; )。如果改为创建下沉手动,那么你当然有它的指针,以及...看来汇和后端都有一个 .flush()方法。我不知道你怎么随便直接将后端的副本调用它的红晕,但对沉冲洗似乎简单地调用它的后端(S)冲洗,这样的作品。下面是我发现了什么工作对我来说一些示例code:

When you call add_file_log(strLogFilename) it returns a shared_ptr<sink> where sink is your type of sink (e.g., shared_ptr< synchronous_sink< text_file_backend > >). If you instead create your sink "manually" then of course you have a pointer to it as well... It seems the sinks and backend both have a .flush() method. I'm not sure offhand how you directly get a copy of the backend to call its flush, but the flush on the sink seems to simply call the flush on its backend(s), so that works. Here's some example code of what I found to work for me:

shared_ptr< synchronous_sink< text_file_backend > > pLogSink = add_file_log(strLogFilaname);
BOOST_LOG_TRIVIAL(debug) << "Boost log!";

// other work goes here

BOOST_LOG_TRIVIAL(debug) << "About to fork...";
if (pLogSink)
  pLogSink->flush();
pid_t pid = fork();

if (pid < 0)
  // handle error
else if (pid > 0)
  exit(EXIT_SUCCESS); // parent terminates
assert(0 == pid); // child continues

BOOST_LOG_TRIVIAL(debug) << "Fork succeeded!";

使用这种方法,我现在看到每条日志消息只有一次。当然,记住这个警告有关叉混合Boost.Log()...
http://boost-log.sourceforge.net/libs/log/doc/html/log/rationale/fork_support.html

在我的例子,它是安全的,只是因为父进程而不触及数在所有(后叉)分叉后立即退出。因此,没有日志的争用。

In my example, it's safe only because the parent process immediately exits after forking without touching the log at all (after the fork). Thus there isn't any contention for the log.

尽管有限制,我可以在一些情况下使用此看到:1)daemonizing的过程(这是我想在这里做的,其实),2)叉EXEC模式(其中确实的正常工作与Boost.Log,根据上述网址),或3)子进程立即关闭文件接收器,并打开指向不同的文件日志一个新的水槽(从父进程正在使用一) - 我的认为的这第三种情况应该是安全的。

Despite the limitations, I can see using this in a few cases: 1) daemonizing a process (which is what I'm trying to do here, actually), 2) fork-exec pattern (which does work fine with Boost.Log, according to the above URL), or 3) child process immediately closes the file sink and opens a new sink for the log that points to a different file (from the one the parent process is using) - I think this third case should be safe.

这篇关于我怎么能手动刷新提振日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆