Poco是否可以缓冲日志条目



我正在使用FileChannel和AsyncChannel与Poco异步记录市场数据,这将每秒创建大量的日志条目。似乎Poco将每条消息单独写入文件,并且不缓冲。我认为这给我的HDD/文件系统带来了相当大的压力,我认为应用程序崩溃是相关的。

有没有办法让Poco只以1Mb的增量将日志保存到磁盘,并在记录器关闭时将缓冲区中的任何剩余内容写入文件?

另外,这是否有可能创建大量的线程?根据我所读到的内容,AsyncChannel只是将消息放入队列中,所以我猜只创建了1个额外的线程?

以下基本上是我正在使用的代码:

#include "Poco/Message.h"
#include "Poco/FormattingChannel.h"
#include "Poco/PatternFormatter.h"
#include "Poco/Logger.h"
#include "Poco/FileChannel.h"
#include "Poco/AutoPtr.h"
#include "Poco/AsyncChannel.h"
class APocoClass
{
private:
Poco::AutoPtr<Poco::FileChannel> pFileChannel;
Poco::AutoPtr<Poco::PatternFormatter> pPF;
Poco::AutoPtr<Poco::FormattingChannel> pFormattingChannel;
Poco::AutoPtr<Poco::AsyncChannel> pFileChannelAsync;
Poco::Logger & _pocoLogger;
public:
APocoClass() :
pFileChannel(new Poco::FileChannel()),
pPF(new Poco::PatternFormatter("%Y%m%d %H:%M:%S.%F: %t")),
pFormattingChannel(new Poco::FormattingChannel(pPF, pFileChannel)),
pFileChannelAsync(new Poco::AsyncChannel(pFormattingChannel)),
_pocoLogger(Poco::Logger::create("PocoLogger", pFileChannelAsync, Poco::Message::PRIO_INFORMATION))
{
pFileChannelAsync->setProperty("priority", "lowest");
pFileChannel->setProperty("path", "MsgStorage/poco.log");
pFileChannel->setProperty("rotation", "daily");
pFileChannel->setProperty("times", "utc");
pFileChannel->setProperty("archive", "timestamp");
}
~APocoClass() {
_pocoLogger.shutdown();
_pocoLogger.close();
pFileChannelAsync = nullptr;
pFileChannel = nullptr;
}
//following is called every time we have a new market data message to log
void MessageReceived(const string & message) {
Poco::Message m("PocoLogger", message, Poco::Message::Priority::PRIO_INFORMATION);
_pocoLogger.log(m);
}
}

Poco是否只以1Mb的增量将日志保存到磁盘,并在记录器关闭时将缓冲区中的任何剩余内容写入文件?

通过FileChannel您没有如此精确的控制级别,但您有可用的刷新属性(默认true),它决定是否在每个日志条目上刷新缓冲区。将其设置为false,看看情况是否有所改善。

如果这不能满足您的性能要求,那么您可以选择编写自己的包装器,请参阅LogStream以获取示例-显然,您需要为LogStreamBuf::writeToDevice()实现自己的逻辑。(若库允许您简单地传入自己的streambuf,那个会简单得多,但不幸的是它并没有。)

另外,这是否有可能创建大量线程?

否,AsyncChannel将在一个线程中启动自己,并在该线程中处理所有通知(即日志消息)。

最新更新