fix(log): get rid of source_location
This commit is contained in:
parent
c9855fd7e5
commit
f119d98c70
@ -4,6 +4,7 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ios>
|
#include <ios>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
FileChannel::FileChannel(const std::string &filename, std::ios_base::openmode mode, Log::Level logLevel)
|
FileChannel::FileChannel(const std::string &filename, std::ios_base::openmode mode, Log::Level logLevel)
|
||||||
: filename_(filename), fileStream_(filename, mode)
|
: filename_(filename), fileStream_(filename, mode)
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <source_location>
|
|
||||||
|
|
||||||
Log::Log()
|
Log::Log()
|
||||||
{
|
{
|
||||||
@ -54,20 +53,12 @@ Log &Log::getInstance()
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::log(Log::Level level, const std::string &message, const std::map<std::string, std::string> &context,
|
void Log::log(Log::Level level, const std::string &message, const std::map<std::string, std::string> &context)
|
||||||
const std::source_location &location)
|
|
||||||
{
|
{
|
||||||
for (auto &it : channels_)
|
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;
|
// 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);
|
return static_cast<int>(elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::trace(const std::string &message, const std::map<std::string, std::string> &context,
|
void Log::trace(const std::string &message, const std::map<std::string, std::string> &context)
|
||||||
const std::source_location &location)
|
|
||||||
{
|
{
|
||||||
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,
|
void Log::debug(const std::string &message, const std::map<std::string, std::string> &context)
|
||||||
const std::source_location &location)
|
|
||||||
{
|
{
|
||||||
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,
|
void Log::info(const std::string &message, const std::map<std::string, std::string> &context)
|
||||||
const std::source_location &location)
|
|
||||||
{
|
{
|
||||||
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,
|
void Log::warning(const std::string &message, const std::map<std::string, std::string> &context)
|
||||||
const std::source_location &location)
|
|
||||||
{
|
{
|
||||||
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,
|
void Log::error(const std::string &message, const std::map<std::string, std::string> &context)
|
||||||
const std::source_location &location)
|
|
||||||
{
|
{
|
||||||
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,
|
void Log::fatal(const std::string &message, const std::map<std::string, std::string> &context)
|
||||||
const std::source_location &location)
|
|
||||||
{
|
{
|
||||||
getInstance().log(Log::Level::Fatal, message, context, location);
|
getInstance().log(Log::Level::Fatal, message, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Log::logLevelToString(Log::Level level)
|
std::string Log::logLevelToString(Log::Level level)
|
||||||
|
|||||||
@ -2,15 +2,32 @@
|
|||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cstring>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <source_location>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
class Channel; // Forward declaration
|
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
|
class Log
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -29,8 +46,7 @@ class Log
|
|||||||
Log &operator=(const Log &other) = delete;
|
Log &operator=(const Log &other) = delete;
|
||||||
Log &operator=(Log &&other) = delete;
|
Log &operator=(Log &&other) = delete;
|
||||||
|
|
||||||
void log(Level level, const std::string &message, const std::map<std::string, std::string> &context,
|
void log(Level level, const std::string &message, const std::map<std::string, std::string> &context);
|
||||||
const std::source_location &location);
|
|
||||||
|
|
||||||
static void setFileChannel(const std::string &filename, std::ios_base::openmode mode = std::ios_base::app,
|
static void setFileChannel(const std::string &filename, std::ios_base::openmode mode = std::ios_base::app,
|
||||||
Level logLevel = Level::Trace);
|
Level logLevel = Level::Trace);
|
||||||
@ -38,18 +54,12 @@ class Log
|
|||||||
|
|
||||||
static int getElapsedTime();
|
static int getElapsedTime();
|
||||||
|
|
||||||
static void trace(const std::string &message, const std::map<std::string, std::string> &context = {},
|
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 = {});
|
||||||
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 = {});
|
||||||
const std::source_location &location = std::source_location::current());
|
static void warning(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 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 = {});
|
||||||
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 std::string logLevelToString(Level level);
|
static std::string logLevelToString(Level level);
|
||||||
static const char *logLevelToColor(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::setFileChannel("webserv.log", std::ios_base::app, Log::Level::Trace);
|
||||||
Log::setStdoutChannel(Log::Level::Trace);
|
Log::setStdoutChannel(Log::Level::Trace);
|
||||||
Log::info("\n======================\nStarting webserv...\n======================\n");
|
Log::info("\n======================\nStarting webserv...\n======================\n");
|
||||||
Log::warning("Testing context", {{"key1", "value1"}, {"key2", "value2"}});
|
|
||||||
ConfigManager::getInstance().init(argv[1]); // NOLINT
|
ConfigManager::getInstance().init(argv[1]); // NOLINT
|
||||||
Server server(ConfigManager::getInstance());
|
Server server(ConfigManager::getInstance());
|
||||||
server.start();
|
server.start();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user