1

I am trying to use log4cpp in my program (*nix machine - Ubuntu 12.10, Codeblocks) to create logs at various instances in the program.

LoggerMain.h

#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" 

MainProgram.h

#include "LoggerMain.h" MainProgram{ public: log4cpp::Appender *appender; }; 

MainProgram.cpp

appender = new log4cpp::FileAppender("default","program.log"); //getting error here appender->setLayout(new log4cpp::BasicLayout()); log4cpp::Category& root = log4cpp::Category::getRoot(); main{ //will use root here for loggin } 

Error: src/MainProgram.cpp|21|error: ‘appender’ in ‘class MainProgram’ does not name a type|

What's going wrong? I am basically doing the same thing as in the log4cpp simple example documentation here

2
  • Is that all of MainProgram.cpp? Because I'm not seeing any #include statements, or even a function definition. Commented Dec 25, 2012 at 11:54
  • @JasonD: No. It isn't. The program itself was very long so I trimmed ti to show only the problem here. Commented Dec 25, 2012 at 11:55

3 Answers 3

2

You can not have operations outside function, put below code into MainProgram construction,

MainProgram::MainProgram(){ appender = new log4cpp::FileAppender("default","program.log"); //getting error here appender->setLayout(new log4cpp::BasicLayout()); log4cpp::Category& root = log4cpp::Category::getRoot(); } 

You could use singleton mode for MainProgram

MainProgram{ public: MainProgram* instance(){ if (!instance){ instance = new MainProgram(); return instance; } } MainProgram() { appender = new new log4cpp::FileAppender("default","program.log"); //getting error here appender->setLayout(new log4cpp::BasicLayout()); } private: log4cpp::Appender *appender; MainProgram* instance; }; int main(int argc, char* argv[]) { MainProgram::Instance(); } 

Note, C++ supports two forms of main functions, you need to change main function to one of below forms:

int main() { /* ... */ } 

and

int main(int argc, char* argv[]) { /* ... */ } 
Sign up to request clarification or add additional context in comments.

5 Comments

Also,I declared the log4cpp::Category& root in the MainProgram.h and tried to use it as root = log4cpp::Category::getRoot(); but I then get an error saying ‘log4cpp::Category& log4cpp::Category::operator=(const log4cpp::Category&)’ is private Shouldn't I be using root globally across my program to write the logs or should I get instance each time in a function?
And btw when I include these things inside the function,I start getting errors saying : 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)'|`
Got it. Linker flag was missing
Just an extra query that how do I use root across my program globally, without the need of initializing it each time in a function.
declare it as a static, but you need to figure out how getRoot() works from log4j document. it may initialize itself each time.
1

You can't have code statements outside of functions. This should perhaps be inside the class constructor?

The compiler is expecting a declaration, which would start with a type. It says that appender isn't a type.

3 Comments

Also,I declared the log4cpp::Category& root in the MainProgram.h and tried to use it as root = log4cpp::Category::getRoot(); but I then get an error saying ‘log4cpp::Category& log4cpp::Category::operator=(const log4cpp::Category&)’ is private Shouldn't I be using root globally across my program to write the logs or should I get instance each time in a function?
And btw when I include these things inside the function,I start getting errors saying : 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)'|`
A difference is that in the example you link to, everything is inside the main function. You have put some of the code outside. That doesn't work (for any code).
0

It seems that you did not create an instance of MainProgram. Try something along these lines:

MainProgram mp; mp.appender = new log4cpp::FileAppender("default","program.log"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.