Using the Logcxx Library and boost library to build the formatted output of the system log

Source: Internet
Author: User
Tags system log

Author: Zhu Jinchan
Source: http://blog.csdn.net/clever101/


As a powerful C + + log system, LOGCXX Library has been used more and more in the industry. However, the Logcxx library lacks the ability to format log output, but it cannot but say that it is a relatively large flaw. Of course you can say with the help of the format of the string class, and then plug it into the Logcxx library interface functions, such as the standard C library snprintf function, STL Std::ostringstream and MFC CString format interface. The disadvantage of snprintf is that you have to define a large buffer (not to worry about the log information), and it is not type-safe, std::ostringstream use security, but first define the Ostringstream object to construct the formatted string CString one is MFC program-specific, two it is not type-safe, three if you plug it into the Logcxx library interface, implied a CString to char* conversion operation. The approach I'm going to introduce is simpler and more secure than the one above, which is using the Boost library's Boost::format and Boost::lexical_cast.


Logcxx Library use method Here I do not repeat, is basically set up a configuration file, the definition of output mode, grade, etc., you can refer to this article: Log4cxx usage. Jin Qing proposed Boost::format and Logcxx library with the use of (see reference 2), I tried it very easy. But I find that logcxx and Boost::lexical_cast combine equally well. Here is the test code:


#include <string> using std::string; #include "boost/format.hpp" #include "boost/lexical_cast.hpp" #include "log4cxx/logger.h" #include "log4cxx/ Propertyconfigurator.h "//Get Log configuration information, define log class variable Log4cxx::P ropertyconfigurator::configure (_t (" Paint.config ")); Log4cxx::loggerptr Rootlogger = Log4cxx::logger::getlogger (_t ("Paint")); Log4cxx_debug (rootlogger,_t ("System ready to initialize/n")); Output the address of the variable i and j to log int i = 0; int j = 0; Log4cxx_debug (Rootlogger,boost::format ("I, J, respectively,%1% and%2%")% (&i)% (&j)); Log4cxx_debug (rootlogger,_t ("I, J address is") +boost::lexical_cast<string> (&i) +_t ("and") +boost::lexical_cast <string> (&j));


Compilation environment: Win XP + Sp3,vs C + + SP1


Reference documents:


1. Log4cxx Usage Detailed


2. Log4cxx with Boost::format, author Jin Qing.










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.