Installation and use of Log4cplus under Linux __linux

Source: Internet
Author: User

In the process of doing the server program I think the most useful debugging means should be the log system, good log can be intuitive fast positioning program problems, such as system crashes, deadlocks, errors thrown, and so on. How to choose a good log library is critical for programmers. The design of crude and rough log system, may lead to the system's performance with the increase in the log and the linear reduction, but also can lead to thread-safety issues, I used the project Log4cplus this log library, Log4cplus is a C + + written open source log system, the function is very comprehensive, Used in their own development projects will be more professional. Log4cplus is a C + + written open source of the log system, the predecessor of the Java-written log4j system. is protected by Apache Software license. The author is Tad E. Smith. Log4cplus has the characteristics of thread safety, flexibility and granularity control, by dividing the information into priority so that it can be oriented to the whole life cycle of program debugging, running, testing, and maintenance. You can choose to output the information to the screen, file, NT event log, or even a remote server ; Periodically back up the log by specifying a policy, and so on.

Log4cplus installation under Linux:

1. Configuration./configure--prefix=/usr

2. Compile Make

3. Install Make Intsall

--prefix is the designated installation directory, Linux will default to look for header files from/usr/include, and look for library files in/usr/lib directory.

Log4cplus ported to arm:

First you need to install and export a cross compilation environment, and then view your cross compilation environment default library installation directory, in the terminal Input command: Echo ' main () {} ' |arm-none-linux-gnueabi-gcc-e-v–

View the default installation directory.

1. Configure sudo./configure--prefix=yourinstall Path--host=arm-linux

2. Modify Makefile to convert all compilation-related compile-chain tools in Makefile to cross-compile tools, including: AR, as, LD, GCC, g++,rannlib

3. Compile make

4. Install make install

Log4cplus basic steps to use:

1. Implements a Appender object that encapsulates the output media

2. Instantiate a layout object that encapsulates the output format

3. Bind the Layout object (attach) to Appender

4. Access to Logger objects

5. Bind Append to Logger

6. Set the log level of the logger

7. Print Log

Example program:

#include <iostream> #include <log4cplus/logger.h> #include <log4cplus/loggingmacros.h> #include <log4cplus/configurator.h> #include <iomanip> #include <log4cplus/logger.h> #include <log4cplus /fileappender.h> #include <log4cplus/consoleappender.h> #include <log4cplus/layout.h> #include <
time.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std;
using namespace Log4cplus;
using namespace Std;
    int main () {sharedappenderptr appendptr (new Rollingfileappender ("Log_text.txt", 1*1024*1024,1,true));
    Logger Logger = logger::getinstance ("test");
    std::string pattern = "%p-%d{%y/%m/%d%h:%m:%s-%q}{threadid:%t}->%m\r\n";
    Std::auto_ptr<layout> Layout_ (new Patternlayout (pattern));
    Appendptr->setlayout (Layout_);
    Logger.addappender (APPENDPTR);
    Logger.setloglevel (All_log_level);
    Log4cplus_info (Logger, "INFO message"); Log4cplus_debug (Logger, "DEBUG messAge ");
    Log4cplus_trace (Logger, "TRACE message");
    Log4cplus_error (Logger, "ERROR message");
    Log4cplus_warn (Logger, "Warning message");
    Log4cplus_fatal (Logger, "FATAL message");
return 0; }

This is a simple example, more specific use of the way you can go to more detailed information: such as: http://wenku.baidu.com/view/51d96c1d964bcf84b9d57b9c.html Log4cplus Log Library is a bit obvious, Don't increase the burden of the system as the number of logs increases, and don't worry about other problems like line Cheng.

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.