Saturday, January 26, 2013

Build and Run Sample Code Using Log4Cpp from Source Code on Ubuntu

This Post will show How , to Build Log4cpp Source code and the Compile and run the Sample code as shown in this Simple Example Using Ubuntu . So Lets begin with Downloading the Log4cpp.

The download Source code is available on Sourceforge on this Download Link . So Download the Source and Extract it in your Home directory or wherever you want to extract  according to your choice . I have extracted the Log4cpp in my Home directory .

Now Follow the steps below.
  1.   $ cd log4cpp 
  2.  $ ./configure
  3.  $ make
  4.  $ sudo make check
  5.  $ sudo make install    
I think Up to this step there should not be any problem to built and install log4cpp source. 
Once the above steps are successfully executed then we are ready to Compile and run Sample Example . 
Make a C++ file called log4cpp.cpp . and copy and paste the Sample example as shown below .
// log4cpp.cpp

#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"

int main(int argc, char** argv) {
 log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
 appender1->setLayout(new log4cpp::BasicLayout());

 log4cpp::Appender *appender2 = new log4cpp::FileAppender("default", "program.log");
 appender2->setLayout(new log4cpp::BasicLayout());

 log4cpp::Category& root = log4cpp::Category::getRoot();

 log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));

 // use of functions for logging messages
 root.error("root error");"root info");
 sub1.error("sub1 error");
 sub1.warn("sub1 warn");

 // printf-style for logging variables
 root.warn("%d + %d == %s ?", 1, 1, "two");

 // use of streams for logging messages
 root << log4cpp::Priority::ERROR << "Streamed root error";
 root << log4cpp::Priority::INFO << "Streamed root info";
 sub1 << log4cpp::Priority::ERROR << "Streamed sub1 error";
 sub1 << log4cpp::Priority::WARN << "Streamed sub1 warn";

 // or this way:
 root.errorStream() << "Another streamed error";

 return 0;
Now its time to compile the above code . SO compile the code with following command -
 $ g++ -gstabs+ -w -Iheaders log4cpp.cpp -o log4cpp_exe -llog4cpp -lpthread 
And the program will compile successfully but at the time or execution I found of example
  • $ ./log4cpp_exe
The Error will come like below 
  • ERROR : ./log4cpp_inited: error while loading shared libraries: cannot open shared object file: No such file or directory
If the above error comes then what you need to do is to set you environment variable like shown in the following step .
  • export LD_LIBRARY_PATH=/usr/local/lib
To set the above environment variable follow the following steps
  • $ gedit ~/.bashrc
Then when the gedit editor for bashrc file will co to the bottom and paste

export LD_LIBRARY_PATH=/usr/local/lib

Then save the bash file and on terminal execute the following command 
  • $ source ~/.bashrc
Now your environment variable is set permanently.

Now once again Run the log4cpp_exe 
  • $ ./log4cpp_exe
and This time the program will run and show the following output.
1352973121 ERROR  : root error
1352973121 ERROR sub1 : sub1 error
1352973121 WARN sub1 : sub1 warn
1352973121 WARN  : 1 + 1 == two ?
1352973121 ERROR  : Streamed root error
1352973121 ERROR sub1 : Streamed sub1 error
1352973121 WARN sub1 : Streamed sub1 warn
1352973121 ERROR  : Another streamed error
If you see the above output on console that means your program run successfully

That's it
Build and Run Sample Code for Log4Cpp C++ library from Source Code on Ubuntu


Newest 'log4cpp' Questions
c++ - getting started with log4cpp in ubuntu
C++ programming: A useful log4cpp example
Searches related to Log4Cpp
log4cpp tutorial
log4cpp category
log4cpp vs log4cxx
log4cpp appender
log4cpp documentation
log4cpp configuration file
log4cpp properties file example
IT Certification Category (English)640x480

Partner Sites

Top Online Courses From ProgrammingKnowledge

Python Course
Java Course
Bash Course
Linux Course
C Course
C++ Course
PHP Course
Android Course
C# Course
JavaFx Course
NodeJs Course
Jenkins Course
Scala Course
Bootstrap Course
MongoDB Course
QT C++ GUI Course