fix(log): get rid of source_location
This commit is contained in:
parent
c9855fd7e5
commit
f119d98c70
@ -4,6 +4,7 @@
|
||||
#include <chrono>
|
||||
#include <ios>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
FileChannel::FileChannel(const std::string &filename, std::ios_base::openmode mode, Log::Level logLevel)
|
||||
: filename_(filename), fileStream_(filename, mode)
|
||||
|
||||
@ -6,7 +6,6 @@
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <source_location>
|
||||
|
||||
Log::Log()
|
||||
{
|
||||
@ -54,20 +53,12 @@ Log &Log::getInstance()
|
||||
return instance;
|
||||
}
|
||||
|
||||
void Log::log(Log::Level level, const std::string &message, const std::map<std::string, std::string> &context,
|
||||
const std::source_location &location)
|
||||
{
|
||||
void Log::log(Log::Level level, const std::string &message, const std::map<std::string, std::string> &context)
|
||||
{
|
||||
for (auto &it : channels_)
|
||||
{
|
||||
std::string extendedMessage;
|
||||
extendedMessage += message + "\n\t| ";
|
||||
|
||||
extendedMessage += std::filesystem::path(location.file_name()).filename().string();
|
||||
extendedMessage += ":" + std::to_string(location.line()) + ":" + std::to_string(location.column());
|
||||
extendedMessage += " (" + std::string(location.function_name()) + ")";
|
||||
|
||||
// extendedMessage += " | " + message;
|
||||
it.second->log(level, extendedMessage, context);
|
||||
it.second->log(level, message, context);
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,40 +70,34 @@ int Log::getElapsedTime()
|
||||
return static_cast<int>(elapsed);
|
||||
}
|
||||
|
||||
void Log::trace(const std::string &message, const std::map<std::string, std::string> &context,
|
||||
const std::source_location &location)
|
||||
void Log::trace(const std::string &message, const std::map<std::string, std::string> &context)
|
||||
{
|
||||
getInstance().log(Log::Level::Trace, message, context, location);
|
||||
getInstance().log(Log::Level::Trace, message, context);
|
||||
}
|
||||
|
||||
void Log::debug(const std::string &message, const std::map<std::string, std::string> &context,
|
||||
const std::source_location &location)
|
||||
void Log::debug(const std::string &message, const std::map<std::string, std::string> &context)
|
||||
{
|
||||
getInstance().log(Log::Level::Debug, message, context, location);
|
||||
getInstance().log(Log::Level::Debug, message, context);
|
||||
}
|
||||
|
||||
void Log::info(const std::string &message, const std::map<std::string, std::string> &context,
|
||||
const std::source_location &location)
|
||||
void Log::info(const std::string &message, const std::map<std::string, std::string> &context)
|
||||
{
|
||||
getInstance().log(Log::Level::Info, message, context, location);
|
||||
getInstance().log(Log::Level::Info, message, context);
|
||||
}
|
||||
|
||||
void Log::warning(const std::string &message, const std::map<std::string, std::string> &context,
|
||||
const std::source_location &location)
|
||||
void Log::warning(const std::string &message, const std::map<std::string, std::string> &context)
|
||||
{
|
||||
getInstance().log(Log::Level::Warn, message, context, location);
|
||||
getInstance().log(Log::Level::Warn, message, context);
|
||||
}
|
||||
|
||||
void Log::error(const std::string &message, const std::map<std::string, std::string> &context,
|
||||
const std::source_location &location)
|
||||
void Log::error(const std::string &message, const std::map<std::string, std::string> &context)
|
||||
{
|
||||
getInstance().log(Log::Level::Error, message, context, location);
|
||||
getInstance().log(Log::Level::Error, message, context);
|
||||
}
|
||||
|
||||
void Log::fatal(const std::string &message, const std::map<std::string, std::string> &context,
|
||||
const std::source_location &location)
|
||||
void Log::fatal(const std::string &message, const std::map<std::string, std::string> &context)
|
||||
{
|
||||
getInstance().log(Log::Level::Fatal, message, context, location);
|
||||
getInstance().log(Log::Level::Fatal, message, context);
|
||||
}
|
||||
|
||||
std::string Log::logLevelToString(Log::Level level)
|
||||
|
||||
@ -2,15 +2,32 @@
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
class Channel; // Forward declaration
|
||||
|
||||
constexpr const char *extractFilename(const char *path)
|
||||
{
|
||||
const char *filename = path;
|
||||
while (*path != '\0')
|
||||
{
|
||||
if (*path == '/' || *path == '\\')
|
||||
{
|
||||
filename = path + 1; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
||||
}
|
||||
++path; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
#define LOCATION \
|
||||
(std::string(extractFilename(__FILE__)) + ":" + std::to_string(__LINE__) + " (" + std::string(__FUNCTION__) + ")")
|
||||
|
||||
class Log
|
||||
{
|
||||
public:
|
||||
@ -29,8 +46,7 @@ class Log
|
||||
Log &operator=(const Log &other) = delete;
|
||||
Log &operator=(Log &&other) = delete;
|
||||
|
||||
void log(Level level, const std::string &message, const std::map<std::string, std::string> &context,
|
||||
const std::source_location &location);
|
||||
void log(Level level, const std::string &message, const std::map<std::string, std::string> &context);
|
||||
|
||||
static void setFileChannel(const std::string &filename, std::ios_base::openmode mode = std::ios_base::app,
|
||||
Level logLevel = Level::Trace);
|
||||
@ -38,18 +54,12 @@ class Log
|
||||
|
||||
static int getElapsedTime();
|
||||
|
||||
static void trace(const std::string &message, const std::map<std::string, std::string> &context = {},
|
||||
const std::source_location &location = std::source_location::current());
|
||||
static void debug(const std::string &message, const std::map<std::string, std::string> &context = {},
|
||||
const std::source_location &location = std::source_location::current());
|
||||
static void info(const std::string &message, const std::map<std::string, std::string> &context = {},
|
||||
const std::source_location &location = std::source_location::current());
|
||||
static void warning(const std::string &message, const std::map<std::string, std::string> &context = {},
|
||||
const std::source_location &location = std::source_location::current());
|
||||
static void error(const std::string &message, const std::map<std::string, std::string> &context = {},
|
||||
const std::source_location &location = std::source_location::current());
|
||||
static void fatal(const std::string &message, const std::map<std::string, std::string> &context = {},
|
||||
const std::source_location &location = std::source_location::current());
|
||||
static void trace(const std::string &message, const std::map<std::string, std::string> &context = {});
|
||||
static void debug(const std::string &message, const std::map<std::string, std::string> &context = {});
|
||||
static void info(const std::string &message, const std::map<std::string, std::string> &context = {});
|
||||
static void warning(const std::string &message, const std::map<std::string, std::string> &context = {});
|
||||
static void error(const std::string &message, const std::map<std::string, std::string> &context = {});
|
||||
static void fatal(const std::string &message, const std::map<std::string, std::string> &context = {});
|
||||
|
||||
static std::string logLevelToString(Level level);
|
||||
static const char *logLevelToColor(Level level);
|
||||
|
||||
@ -18,7 +18,7 @@ int main(int argc, char **argv)
|
||||
Log::setFileChannel("webserv.log", std::ios_base::app, Log::Level::Trace);
|
||||
Log::setStdoutChannel(Log::Level::Trace);
|
||||
Log::info("\n======================\nStarting webserv...\n======================\n");
|
||||
Log::warning("Testing context", {{"key1", "value1"}, {"key2", "value2"}});
|
||||
|
||||
ConfigManager::getInstance().init(argv[1]); // NOLINT
|
||||
Server server(ConfigManager::getInstance());
|
||||
server.start();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user