2014-05-20 16:17:56 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-11-26 10:59:45 +00:00
|
|
|
#include "crow/settings.h"
|
|
|
|
|
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>
|
2021-11-26 10:59:45 +00:00
|
|
|
#include <string>
|
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
|
2021-06-02 15:05:52 +00:00
|
|
|
#ifndef DEBUG
|
2016-08-27 05:15:16 +00:00
|
|
|
DEBUG = 0,
|
2014-08-07 16:14:27 +00:00
|
|
|
INFO,
|
|
|
|
WARNING,
|
|
|
|
ERROR,
|
|
|
|
CRITICAL,
|
2021-06-02 15:05:52 +00:00
|
|
|
#endif
|
2016-08-27 05:15:16 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
Debug = 0,
|
|
|
|
Info,
|
|
|
|
Warning,
|
|
|
|
Error,
|
|
|
|
Critical,
|
2014-08-07 16:14:27 +00:00
|
|
|
};
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
class ILogHandler
|
|
|
|
{
|
|
|
|
public:
|
2023-03-30 12:03:02 +00:00
|
|
|
virtual ~ILogHandler() = default;
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
virtual void log(std::string message, LogLevel level) = 0;
|
2014-08-07 16:14:27 +00:00
|
|
|
};
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
class CerrLogHandler : public ILogHandler
|
|
|
|
{
|
|
|
|
public:
|
2021-11-26 10:59:45 +00:00
|
|
|
void log(std::string message, LogLevel level) override
|
2021-11-25 11:45:38 +00:00
|
|
|
{
|
2021-11-26 10:59:45 +00:00
|
|
|
std::string prefix;
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case LogLevel::Debug:
|
|
|
|
prefix = "DEBUG ";
|
|
|
|
break;
|
|
|
|
case LogLevel::Info:
|
|
|
|
prefix = "INFO ";
|
|
|
|
break;
|
|
|
|
case LogLevel::Warning:
|
|
|
|
prefix = "WARNING ";
|
|
|
|
break;
|
|
|
|
case LogLevel::Error:
|
|
|
|
prefix = "ERROR ";
|
|
|
|
break;
|
|
|
|
case LogLevel::Critical:
|
|
|
|
prefix = "CRITICAL";
|
|
|
|
break;
|
2014-08-07 16:14:27 +00:00
|
|
|
}
|
2022-04-12 01:18:38 +00:00
|
|
|
std::cerr << std::string("(") + timestamp() + std::string(") [") + prefix + std::string("] ") + message << std::endl;
|
2021-11-25 11:45:38 +00:00
|
|
|
}
|
2014-08-07 16:14:27 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
private:
|
|
|
|
static std::string timestamp()
|
|
|
|
{
|
|
|
|
char date[32];
|
|
|
|
time_t t = time(0);
|
2015-02-20 02:07:23 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
tm my_tm;
|
2015-02-20 02:07:23 +00:00
|
|
|
|
2021-02-08 16:25:02 +00:00
|
|
|
#if defined(_MSC_VER) || defined(__MINGW32__)
|
2022-03-20 06:45:49 +00:00
|
|
|
#ifdef CROW_USE_LOCALTIMEZONE
|
|
|
|
localtime_s(&my_tm, &t);
|
|
|
|
#else
|
2021-11-25 11:45:38 +00:00
|
|
|
gmtime_s(&my_tm, &t);
|
2022-03-20 06:45:49 +00:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#ifdef CROW_USE_LOCALTIMEZONE
|
|
|
|
localtime_r(&t, &my_tm);
|
2015-02-20 02:07:23 +00:00
|
|
|
#else
|
2021-11-25 11:45:38 +00:00
|
|
|
gmtime_r(&t, &my_tm);
|
2022-03-20 06:45:49 +00:00
|
|
|
#endif
|
2015-02-20 02:07:23 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
size_t sz = strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", &my_tm);
|
|
|
|
return std::string(date, date + sz);
|
|
|
|
}
|
2021-11-26 10:59:45 +00:00
|
|
|
};
|
2014-08-07 16:14:27 +00:00
|
|
|
|
2021-11-26 10:59:45 +00:00
|
|
|
class logger
|
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
public:
|
2021-11-26 10:59:45 +00:00
|
|
|
logger(LogLevel level):
|
2021-11-25 11:45:38 +00:00
|
|
|
level_(level)
|
2021-11-27 17:44:51 +00:00
|
|
|
{}
|
2021-11-25 11:45:38 +00:00
|
|
|
~logger()
|
|
|
|
{
|
|
|
|
#ifdef CROW_ENABLE_LOGGING
|
|
|
|
if (level_ >= get_current_log_level())
|
|
|
|
{
|
|
|
|
get_handler_ref()->log(stringstream_.str(), level_);
|
2014-08-07 16:14:27 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
#endif
|
|
|
|
}
|
2014-08-07 16:14:27 +00:00
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
//
|
|
|
|
template<typename T>
|
|
|
|
logger& operator<<(T const& value)
|
|
|
|
{
|
|
|
|
#ifdef CROW_ENABLE_LOGGING
|
|
|
|
if (level_ >= get_current_log_level())
|
2014-08-07 16:14:27 +00:00
|
|
|
{
|
2021-11-25 11:45:38 +00:00
|
|
|
stringstream_ << value;
|
2014-08-07 16:14:27 +00:00
|
|
|
}
|
2021-11-25 11:45:38 +00:00
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2021-11-26 10:59:45 +00:00
|
|
|
static void setLogLevel(LogLevel level) { get_log_level_ref() = level; }
|
2021-11-25 11:45:38 +00:00
|
|
|
|
2021-11-26 10:59:45 +00:00
|
|
|
static void setHandler(ILogHandler* handler) { get_handler_ref() = handler; }
|
2021-11-25 11:45:38 +00:00
|
|
|
|
2021-11-26 10:59:45 +00:00
|
|
|
static LogLevel get_current_log_level() { return get_log_level_ref(); }
|
2021-11-25 11:45:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
//
|
|
|
|
static LogLevel& get_log_level_ref()
|
|
|
|
{
|
|
|
|
static LogLevel current_level = static_cast<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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
std::ostringstream stringstream_;
|
|
|
|
LogLevel level_;
|
2014-08-07 16:14:27 +00:00
|
|
|
};
|
2021-11-25 11:45:38 +00:00
|
|
|
} // namespace crow
|
|
|
|
|
|
|
|
#define CROW_LOG_CRITICAL \
|
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Critical) \
|
2021-11-26 10:59:45 +00:00
|
|
|
crow::logger(crow::LogLevel::Critical)
|
2021-11-25 11:45:38 +00:00
|
|
|
#define CROW_LOG_ERROR \
|
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Error) \
|
2021-11-26 10:59:45 +00:00
|
|
|
crow::logger(crow::LogLevel::Error)
|
2021-11-25 11:45:38 +00:00
|
|
|
#define CROW_LOG_WARNING \
|
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Warning) \
|
2021-11-26 10:59:45 +00:00
|
|
|
crow::logger(crow::LogLevel::Warning)
|
2021-11-25 11:45:38 +00:00
|
|
|
#define CROW_LOG_INFO \
|
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Info) \
|
2021-11-26 10:59:45 +00:00
|
|
|
crow::logger(crow::LogLevel::Info)
|
2021-11-25 11:45:38 +00:00
|
|
|
#define CROW_LOG_DEBUG \
|
|
|
|
if (crow::logger::get_current_log_level() <= crow::LogLevel::Debug) \
|
2021-11-26 10:59:45 +00:00
|
|
|
crow::logger(crow::LogLevel::Debug)
|