Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
log.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4
5#include <format>
6#include <string_view>
7#include <utility>
8
9namespace Flint {
10
11 enum class LOG_LEVEL {
12 Trace,
13 Debug,
14 Info,
15 Warn,
16 Error,
17 Critical,
18 Off
19 };
20
21 enum class LOG_CHANNEL {
22 Engine,
23 Application
24 };
25
26 class Log {
27 public:
28 static void init(std::string_view applicationName, LOG_LEVEL engineLevel,
29 LOG_LEVEL applicationLevel);
30
31 [[nodiscard]] static bool isInitialized();
32
33 static void setLevel(LOG_CHANNEL channel, LOG_LEVEL level);
34
35 template <typename... Args>
36 static void engine(LOG_LEVEL level, std::format_string<Args...> format, Args &&...args) {
37 if (!shouldLog(LOG_CHANNEL::Engine, level)) {
38 return;
39 }
40
41 write(LOG_CHANNEL::Engine, level, std::format(format, std::forward<Args>(args)...));
42 }
43
44 template <typename... Args>
45 static void application(LOG_LEVEL level, std::format_string<Args...> format, Args &&...args) {
46 if (!shouldLog(LOG_CHANNEL::Application, level)) {
47 return;
48 }
49
50 write(LOG_CHANNEL::Application, level, std::format(format, std::forward<Args>(args)...));
51 }
52
53 private:
54 [[nodiscard]] static bool shouldLog(LOG_CHANNEL channel, LOG_LEVEL level);
55 static void write(LOG_CHANNEL channel, LOG_LEVEL level, std::string_view message);
56 };
57
58}
59
60#define FLINT_ENGINE_TRACE(...) ::Flint::Log::engine(::Flint::LOG_LEVEL::Trace, __VA_ARGS__)
61
62#define FLINT_ENGINE_DEBUG(...) ::Flint::Log::engine(::Flint::LOG_LEVEL::Debug, __VA_ARGS__)
63
64#define FLINT_ENGINE_INFO(...) ::Flint::Log::engine(::Flint::LOG_LEVEL::Info, __VA_ARGS__)
65
66#define FLINT_ENGINE_WARN(...) ::Flint::Log::engine(::Flint::LOG_LEVEL::Warn, __VA_ARGS__)
67
68#define FLINT_ENGINE_ERROR(...) ::Flint::Log::engine(::Flint::LOG_LEVEL::Error, __VA_ARGS__)
69
70#define FLINT_ENGINE_CRITICAL(...) ::Flint::Log::engine(::Flint::LOG_LEVEL::Critical, __VA_ARGS__)
71
72#define FLINT_APP_TRACE(...) ::Flint::Log::application(::Flint::LOG_LEVEL::Trace, __VA_ARGS__)
73
74#define FLINT_APP_DEBUG(...) ::Flint::Log::application(::Flint::LOG_LEVEL::Debug, __VA_ARGS__)
75
76#define FLINT_APP_INFO(...) ::Flint::Log::application(::Flint::LOG_LEVEL::Info, __VA_ARGS__)
77
78#define FLINT_APP_WARN(...) ::Flint::Log::application(::Flint::LOG_LEVEL::Warn, __VA_ARGS__)
79
80#define FLINT_APP_ERROR(...) ::Flint::Log::application(::Flint::LOG_LEVEL::Error, __VA_ARGS__)
81
82#define FLINT_APP_CRITICAL(...) ::Flint::Log::application(::Flint::LOG_LEVEL::Critical, __VA_ARGS__)