SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Logger.hpp
Go to the documentation of this file.
1#ifndef _LOGGER_H_
2#define _LOGGER_H_
3
4#include <Core/OSDef.hpp>
5
6#define SPDLOG_WCHAR_SUPPORT
7
8#include <spdlog/async.h>
9#include <spdlog/sinks/basic_file_sink.h>
10#include <spdlog/sinks/stdout_color_sinks.h>
11#include <spdlog/spdlog.h>
12
13#ifdef SLEAK_ENGINE
14#define _LOGGER Sleak::Logger::GetCoreLogger()
15#else
16#define _LOGGER Sleak::Logger::GetLogger()
17#endif
18
19#define SLEAK_LOG(...) _LOGGER->trace(__VA_ARGS__);
20#define SLEAK_INFO(...) _LOGGER->info(__VA_ARGS__);
21#define SLEAK_WARN(...) _LOGGER->warn(__VA_ARGS__);
22#define SLEAK_ERROR(...) _LOGGER->error(__VA_ARGS__);
23#define SLEAK_FATAL(...) _LOGGER->critical(__VA_ARGS__);
24
25#define SLEAK_RETURN_ERR(...) \
26 { \
27 SLEAK_ERROR(__VA_ARGS__) \
28 return false; \
29 }
30
31namespace Sleak {
32
33/// Sets up the engine and game spdlog sinks. Access via the SLEAK_LOG /
34/// SLEAK_INFO / SLEAK_WARN / SLEAK_ERROR macros rather than directly.
35///
36/// Call Init() once from `main()` before anything else logs, passing your
37/// project name. It creates a colored console sink and a file sink, and
38/// registers two loggers behind them: one for engine internals and one for
39/// game code. The macros pick the right logger automatically based on
40/// which target is compiling, so game code and engine code both just use
41/// SLEAK_LOG and friends.
42///
43/// Messages use fmt-style `{}` placeholders, not printf specifiers. The
44/// five levels, from quietest to loudest, are SLEAK_LOG (trace),
45/// SLEAK_INFO, SLEAK_WARN, SLEAK_ERROR, and SLEAK_FATAL. SLEAK_RETURN_ERR
46/// logs at error level and returns false, which suits initialization
47/// paths.
48///
49/// @code{.cpp}
50/// int main(int argc, char** argv) {
51/// Sleak::CommandLine::Parse(argc, argv);
52/// Sleak::Logger::Init("MyGame");
53/// // ...
54/// }
55///
56/// // Anywhere afterward
57/// SLEAK_INFO("Loaded {} chunks in {:.2f} ms", count, elapsedMs);
58/// SLEAK_WARN("Texture {} is not power of two", path);
59///
60/// bool LoadLevel(const std::string& path) {
61/// if (!Exists(path)) SLEAK_RETURN_ERR("Missing level {}", path);
62/// return true;
63/// }
64/// @endcode
65///
66/// @see Application, CommandLine
67/// @ingroup core
68class ENGINE_API Logger {
69 public:
70 /// Creates the console + file sinks and registers both loggers. Call once at startup.
71 static void Init(const std::string& ProjectName);
72
73 /// Logger used by engine code (SLEAK_ENGINE builds).
74 static std::shared_ptr<spdlog::logger>& GetCoreLogger();
75 /// Logger used by game code.
76 static std::shared_ptr<spdlog::logger>& GetLogger();
77
78 private:
79 static std::shared_ptr<spdlog::logger> CoreLogger;
80 static std::shared_ptr<spdlog::logger> GameLogger;
81};
82
83} // namespace Sleak
84
85#endif // _LOGGER_H_
static std::shared_ptr< spdlog::logger > & GetLogger()
Logger used by game code.
Definition Logger.cpp:108
static std::shared_ptr< spdlog::logger > & GetCoreLogger()
Logger used by engine code (SLEAK_ENGINE builds).
Definition Logger.cpp:103
static void Init(const std::string &ProjectName)
Creates the console + file sinks and registers both loggers. Call once at startup.
Definition Logger.cpp:34
Root namespace for everything the engine exposes.
Definition Camera.hpp:10