2014-05-20 16:17:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
2014-07-08 09:26:55 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
2014-05-29 16:10:18 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2014-05-20 16:17:56 +00:00
|
|
|
|
2016-09-21 14:11:06 +00:00
|
|
|
#include "crow/settings.h"
|
2014-07-08 09:26:35 +00:00
|
|
|
|
2014-05-23 07:22:12 +00:00
|
|
|
namespace crow
|
2014-05-20 16:17:56 +00:00
|
|
|
{
|
2014-08-07 16:14:27 +00:00
|
|
|
enum class LogLevel
|
|
|
|
{
|
2016-08-27 05:15:16 +00:00
|
|
|
#ifndef ERROR
|
|
|
|
DEBUG = 0,
|
2014-08-07 16:14:27 +00:00
|
|
|
INFO,
|
|
|
|
WARNING,
|
|
|
|
ERROR,
|
|
|
|
CRITICAL,
|
2016-08-27 05:15:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Debug = 0,
|
|
|
|
Info,
|
|
|
|
Warning,
|
|
|
|
Error,
|
|
|
|
Critical,
|
2014-08-07 16:14:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ILogHandler {
|
|
|
|
public:
|
2014-10-22 08:34:30 +00:00
|
|
|
virtual void log(std::string message, LogLevel level) = 0;
|
2014-08-07 16:14:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CerrLogHandler : public ILogHandler {
|
|
|
|
public:
|
2016-08-27 09:03:49 +00:00
|
|
|
void log(std::string message, LogLevel /*level*/) override {
|
2014-10-22 08:40:33 +00:00
|
|
|
std::cerr << message;
|
2014-08-07 16:14:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class logger {
|
|
|
|
|
|
|
|
private:
|
|
|
|
//
|
2014-10-22 08:34:30 +00:00
|
|
|
static std::string timestamp()
|
2014-08-07 16:14:27 +00:00
|
|
|
{
|
|
|
|
char date[32];
|
2015-02-20 02:07:23 +00:00
|
|
|
time_t t = time(0);
|
|
|
|
|
|
|
|
tm my_tm;
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
gmtime_s(&my_tm, &t);
|
|
|
|
#else
|
|
|
|
gmtime_r(&t, &my_tm);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size_t sz = strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", &my_tm);
|
|
|
|
return std::string(date, date+sz);
|
2014-08-07 16:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
2014-10-22 08:34:30 +00:00
|
|
|
logger(std::string prefix, LogLevel level) : level_(level) {
|
2014-08-07 16:14:27 +00:00
|
|
|
#ifdef CROW_ENABLE_LOGGING
|
|
|
|
stringstream_ << "(" << timestamp() << ") [" << prefix << "] ";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
~logger() {
|
|
|
|
#ifdef CROW_ENABLE_LOGGING
|
|
|
|
if(level_ >= get_current_log_level()) {
|
2014-10-22 08:40:33 +00:00
|
|
|
stringstream_ << std::endl;
|
2014-08-07 16:14:27 +00:00
|
|
|
get_handler_ref()->log(stringstream_.str(), level_);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
template <typename T>
|
|
|
|
logger& operator<<(T const &value) {
|
|
|
|
|
|
|
|
#ifdef CROW_ENABLE_LOGGING
|
|
|
|
if(level_ >= get_current_log_level()) {
|
|
|
|
stringstream_ << value;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
static void setLogLevel(LogLevel level) {
|
|
|
|
get_log_level_ref() = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setHandler(ILogHandler* handler) {
|
|
|
|
get_handler_ref() = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LogLevel get_current_log_level() {
|
|
|
|
return get_log_level_ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
//
|
|
|
|
static LogLevel& get_log_level_ref()
|
|
|
|
{
|
|
|
|
static LogLevel current_level = (LogLevel)CROW_LOG_LEVEL;
|
|
|
|
return current_level;
|
|
|
|
}
|
|
|
|
static ILogHandler*& get_handler_ref()
|
|
|
|
{
|
|
|
|
static CerrLogHandler default_handler;
|
|
|
|
static ILogHandler* current_handler = &default_handler;
|
|
|
|
return current_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-10-22 08:34:30 +00:00
|
|
|
std::ostringstream stringstream_;
|
2014-08-07 16:14:27 +00:00
|
|
|
LogLevel level_;
|
|
|
|
};
|
2014-05-23 07:22:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 01:44:49 +00:00
|
|
|
#define CROW_LOG_CRITICAL \
|
2016-08-27 05:15:16 +00:00
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Critical) \
|
|
|
|
crow::logger("CRITICAL", crow::LogLevel::Critical)
|
2014-08-15 01:44:49 +00:00
|
|
|
#define CROW_LOG_ERROR \
|
2016-08-27 05:15:16 +00:00
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Error) \
|
|
|
|
crow::logger("ERROR ", crow::LogLevel::Error)
|
2014-08-15 01:44:49 +00:00
|
|
|
#define CROW_LOG_WARNING \
|
2016-08-27 05:15:16 +00:00
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Warning) \
|
|
|
|
crow::logger("WARNING ", crow::LogLevel::Warning)
|
2014-08-15 01:44:49 +00:00
|
|
|
#define CROW_LOG_INFO \
|
2016-08-27 05:15:16 +00:00
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Info) \
|
|
|
|
crow::logger("INFO ", crow::LogLevel::Info)
|
2014-08-15 01:44:49 +00:00
|
|
|
#define CROW_LOG_DEBUG \
|
2016-08-27 05:15:16 +00:00
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Debug) \
|
|
|
|
crow::logger("DEBUG ", crow::LogLevel::Debug)
|
2014-05-20 16:17:56 +00:00
|
|
|
|