PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
log.h
1#pragma once
2
3#include <fmt/format.h>
4
5#include <array>
6#include <cstddef>
7#include <cstdint>
8#include <filesystem>
9#include <memory>
10#include <string>
11#include <string_view>
12#include <utility>
13
14namespace pixelbullet::log
15{
16enum class Level
17{
18 Trace = 0,
19 Info,
20 Warn,
21 Error,
22 Critical,
23 count
24};
25
26inline constexpr std::array<const char*, static_cast<std::size_t>(Level::count)> level_strings = { "TRACE", "INFO", "WARN", "ERROR",
27 "CRITICAL" };
28
29inline constexpr const char* to_string(const Level level) noexcept
30{
31 const std::size_t index = static_cast<std::size_t>(level);
32 return index < level_strings.size() ? level_strings[index] : "UNKNOWN";
33}
34
36{
37 std::filesystem::path root_directory;
38 std::size_t retained_session_count = 20;
39 bool allow_environment_override = true;
40};
41
43{
44 std::string timestamp_iso8601;
45 std::string elapsed;
46 uint32_t process_id = 0;
47 std::string thread_id;
48 Level severity = Level::Info;
49 std::string logger_name;
50 std::string message;
51};
52
53class Logger
54{
55public:
56 explicit Logger(std::string name);
57
58 [[nodiscard]] const std::string& name() const
59 {
60 return name_;
61 }
62
63 template <typename... Args>
64 void trace(fmt::format_string<Args...> fmt_str, Args&&... args) const
65 {
66 write(Level::Trace, fmt::format(fmt_str, std::forward<Args>(args)...));
67 }
68
69 template <typename... Args>
70 void info(fmt::format_string<Args...> fmt_str, Args&&... args) const
71 {
72 write(Level::Info, fmt::format(fmt_str, std::forward<Args>(args)...));
73 }
74
75 template <typename... Args>
76 void warn(fmt::format_string<Args...> fmt_str, Args&&... args) const
77 {
78 write(Level::Warn, fmt::format(fmt_str, std::forward<Args>(args)...));
79 }
80
81 template <typename... Args>
82 void error(fmt::format_string<Args...> fmt_str, Args&&... args) const
83 {
84 write(Level::Error, fmt::format(fmt_str, std::forward<Args>(args)...));
85 }
86
87 template <typename... Args>
88 void critical(fmt::format_string<Args...> fmt_str, Args&&... args) const
89 {
90 write(Level::Critical, fmt::format(fmt_str, std::forward<Args>(args)...));
91 }
92
93private:
94 void write(Level level, std::string message) const;
95
96 std::string name_;
97};
98} // namespace pixelbullet::log
99
100namespace pixelbullet::logging
101{
103{
104 std::string binary_path;
105 std::filesystem::path log_directory_override;
106};
107
109{
110 std::string client_name;
111 std::string client_version;
112 std::string engine_name;
113 std::string engine_version;
114};
115
116namespace names
117{
118inline constexpr std::string_view core = "core";
119inline constexpr std::string_view core_app = "core.app";
120inline constexpr std::string_view core_graphics = "core.graphics";
121inline constexpr std::string_view core_image = "core.image";
122inline constexpr std::string_view core_assets = "core.assets";
123inline constexpr std::string_view core_shader = "core.shader";
124inline constexpr std::string_view core_ui = "core.ui";
125
126inline constexpr std::string_view client = "client";
127
128inline constexpr std::string_view vulkan_validation = "vulkan.validation";
129} // namespace names
130
131void initialize(const StartupInfo& startup_info, const log::LogConfig& config = {});
132void shutdown();
133
134[[nodiscard]] bool is_initialized();
135[[nodiscard]] std::shared_ptr<log::Logger> get(std::string_view name);
136
137void update_session_metadata(const SessionMetadata& metadata);
138
139[[nodiscard]] std::filesystem::path log_root();
140[[nodiscard]] std::filesystem::path session_directory();
141[[nodiscard]] std::filesystem::path latest_manifest_path();
142} // namespace pixelbullet::logging
143
144namespace pixelbullet::log
145{
146template <typename... Args>
147void trace(fmt::format_string<Args...> fmt_str, Args&&... args)
148{
149 logging::get(logging::names::core)->trace(fmt_str, std::forward<Args>(args)...);
150}
151
152template <typename... Args>
153void info(fmt::format_string<Args...> fmt_str, Args&&... args)
154{
155 logging::get(logging::names::core)->info(fmt_str, std::forward<Args>(args)...);
156}
157
158template <typename... Args>
159void warn(fmt::format_string<Args...> fmt_str, Args&&... args)
160{
161 logging::get(logging::names::core)->warn(fmt_str, std::forward<Args>(args)...);
162}
163
164template <typename... Args>
165void error(fmt::format_string<Args...> fmt_str, Args&&... args)
166{
167 logging::get(logging::names::core)->error(fmt_str, std::forward<Args>(args)...);
168}
169
170template <typename... Args>
171void critical(fmt::format_string<Args...> fmt_str, Args&&... args)
172{
173 logging::get(logging::names::core)->critical(fmt_str, std::forward<Args>(args)...);
174}
175} // namespace pixelbullet::log
Definition log.h:36
Definition log.h:43