Added log handlers via the ILogHandler interface. At the moment these are sti pretty trivial but should still allow you to add handlers for network, file etc.

This commit is contained in:
Antony Woods 2014-05-23 08:22:12 +01:00
parent 1ae0387a25
commit 639bacf024
2 changed files with 101 additions and 66 deletions

View File

@ -3,6 +3,13 @@
#include <sstream>
class ExampleLogHandler : public crow::ILogHandler {
public:
void log(string message, crow::LogLevel level) override {
cerr << "ExampleLogHandler -> " << message;
}
};
int main()
{
crow::Crow app;
@ -53,6 +60,9 @@ int main()
return crow::response{os.str()};
});
//crow::logger::setLogLevel(LogLevel::INFO);
//crow::logger::setHandler(std::make_shared<ExampleLogHandler>());
app.port(18080)
.multithreaded()
.run();

157
logging.h
View File

@ -8,75 +8,100 @@
using namespace std;
string timeStamp()
namespace crow
{
char date[32];
time_t t = time(0);
strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", gmtime(&t));
return string(date);
enum class LogLevel
{
CRITICAL,
ERROR,
WARNING,
INFO,
DEBUG
};
class ILogHandler {
public:
virtual void log(string message, LogLevel level) = 0;
};
class CerrLogHandler : public ILogHandler {
public:
void log(string message, LogLevel level) override {
cerr << message;
}
};
class logger {
private:
//
static string timeStamp()
{
char date[32];
time_t t = time(0);
strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", gmtime(&t));
return string(date);
}
public:
//
static LogLevel currentLevel;
static std::shared_ptr<ILogHandler> currentHandler;
logger(string prefix, LogLevel level) : m_prefix(prefix), m_level(level) {
}
~logger() {
#ifdef CROW_ENABLE_LOGGING
if(m_level <= currentLevel) {
ostringstream str;
str << "(" << timeStamp() << ") [" << m_prefix << "] " << m_stringStream.str() << endl;
currentHandler->log(str.str(), m_level);
}
#endif
}
//
template <typename T>
logger& operator<<(T const &value) {
#ifdef CROW_ENABLE_LOGGING
if(m_level <= currentLevel) {
m_stringStream << value;
}
#endif
return *this;
}
//
static void setLogLevel(LogLevel level) {
currentLevel = level;
}
static void setHandler(std::shared_ptr<ILogHandler> handler) {
currentHandler = handler;
}
private:
//
ostringstream m_stringStream;
string m_prefix;
LogLevel m_level;
};
//
LogLevel logger::currentLevel = (LogLevel)CROW_LOG_LEVEL;
std::shared_ptr<ILogHandler> logger::currentHandler = std::make_shared<CerrLogHandler>();
}
class logger {
public:
//
enum class Level
{
CRITICAL,
ERROR,
WARNING,
INFO,
DEBUG
};
//
static Level currentLevel;
logger(string prefix, logger::Level level) : m_prefix(prefix), m_level(level) {
}
~logger() {
#ifdef CROW_ENABLE_LOGGING
if(m_level <= currentLevel) {
cerr << "(" << timeStamp() << ") [" << m_prefix << "] " << m_stringStream.str() << endl;
}
#endif
}
//
template <typename T>
logger& operator<<(T const &value) {
#ifdef CROW_ENABLE_LOGGING
if(m_level <= currentLevel) {
m_stringStream << value;
}
#endif
return *this;
}
//
static void setLogLevel(logger::Level level) {
currentLevel = level;
}
private:
//
ostringstream m_stringStream;
string m_prefix;
Level m_level;
};
//
logger::Level logger::currentLevel = (Level)CROW_LOG_LEVEL;
#define CROW_LOG_CRITICAL logger("CRITICAL", logger::Level::CRITICAL)
#define CROW_LOG_ERROR logger("ERROR ", logger::Level::ERROR)
#define CROW_LOG_WARNING logger("WARNING ", logger::Level::WARNING)
#define CROW_LOG_INFO logger("INFO ", logger::Level::INFO)
#define CROW_LOG_DEBUG logger("DEBUG ", logger::Level::DEBUG)
#define CROW_LOG_CRITICAL logger("CRITICAL", crow::LogLevel::CRITICAL)
#define CROW_LOG_ERROR logger("ERROR ", crow::LogLevel::ERROR)
#define CROW_LOG_WARNING logger("WARNING ", crow::LogLevel::WARNING)
#define CROW_LOG_INFO logger("INFO ", crow::LogLevel::INFO)
#define CROW_LOG_DEBUG logger("DEBUG ", crow::LogLevel::DEBUG)