log4cpp with eclipse

Hi

I am new to c++. I want to develop a transaction management system. in that case i have to maintain log files. I got the an API for log file creating. It is log4cpp. I installed it. I am using eclipse indigo for c++. I try with fallowing example code.

#include <stdio.h>
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/SimpleLayout.hh>

#define LOGFILE "/home/user/test.log"

int main()
{
/*Setting up Appender, layout and Category*/
log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",LOGFILE);
log4cpp::Layout *layout = new log4cpp::SimpleLayout();
log4cpp::Category& category = log4cpp::Category::getInstance("Category");

appender->setLayout(layout);
category.setAppender(appender);
category.setPriority(log4cpp::Priority::INFO);

/*The actual logging*/
category.info("This is for tracing the flow");
category.notice("This is to notify certain events");
category.warn("This is to generate certain warnings");
}


I got this code from http://joysofprogramming.com/simple-log4cpp-example/

But in eclipse it's not work. And "log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",LOGFILE);" line and other lines are indicate red colour. the compiler message is "undefined reference to `log4cpp::FileAppender::FileAppender(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool,
unsigned int)'". Please help me. I don't know how to config this. the preprocessor including not indicate red colour.

Thank you.
Look in log4cpp/FileAppender.hh, what is the actual signature of the constructor?
Hi,

Thank you kbw. the log4cpp/FileAppender.hh constructor is ".hh" but It's not working. can anyone give me a working example for log4cpp? please.

Thank you.
You need to open the file, look at the class definition and identify the constructors. Once you've done that, you'll be able to pass in the correct parameters to the constructor as you're currently passing in the wrong parameters and the compiler's trying to tell you that; that's what the message means.
Hi

I done it like fallowing. but it still not working. Can you give me a sample code for this please.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/SimpleLayout.hh>

#define LOGFILE "/opt/text.txt"

int main()
{
	 /*Setting up Appender, layout and Category*/
	    int fd;
	    fd = open (LOGFILE,O_CREAT|O_APPEND|O_WRONLY );
	    if (fd < 0) {
	       perror("File Open: Error");
	       exit(0);
	    }

	    log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",fd);
	    log4cpp::Layout *layout = new log4cpp::SimpleLayout();
	    log4cpp::Category& category = log4cpp::Category::getInstance("Category");

	    appender->setLayout(layout);
	    category.setAppender(appender);
	    category.setPriority(log4cpp::Priority::INFO);

	    category.info("This is for tracing the flow");
	    category.notice("This is to notify certain events");
	    category.warn("This is to generate certain warnings");
}


I am using fedora 15 and it is 64 bit.

Thank you.
Ok, you made me download the file!

The header has:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        /**
           Constructs a FileAppender.
           @param name the name of the Appender.
           @param fileName the name of the file to which the Appender has 
           to log.
           @param append whether the Appender has to truncate the file or
           just append to it if it already exists. Defaults to 'true'.
           @param mode file mode to open the logfile with. Defaults to 00644.
        **/  
        FileAppender(const std::string& name, const std::string& fileName,
                     bool append = true, mode_t mode = 00644);

        /**
           Constructs a FileAppender to an already open file descriptor.
           @param name the name of the Appender.
           @param fd the file descriptor to which the Appender has to log.
        **/
        FileAppender(const std::string& name, int fd);


So use one of those constructors.
He is using those constructors.
You need to configure the project to link against the library.
From your link g++ log.cpp -llog4cpp
Hi,

Thank you both of you. I am still with my problem. I also think this problem with the configurations of eclipse. I did this

write click on the project
select the properties menu item (I got a window with some tabs and a tree view)
then I did select C/C++ Build from the tree view
then I got the Settings tree node. I clicked it.
then I got Tool Settings tab. Inside of that tab there also another tree view.
I did select the GCC C++ Linker and Libraries tree node.
then I got a place as Libraries (-l) for give the Library names and other text area for give the Library path as it has Library search path (-L) as the name.
I did put the log4cpp as the Library name and the "/usr/include/log4cpp" as my library location.
I did clean and try to build again.

The previous error indicators are cleaned but the project getting a red cross mark. Can anyone help me to find haw to configure the eclipse for the log4cpp library.

Thank you.
Topic archived. No new replies allowed.