From 7595e8f19e0c7e03128d38e4aba7bd4a25c79d1e Mon Sep 17 00:00:00 2001 From: neru Date: Thu, 7 May 2026 01:25:37 -0300 Subject: [PATCH] feat: add log library --- src/lib/seallib/log.h | 84 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/lib/seallib/log.h diff --git a/src/lib/seallib/log.h b/src/lib/seallib/log.h new file mode 100644 index 0000000..8cc2d72 --- /dev/null +++ b/src/lib/seallib/log.h @@ -0,0 +1,84 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace seallib +{ + enum class LogType + { + VERBOSE, + INFO, + WARNING, + ERROR + }; + + inline constexpr std::string_view logTypeNames[]{"VERBOSE", "INFO", "WARNING", "ERROR"}; + inline constexpr std::string_view logTypeColors[]{"\x1b[34;40m", "\x1b[32;40m", "\x1b[30;43m", "\x1b[97;41m"}; + + inline constexpr std::string_view getLogTypeName(LogType type) + { + return logTypeNames[static_cast(type)]; + } + + inline constexpr std::string_view getLogTypeColor(LogType type) + { + return logTypeColors[static_cast(type)]; + } + + /* + log sink for subscriptions + */ + class ILogSink + { + public: + virtual ~ILogSink() = default; + virtual void receiveLog(LogType type, std::string_view loggerName, std::string_view message) = 0; + }; + + /* + logger class + */ + class Logger + { + public: + explicit Logger(std::string_view name) : _name(name) {} + + template void verbose(std::format_string fmt, Args&&... args) + { + write(LogType::VERBOSE, std::vformat(fmt.get(), std::make_format_args(args...))); + } + + template void info(std::format_string fmt, Args&&... args) + { + write(LogType::INFO, std::vformat(fmt.get(), std::make_format_args(args...))); + } + + template void warning(std::format_string fmt, Args&&... args) + { + write(LogType::WARNING, std::vformat(fmt.get(), std::make_format_args(args...))); + } + + template void error(std::format_string fmt, Args&&... args) + { + write(LogType::ERROR, std::vformat(fmt.get(), std::make_format_args(args...))); + } + + [[nodiscard]] std::string_view getName() const { return _name; } + void addSink(std::shared_ptr sink) { _sinks.push_back(std::move(sink)); } + + protected: + void write(LogType type, const std::string& message) + { + for (auto& sink : _sinks) + sink->receiveLog(type, _name, message); + } + + private: + std::string _name; + std::vector> _sinks; + }; +} // namespace seallib