开发者

How to create a Log in C++?

I want to print lots of debug log prints in C++. Is there a built-in library that will allow me to turn on/off these prints without commenting them out as I do today (i.e., let the user use a -print_log/-debug flag)?

EDIT: third party loggers will be welcomed.

Please include a short exa开发者_开发问答mple of how to do this.


google glog is a good example

http://google-glog.googlecode.com/svn/trunk/doc/glog.html


You can use syslog


If you're using Boost, consider using the proposed Boost.Log library. It's not yet part of the official Boost release, but I've found it to be quite good. Manually integrating it into the Boost build process was a little challenging though.


One of approaches: write all messages to std::wclog. It's similar to std::wcout and can be redirected in the same way.

Not sure about its multithread safety, or other features, but it seems to be the simplest way - it already exists.


log4cpp works really well.

There is a BOOST log although it is not part of the official set of libraries.


There is no built-in library. There are third-party libraries, or you could implement your own without too much effort. One decent way to do this is to have a logging function that accepts a FILE*, that can be stdout or /dev/null (or equivalent), or a file, etc.

Since you mention that it's C++, you can also do it with streams, accepting an ostream& that can be cout, cerr, a file, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜