"log4cpp::Category& category = ..." Syntax??

Jul 3, 2011 at 8:10pm
Background‬‬ : The ampersand in "log4cpp::Category& category = ..." is beyond my knowledge. I am actively learning C++, but I am still very inexperienced. I am trying to build my own logging class that utilizes log4cpp logging facilities. log4cpp works fine for me if I implement a simple example in main(), but I (strategically) want the simplification of my own custom class that isolates my log4cpp code use into one simple class (involving only one .h and one .cpp source).

‪‪Need‬‬ : I need to split my log4cpp component definitions and declarations between A) a header file and B) constructor and member functions. I do not fully understand the instantiation of the "category" object in the "Category.hh" header, or example, so I am not succeeding in splitting-out the "category" object to header-based definition(s) vs. its instantiation/initialization instructions. The ampersand character in "‪log4cpp::Category& category =" ‬is completely loosing me. My mind is trying to "take the address of …" with the ampersand syntax.‪ ‬Please advise me. I am hopelessly lost (I have reviewed the Category.hh header source). Thanks for any help you can provide!!!


Working Example‬

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

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

‪‪int‬ ‬main()
{
log4cpp::Appender *appender = ‪‪new‬ ‬log4cpp::FileAppender(‪"FileAppender"‬,LOGFILE);
log4cpp::Layout *layout = ‪‪new‬ ‬log4cpp:‪‬impleLayout();
log4cpp::Category& category = log4cpp::Category::getInstance(‪"Category"‬);

appender->setLayout(layout);
category.setAppender(appender);
category.setPriority(log4cpp:‪‬riority::INFO);

‪category.info(‬"sample log entry"‪);‬
}


My Feable Attempts to Split "Category" Definition from Declaration‬

(same headers as example above)

class eLog
{
private:

log4cpp::Appender *appender;
log4cpp::Layout *layout;
log4cpp::Category category; ‪(this is not working, just an attempt)‬
...
}


eLog::eLog()
{
appender = new log4cpp::FileAppender("FileAppender",LOGFILE);
layout = new log4cpp:‪‬impleLayout();
category.getInstance("Category"); ‪(this is not working, just an attempt)‬

appender->setLayout(layout);
category.setAppender(appender);
category.setPriority(log4cpp:‪‬riority::INFO);
}


log4cpp Category Header‬ Definitions (excerpt)

class LOG4CPP_EXPORT Category {

friend class HierarchyMaintainer;

public:
/**
* Return the root of the Category hierarchy.
*
* The root category is always instantiated and available. It's
* name is the empty string.
* Unlike in log4j, calling Category.getInstance("")
* ‪does‬ retrieve the root category
* and not a category just under root named "".
* @returns The root category
**/

static Category& getRoot();
...

/**
* Instantiate a Category with name name. This
* method does not set priority of the category which is by
* default Priority::NOTSET.
*
* @param name The name of the category to retrieve.
**/
static Category& getInstance(const std::string& name);
...

‪‪‪‪Summary‬‬ : How do I split the Category definition and declaration??? Thanks for any help you can provide!!!
Jul 13, 2011 at 10:13am
& in a declaration notes a reference. http://en.wikipedia.org/wiki/Reference_(C%2B%2B)

I don't understand what you are trying to split. It seems that what you are doing is wrapping things into a class.
Any way, read something about references, then read about initializer lists. That should explain how to use a reference here.
Though you could get by with a pointer (probably even with a normal variable, but that might cause problems. I can't say for sure, because I've never used log4cpp)
Topic archived. No new replies allowed.