C++ 用于高性能应用程序的多线程日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3921944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:04:04  来源:igfitidea点击:

multithreaded logging for high performance application

c++multithreadinglogging

提问by Tony The Lion

I have an application (server application) that needs an extensive amount of logging implemented and shouldn't be too affected performance wise by enabling logging.

我有一个应用程序(服务器应用程序)需要实现大量的日志记录,并且不应该通过启用日志记录来影响性能。

The application has a thread pool of worker threads performing the work. Originally I was going to just log on these thread pool threads, but then I'd need to lock practically the whole thread and so there goes my 'multithreaded' app.

应用程序有一个执行工作的工作线程的线程池。最初我只想登录这些线程池线程,但后来我几乎需要锁定整个线程,因此我的“多线程”应用程序就出现了。

I have been looking at better ways to log from multiple threads and I've found using a Queue or ring buffer could be an idea.

我一直在寻找从多个线程进行日志记录的更好方法,我发现使用队列或环形缓冲区可能是一个想法。

Can anybody suggest (perhaps from experience) any good ways to implement effective logging (to a file mostly) for a multithreaded application that also should stay somewhat performant?

任何人都可以建议(也许是根据经验)为多线程应用程序实现有效日志记录(主要是文件)的任何好方法,该应用程序也应该保持一定的性能?

I would like to use the Boost Logging Library.

我想使用 Boost Logging 库。

采纳答案by m_pGladiator

Pantheiosis the fastest logging library for C++ out there, as far as I know. I recommend using it instead of Boost Logging. With Pantheios you simply log to the file, and you don't care from which thread. You can put the thread name in the logline prefix if you want and it does everything for you.

据我所知,Pantheios是最快的 C++ 日志库。我建议使用它而不是 Boost Logging。使用 Pantheios,您只需登录到文件,而不必关心来自哪个线程。如果需要,您可以将线程名称放在日志行前缀中,它会为您完成所有工作。

回答by dutt

Personally I would look into Pantheios, gave it a glance and it seems interesting, going to include that in a future project of mine.

就我个人而言,我会研究 Pantheios,瞥了一眼,这似乎很有趣,并将其包含在我未来的项目中。

If you really want to use boost logging I would use a synchronized queue that handles all the locking internally so your workers doesn't have to worry about that.

如果你真的想使用 boost 日志,我会使用一个同步队列来处理内部所有的锁定,这样你的工作人员就不必担心了。

pseudocode:

伪代码:

 class SynchedQueue {
      void write(text) { lock() logfile.write(text) unlock() }

Or if you really want to make it fast, use an internal queue and transfer from the public queue in batches of X lines at a time. The public queue doesn't have to wait for file i/o that might take a relativily long time and the private queue only gets lines seldomly. Although it will probably still be slower than Pantheios.

或者如果你真的想快点,使用一个内部队列,一次从公共队列分批 X 行转移。公共队列不必等待可能需要相对较长时间的文件 i/o,而私有队列很少获得行。虽然它可能仍然比 Pantheios 慢。

回答by Ralf

I read an interesting article using Active Objects for mt logging on ddj recently:

我最近阅读了一篇使用 Active Objects 进行 ddj 日志记录的有趣文章:

http://www.drdobbs.com/high-performance-computing/227500074

http://www.drdobbs.com/high-performance-computing/227500074

An older article also discussed some design considerations for thread-safe logging:

一篇较早的文章还讨论了线程安全日志记录的一些设计注意事项:

http://www.drdobbs.com/cpp/201804215

http://www.drdobbs.com/cpp/201804215

回答by DumbCoder

Apache log4cxx. I have seen quite a lot of places where it is being used extensively. And most of them were trading applications, multi threaded and low latency applications. log4cxx isn't inferior to any logging library and it is available for C++ and Java(Apache log4j), these 2 I have used.

Apache log4cxx。我已经看到很多地方广泛使用它。其中大部分是交易应用程序、多线程和低延迟应用程序。log4cxx 并不逊色于任何日志库,它可用于 C++ 和 Java(Apache log4j),这两个我用过。

回答by DumbCoder

I am using and really liking Petru Marginean's logging system. It is really light fast and neat. It took me a little while to get my head round it so I could adapt it to my liking, but once you understand it, it's a beauty and as the whole library is only really 1 header file of 250 lines, its not much to get through.

我正在使用并且非常喜欢Petru Marginean 的日志系统。它真的很轻,又快又整洁。我花了一点时间来理解它,所以我可以根据自己的喜好调整它,但是一旦你理解了它,它就是一种美,因为整个库实际上只有 1 个 250 行的头文件,所以没有多少通过。

The slowest part of it is it uses an object with an ostringstream, but I doubt you will find anything faster. At least not without being more C than C++.

它最慢的部分是它使用一个带有 ostringstream 的对象,但我怀疑你会发现更快的东西。至少不是没有比 C++ 更多的 C。

In his second article and versionhe shows how he made it thread safe.

在他的第二篇文章和版本中,他展示了如何使其线程安全。

If you cant find the source code, let me know, I have it. I remember I had to search for it a bit.

如果找不到源代码,请告诉我,我有。我记得我不得不搜索一下。

回答by wageoghe

I'm not familiar with the Boost Logging library, so I can't comment on that.

我不熟悉 Boost Logging 库,所以我无法对此发表评论。

NLogsupports logging from C/C++ and COM (as well as from managed .NET applications). Not sure if it has the performance characteristics you are after or not.

NLog支持来自 C/C++ 和 COM(以及来自托管 .NET 应用程序)的日志记录。不确定它是否具有您所追求的性能特征。

You might also consider ETW. It is a high speed logging system implemented in the Windows kernel. I have not used it but Microsoft does seem to be pushing it a little more and making it a little easier to use with each release. They are certainly making it easier to use from managed code. As it stands right now, it looks to me to be somewhat complicated to get set up and then to get the output. I have never gotten to the point that I have seriously considered implementing ETW, so I can't really comment on how easy or difficult it really is.

您也可以考虑ETW。它是一个在 Windows 内核中实现的高速日志系统。我没有使用过它,但 Microsoft 似乎确实在进一步推动它,并使其在每个版本中更易于使用。它们无疑让托管代码更易于使用。就目前而言,在我看来,设置和获取输出有些复杂。我从来没有认真考虑过实施 ETW,所以我无法真正评论它到底有多容易或有多困难。