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<size_t>(Level::count)> level_strings = { "TRACE", "INFO", "WARN", "ERROR",
27 "CRITICAL" };
28
29inline constexpr const char* ToString(Level level)
30{
31 return level_strings[static_cast<size_t>(level)];
32}
33
35{
36 std::filesystem::path root_directory;
37 std::size_t retained_session_count = 20;
38 bool allow_environment_override = true;
39};
40
42{
43 std::string timestamp_iso8601;
44 std::string elapsed;
45 uint32_t process_id = 0;
46 std::string thread_id;
47 Level severity = Level::Info;
48 std::string logger_name;
49 std::string message;
50};
51
52class Logger
53{
54public:
55 explicit Logger(std::string name);
56
57 [[nodiscard]] const std::string& GetName() const
58 {
59 return name_;
60 }
61
62 template <typename... Args>
63 void Trace(fmt::format_string<Args...> fmtStr, Args&&... args) const
64 {
65 Write(Level::Trace, fmt::format(fmtStr, std::forward<Args>(args)...));
66 }
67
68 template <typename... Args>
69 void Info(fmt::format_string<Args...> fmtStr, Args&&... args) const
70 {
71 Write(Level::Info, fmt::format(fmtStr, std::forward<Args>(args)...));
72 }
73
74 template <typename... Args>
75 void Warn(fmt::format_string<Args...> fmtStr, Args&&... args) const
76 {
77 Write(Level::Warn, fmt::format(fmtStr, std::forward<Args>(args)...));
78 }
79
80 template <typename... Args>
81 void Error(fmt::format_string<Args...> fmtStr, Args&&... args) const
82 {
83 Write(Level::Error, fmt::format(fmtStr, std::forward<Args>(args)...));
84 }
85
86 template <typename... Args>
87 void Critical(fmt::format_string<Args...> fmtStr, Args&&... args) const
88 {
89 Write(Level::Critical, fmt::format(fmtStr, std::forward<Args>(args)...));
90 }
91
92private:
93 void Write(Level level, std::string message) const;
94
95 std::string name_;
96};
97} // namespace pixelbullet::log
98
99namespace pixelbullet
100{
103} // namespace pixelbullet
104
105namespace pixelbullet::logging
106{
107namespace names
108{
109inline constexpr std::string_view core = "core";
110inline constexpr std::string_view core_app = "core.app";
111inline constexpr std::string_view core_graphics = "core.graphics";
112inline constexpr std::string_view core_assets = "core.assets";
113inline constexpr std::string_view core_shader = "core.shader";
114
115inline constexpr std::string_view client = "client";
116
117inline constexpr std::string_view vulkan_validation = "vulkan.validation";
118} // namespace names
119
120void Initialize(const ApplicationBootstrap& bootstrap, const log::LogConfig& config = {});
121void Shutdown();
122
123[[nodiscard]] bool IsInitialized();
124[[nodiscard]] std::shared_ptr<log::Logger> Get(std::string_view name);
125
126void UpdateSessionMetadata(const ApplicationSpecification& specification);
127
128[[nodiscard]] std::filesystem::path GetLogRoot();
129[[nodiscard]] std::filesystem::path GetSessionDirectory();
130[[nodiscard]] std::filesystem::path GetLatestManifestPath();
131} // namespace pixelbullet::logging
132
133namespace pixelbullet::log
134{
135template <typename... Args>
136void Trace(fmt::format_string<Args...> fmtStr, Args&&... args)
137{
138 logging::Get(logging::names::core)->Trace(fmtStr, std::forward<Args>(args)...);
139}
140
141template <typename... Args>
142void Info(fmt::format_string<Args...> fmtStr, Args&&... args)
143{
144 logging::Get(logging::names::core)->Info(fmtStr, std::forward<Args>(args)...);
145}
146
147template <typename... Args>
148void Warn(fmt::format_string<Args...> fmtStr, Args&&... args)
149{
150 logging::Get(logging::names::core)->Warn(fmtStr, std::forward<Args>(args)...);
151}
152
153template <typename... Args>
154void Error(fmt::format_string<Args...> fmtStr, Args&&... args)
155{
156 logging::Get(logging::names::core)->Error(fmtStr, std::forward<Args>(args)...);
157}
158
159template <typename... Args>
160void Critical(fmt::format_string<Args...> fmtStr, Args&&... args)
161{
162 logging::Get(logging::names::core)->Critical(fmtStr, std::forward<Args>(args)...);
163}
164} // namespace pixelbullet::log
Definition log.h:53
Definition specification.h:62
Contains configuration options for both the game and the engine.
Definition specification.h:145
Definition log.h:35
Definition log.h:42