Skip to content
Permalink
Browse files

logging function reorganization

 * possible to use other loggers than log4cpp and stderr
 * setting defaultLogLevel(...) no longer reconfigures log4cpp
  • Loading branch information...
alexander-buerger-met-no committed Feb 22, 2017
1 parent 94f993a commit 704388a740caca7887bb3488d80d785784ea6236
Showing with 228 additions and 82 deletions.
  1. +57 −6 include/fimex/Logger.h
  2. +18 −9 src/Log4cppLogger.cc
  3. +18 −10 src/Log4cppLogger.h
  4. +135 −57 src/Logger.cc
@@ -25,11 +25,15 @@
#define LOGGER_H_

#include <boost/shared_ptr.hpp>
#include <list>
#include <string>
#include <sstream>

namespace MetNoFimex {

class LoggerClass;
class LoggerImpl;

/**
* @headerfile fimex/Logger.h
*/
@@ -46,8 +50,9 @@ class Logger
{
private:
std::string className_;
std::auto_ptr<Logger> pimpl_; // lazy initialized member
Logger* getImpl();
std::auto_ptr<LoggerImpl> pimpl_; // lazy initialized member
LoggerImpl* impl();

public:
/**
* different log levels
@@ -60,20 +65,27 @@ class Logger
INFO = 600,
DEBUG = 500
};

Logger(const std::string& className);
virtual ~Logger();
~Logger();

/**
* check if the loglevel of this logger is active
*/
virtual bool isEnabledFor(LogLevel level);
bool isEnabledFor(LogLevel level);

/** Delete the present logger implementation. */
void reset();

/**
* log (without checking) for this loglevel
* log for this loglevel
* @param level log-level to log
* @param message log-message
* @param filename best retrieved with __FILE__
* @param lineNumber best retrieved with __LINE__
*/
virtual void forcedLog(LogLevel level, const std::string& message, const char* filename, unsigned int lineNumber);
void forcedLog(LogLevel level, const std::string& message, const char* filename, unsigned int lineNumber);

/**
* different logger eventually enabled in fimex
*/
@@ -88,8 +100,47 @@ class Logger
* @return false if the logClass is not compiled into fimex
*/
static bool setClass(LogClass logClass);

/**
* Choose a loggerclass in fimex
*
* Changing LoggerClass after invoking any functions in the
* library (more precisely, after any creation of LoggerImpl
* objects), is experimental.
*
* Ownership of lc is transferred. To destroy the LoggerClass,
* e.g. before exit, it is necessary to call setClass(0).
*
* @param lc, ownership is transferred; deletes the present loggerclass instance
* @return false if lc is null
*/
static bool setClass(LoggerClass* lc);
};


class LoggerImpl {
public:
virtual ~LoggerImpl();
virtual bool isEnabledFor(Logger::LogLevel level) = 0;
virtual void log(Logger::LogLevel level, const std::string& message, const char* filename, unsigned int lineNumber) = 0;
};


class LoggerClass {
public:
virtual ~LoggerClass();
virtual LoggerImpl* loggerFor(Logger* logger, const std::string& className) = 0;

protected:
void remember(Logger* logger);
void reset();

protected:
typedef std::list<Logger*> loggers_t;
loggers_t loggers_;
};


/**
* the defaultLogLevel can be used by the implemented logger to determine
* the minimum LogLevel. This value might be ignored/overwritten by a configuration
@@ -29,8 +29,7 @@
#include "fimex/Utils.h"
#include "log4cpp/CategoryStream.hh"

namespace MetNoFimex
{
namespace MetNoFimex {

using namespace log4cpp;

@@ -43,22 +42,32 @@ Priority::Value logLevel2cppPriority(Logger::LogLevel level)
case Logger::INFO: return Priority::INFO;
case Logger::DEBUG: return Priority::DEBUG;
case Logger::OFF: return Priority::NOTSET;
default: assert(false); break;
}
return Priority::FATAL;
}

Log4cppLogger::Log4cppLogger(const std::string& className)
: log_(log4cpp::Category::getInstance(className))
{
}

bool Log4cppLogger::isEnabledFor(Logger::LogLevel level)
{
Priority::Value prio = logLevel2cppPriority(level);
return log_.isPriorityEnabled(prio);
}
void Log4cppLogger::forcedLog(LogLevel level, const std::string& message, const char* filename, unsigned int lineNumber)

void Log4cppLogger::log(Logger::LogLevel level, const std::string& message, const char* filename, unsigned int lineNumber)
{
log_.getStream(logLevel2cppPriority(level))
<< message
<< " in " << filename
<< " at line " << lineNumber;
}

LoggerImpl* Log4cppClass::loggerFor(Logger* logger, const std::string& className)
{
using namespace std;
stringstream ss;
ss << message << " in " << filename << " at line " << lineNumber;
log_.getStream(logLevel2cppPriority(level)) << ss.str();
remember(logger);
return new Log4cppLogger(className);
}

} /* namespace MetNoFimex */
@@ -27,9 +27,12 @@
#ifndef LOG4CPPLOGGER_H_
#define LOG4CPPLOGGER_H_

#include "fimex/Logger.h"
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif // HAVE_CONFIG_H

#ifdef HAVE_LOG4CPP
#include "fimex/Logger.h"
#include "log4cpp/Appender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
@@ -39,32 +42,37 @@
#include <boost/shared_ptr.hpp>
#include "log4cpp/Category.hh"

namespace MetNoFimex
{
namespace MetNoFimex {

log4cpp::Priority::Value logLevel2cppPriority(Logger::LogLevel level);

class Log4cppLogger: public MetNoFimex::Logger
{
class Log4cppLogger : public LoggerImpl {
public:
Log4cppLogger(const std::string& className) : Logger(className), log_(log4cpp::Category::getInstance(className)) {};
virtual ~Log4cppLogger() {};
Log4cppLogger(const std::string& className);

/**
* check if the loglevel of this logger is active
*/
virtual bool isEnabledFor(LogLevel level);
bool isEnabledFor(Logger::LogLevel level) /* override */;

/**
* log (without checking) for this loglevel
* log for this loglevel
* @param level log-level to log
* @param message log-message
* @param filename best retrieved with __FILE__
* @param lineNumber best retrieved with __LINE__
*/
virtual void forcedLog(LogLevel level, const std::string& message, const char* filename, unsigned int lineNumber);
void log(Logger::LogLevel level, const std::string& message, const char* filename, unsigned int lineNumber);

private:
log4cpp::Category& log_;
};

class Log4cppClass : public LoggerClass {
public:
LoggerImpl* loggerFor(Logger* logger, const std::string& className) /* override */;
};

} /* namespace MetNoFimex */
#endif /* HAVE_LOG4PP */
#endif /* LOG4CPPLOGGER_H_ */

0 comments on commit 704388a

Please sign in to comment.
You can’t perform that action at this time.