From f3ba19b162a89b2081c0598b4a0bf126146e3671 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Thu, 4 Apr 2024 22:05:06 +0200 Subject: Add logging system --- deimos/core/log.h | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 deimos/core/log.h (limited to 'deimos/core/log.h') diff --git a/deimos/core/log.h b/deimos/core/log.h new file mode 100644 index 0000000..cf92391 --- /dev/null +++ b/deimos/core/log.h @@ -0,0 +1,71 @@ +#pragma once + +#include "deimos/core/base.h" +#include "deimos/core/id_name.h" +#include "deimos/core/format.h" + +namespace deimos +{ + +enum class LogSeverity +{ + kInfo, + kDebug, + kError, +}; + +class ILogger +{ +public: + ILogger() = default; + deimos_NO_COPY_MOVE(ILogger); + virtual ~ILogger() = default; + + // @Todo Use string view + virtual void Log(LogSeverity, const SourceLocation&, Span) = 0; +}; + +// Just a helper to pass a SourceLocation without having to write {} to logging functions. +struct LogSourceLocation +{ + gsl::czstring fmt; + SourceLocation source_location; + + // NOLINTNEXTLINE(*-explicit-conversions) + LogSourceLocation(gsl::czstring fmt_, const SourceLocation& source_location_ = {}) : + fmt{fmt_}, source_location{source_location_} + {} +}; + +class LogApi +{ +public: + static constexpr IdName kApiName{"deimos::LogApi"}; + + LogApi() = default; + deimos_NO_COPY_MOVE(LogApi); + virtual ~LogApi() = default; + + virtual void LogVa(LogSeverity, const SourceLocation&, gsl::czstring msg, Span) = 0; + + template + void LogInfo(const LogSourceLocation& fmt_source_location, Args&&... args) + { + LogVa(LogSeverity::kInfo, fmt_source_location.source_location, fmt_source_location.fmt, { FormatArg(std::forward(args))... }); + } + + template + void LogDebug(const LogSourceLocation& fmt_source_location, Args&&... args) + { + LogVa(LogSeverity::kDebug, fmt_source_location.source_location, fmt_source_location.fmt, { FormatArg(std::forward(args))... }); + } + + template + void LogError(const LogSourceLocation& fmt_source_location, Args&&... args) + { + LogVa(LogSeverity::kError, fmt_source_location.source_location, fmt_source_location.fmt, { FormatArg(std::forward(args))... }); + } +}; + +} // namespace deimos + -- cgit