feat: add log library

This commit is contained in:
2026-05-07 01:25:37 -03:00
parent 46a991224c
commit 7595e8f19e
+84
View File
@@ -0,0 +1,84 @@
#pragma once
#include <string>
#include <string_view>
#include <format>
#include <memory>
#include <vector>
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<size_t>(type)];
}
inline constexpr std::string_view getLogTypeColor(LogType type)
{
return logTypeColors[static_cast<size_t>(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 <typename... Args> void verbose(std::format_string<Args...> fmt, Args&&... args)
{
write(LogType::VERBOSE, std::vformat(fmt.get(), std::make_format_args(args...)));
}
template <typename... Args> void info(std::format_string<Args...> fmt, Args&&... args)
{
write(LogType::INFO, std::vformat(fmt.get(), std::make_format_args(args...)));
}
template <typename... Args> void warning(std::format_string<Args...> fmt, Args&&... args)
{
write(LogType::WARNING, std::vformat(fmt.get(), std::make_format_args(args...)));
}
template <typename... Args> void error(std::format_string<Args...> 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<ILogSink> 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<std::shared_ptr<ILogSink>> _sinks;
};
} // namespace seallib