I am implementing a library and would like to provide just simple logging facilities without using a logging framework.
I have come to this small piece of code but it does a bit more than i expect :
// logging.h
#ifndef LOGGING_H
#define LOGGING_H
#include <iostream>
enum LogLevel {
// no log is the default
NoLog = 0,
// written to stderr
Err = 1,
Warning = 2,
// written to stdout
Info = 3,
Debug = 4
};
void initLogger(LogLevel level);
LogLevel loggerLevel();
#define LOG(level, msg) \
do { \
if (level > NoLog && level <= loggerLevel()) { \
if (level <= Warning) \
std::cerr << __FILE__ << "(" << __LINE__ << ") " << msg << std::endl; \
else \
std::cout << __FILE__ << "(" << __LINE__ << ") " << msg << std::endl; \
} \
} while (0)
#define LOG_ERR(msg) LOG(Err, msg)
#define LOG_WARN(msg) LOG(Warning, msg)
#define LOG_INFO(msg) LOG(Info, msg)
#define LOG_DEBUG(msg) LOG(Debug, msg)
#endif // LOGGING_H
and
// logging.cpp
include "logging.h"
static LogLevel logLevel = NoLog;
void initLogger(LogLevel level)
{
logLevel = level;
}
LogLevel loggerLevel()
{
return logLevel;
}
Here, the user is able to change the logging level at run-time which i don't want :
initLogger(Err);
// not printed
LOG_INFO("info");
initLogger(Info);
// printed
LOG_INFO("info");
I would prefer to give the user the choice to initialize the logging level once and for all or skip this step completely and fallback to a default level. I have a singleton in mind.
I guess there should be something more straightforward. What would be the best way to achieve that ?
Thank you.
With singleton, you might do:
So first call to
loggerLevelInstanceinitialize for once.