The row number cannot be logged after the log4cplus is encapsulated.

Source: Internet
Author: User
Copyright Notice

Respect Original Works. Reprinted, please maintain the integrity of the article, and in the form of a hyperlink to indicate the original author "tingsking18" and the main site address, so that other friends can ask and correct.

 

After encapsulating log4cplus, the row number cannot be recorded. When using log4cplus, % L is configured in the configuration file, which is the file and row number where the log information is recorded. We can directly record log4cplus_error (logger, logmsg. In this way, the log information contains the file and row number of our log information. But sometimes we may need to encapsulate log4cplus. The encapsulated code may look like the following: void _ logerror (const char * FMT ,...) {char logmsg [4096]; intstrlen; memset (logmsg, 0x00, sizeof (logmsg); va_listap; va_start (AP, FMT); strlen = vsprintf (logmsg, FMT, AP); va_end (AP); log4cplus_error (logger, logmsg); return;} but in this case, the file name and row number of the log information record are the file where _ logerror is located and log4cplus_error (logger, logmsg); the row number. In this way, we cannot achieve what we want. 2010-01-06 00:52:30 [test. CPP: 47]-> ======================================== ======================================== 00:52:30 [test. CPP: 47]-> XXXXX log 2010-01-06 00:52:30 [test. CPP: 47]-> ======================================== ======================================== 00:52:30 [test. CPP: 47]-> when the program starts, there are two global variables: char * filename; int line; then define a macro: # define logerror filename =__ file __, line =__ line _ _, _ Logerror: Change log4cplus_error (logger, logmsg) in the _ logerror function to logger. Log (error_log_level, logmsg, filename, line. 2010-01-06 00:52:43 [test. CPP: 60]-> ========================================== ======================================== 00:52:43 [test. CPP: 61]-> XXXXX log 2010-01-06 00:52:43 [test. CPP: 62]-> ========================================== ======================================== 00:52:43 [test. CPP: 63]-> the program starts to start, but the above situation only applies to a single thread. If you want to use a multi-threaded environment, you can implement filename and line as functions, just like the implementation of errno in multi-thread scenarios, where thread specific data is used.

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.