Search code examples
c++qtlogginglog4cxx

Log4cxx producing error in log4cxx/logger.h


I have a Qt program that I am updating has an inadequate logging system (which logs to syslog) so I wish to use Log4cxx (I am new to Log4cxx). I have tried some of the examples on the webpage (https://logging.apache.org/log4cxx/latest_stable/examples.html)

I have tried to set up the basic calls in my program but I have the following 2 error which I do not understand.

/usr/include/log4cxx/logger.h:1357: error: 'LevelPtr' does not name a type
         void log(const LevelPtr& level, const std::string& message,
/usr/include/log4cxx/logger.h:1358: error: invalid use of '::'
             const log4cxx::spi::LocationInfo& location) const;
 

This error is coming from my Input.cpp file which calls the AppLog.h file which points to the error from the below marked line.

#ifndef LOG_H
#define LOG_H

#include <QString>
#include <stdlib.h>
#include <log4cxx/helpers/objectptr.h>
#include <log4cxx/level.h>

#include <log4cxx/logger.h> //ERROR HERE
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/logmanager.h>
#include <log4cxx/ndc.h>
#include <locale.h>

/*!
  * @brief Setup logging and log level to syslog. All log messages are written to syslog.
*/
class Q_Log
{
public:
  static Q_Log *getInstance();

private:
  Q_Log();
  static Q_Log *_instance;
  static log4cxx::LoggerPtr logger;

};

#endif

AppLog.cpp

    #include "AppLog.h"
    #include <QCoreApplication>
    
    using namespace log4cxx;
    using namespace log4cxx::helpers;
        
    Q_Log *Q_Log::_instance = 0;
    
    Q_Log::Q_Log()
    {
       QString name = "PROGRAM_CONVERTER_" +  QCoreApplication::arguments().at(1);
       char* p = new char[name.length() + 1];
       strcpy(p, name.toLatin1().constData());
         
    
       BasicConfigurator::configure();
       LoggerPtr rootLogger = Logger::getRootLogger();
       logger = log4cxx::LogManager::getLogger(p);
    
       LOG4CXX_INFO(logger, "Entering application.");
    }
Q_Log *Q_Log::getInstance()
{
  if(_instance == 0)
  {
    _instance = new Q_Log();
  }

  return _instance;
}

My libs line in the .pro file

LIBS += -lapr-1 -laprutil-1 -llog4cxx-1

I have log4cxx and log4cxx-devel installed and qmake runs correctly.

I have tried including many different header files for log4cxx that are not listed in the example but the same error continues. I do not have an calls to any function call log() and there are no #include <syslog.h> in the code.

Any ideas great appreciated.

Thanks.


Solution

  • Seems that it didn't like logger being defined in the header file.

    static log4cxx::LoggerPtr logger;
    

    so moved it's definition to .cpp file

    #include "Log.h"
    #include <QString>
    #include <QDebug>
    #include <QCoreApplication>
    
    #include <log4cxx/logger.h>
    #include <log4cxx/basicconfigurator.h>
    #include <log4cxx/helpers/exception.h>
    #include <log4cxx/xml/domconfigurator.h>
    #include <log4cxx/fileappender.h>
    
    using namespace log4cxx;
    using namespace log4cxx::helpers;
    using namespace log4cxx::xml;
    
    LoggerPtr rootLogger = Logger::getRootLogger();
    LoggerPtr logger(Logger::getLogger("NAME"));
    
    Q_Log *Q_Log::_instance = 0;
    
    Q_Log::Q_Log(QString filename, QString appname)
    {   
       //Some code removed here
       // Load XML configuration file using DOMConfigurator
       DOMConfigurator::configure(filename.toStdString());
       BasicConfigurator::configure();
       logger->setLevel(log4cxx::Level::getInfo());
    }