LiteFX 0.3.1.2022
Computer Graphics Engine
logging.hpp
1#pragma once
2
3#if !defined (LITEFX_LOGGING_API)
4# if defined(LiteFX_Logging_EXPORTS) && (defined _WIN32 || defined WINCE)
5# define LITEFX_LOGGING_API __declspec(dllexport)
6# elif (defined(LiteFX_Logging_EXPORTS) || defined(__APPLE__)) && defined __GNUC__ && __GNUC__ >= 4
7# define LITEFX_LOGGING_API __attribute__ ((visibility ("default")))
8# elif !defined(LiteFX_Logging_EXPORTS) && (defined _WIN32 || defined WINCE)
9# define LITEFX_LOGGING_API __declspec(dllimport)
10# endif
11#endif
12
13#ifndef LITEFX_LOGGING_API
14# define LITEFX_LOGGING_API
15#endif
16
17#include <litefx/core.h>
18#include <fmt/core.h>
19#include <spdlog/spdlog.h>
20#include <spdlog/sinks/sink.h>
21
22namespace LiteFX::Logging {
23 using namespace LiteFX;
24
25 enum class LITEFX_LOGGING_API LogLevel {
26 Trace = SPDLOG_LEVEL_TRACE,
27 Debug = SPDLOG_LEVEL_DEBUG,
28 Info = SPDLOG_LEVEL_INFO,
29 Warning = SPDLOG_LEVEL_WARN,
30 Error = SPDLOG_LEVEL_ERROR,
31 Fatal = SPDLOG_LEVEL_CRITICAL,
32 Off = SPDLOG_LEVEL_OFF,
33 Invalid = 0xFF
34 };
35
36 class LITEFX_LOGGING_API ISink {
37 public:
41 virtual LogLevel getLevel() const = 0;
42
46 virtual String getName() const = 0;
47
51 virtual String getPattern() const = 0;
52
53 protected:
54 friend class Logger;
55 virtual spdlog::sink_ptr get() const = 0;
56 };
57
58 class LITEFX_LOGGING_API ConsoleSink : public ISink {
59 LITEFX_IMPLEMENTATION(ConsoleSinkImpl);
60
61 public:
62 ConsoleSink(const LogLevel& level = LogLevel::Info, const String& pattern = "%+");
63 ConsoleSink(const ConsoleSink&) = delete;
65 virtual ~ConsoleSink() noexcept;
66
67 public:
69 virtual LogLevel getLevel() const override;
70
72 virtual String getName() const override;
73
75 virtual String getPattern() const override;
76
77 protected:
78 virtual spdlog::sink_ptr get() const override;
79 };
80
81 class LITEFX_LOGGING_API RollingFileSink : public ISink {
82 LITEFX_IMPLEMENTATION(RollingFileSinkImpl);
83
84 public:
85 RollingFileSink(const String& fileName, const LogLevel& level = LogLevel::Info, const String& pattern = "%+", const bool& truncate = false, const int& maxFiles = 0);
88 virtual ~RollingFileSink() noexcept;
89
90 public:
92 virtual LogLevel getLevel() const override;
93
95 virtual String getName() const override;
96
98 virtual String getPattern() const override;
99
100 virtual String getFileName() const;
101
102 virtual bool getTruncate() const;
103
104 virtual int getMaxFiles() const;
105
106 protected:
107 virtual spdlog::sink_ptr get() const override;
108 };
109
110 class LITEFX_LOGGING_API Log {
111 LITEFX_IMPLEMENTATION(LogImpl);
112
113 public:
114 Log(const String& name);
115 Log(Log&&) = delete;
116 Log(const Log&) = delete;
117 virtual ~Log() noexcept;
118
119 public:
123 virtual inline const String& getName() const noexcept;
124
125 protected:
126 virtual void log(const LogLevel& level, StringView message);
127
128 public:
129 template<typename ...TArgs>
130 inline void log(const LogLevel& level, StringView format, TArgs&&... args) {
131 this->log(level, fmt::format(fmt::runtime(format), std::forward<TArgs>(args)...));
132 }
133
134 template<typename ...TArgs>
135 inline void trace(StringView format, TArgs&&... args) {
136#ifndef NDEBUG
137 this->log(LogLevel::Trace, format, std::forward<TArgs>(args)...);
138#endif
139 }
140
141 template<typename ...TArgs>
142 inline void debug(StringView format, TArgs&&... args) {
143#ifndef NDEBUG
144 this->log(LogLevel::Debug, format, std::forward<TArgs>(args)...);
145#endif
146 }
147
148 template<typename ...TArgs>
149 inline void info(StringView format, TArgs&&... args) {
150 this->log(LogLevel::Info, format, std::forward<TArgs>(args)...);
151 }
152
153 template<typename ...TArgs>
154 inline void warning(StringView format, TArgs&&... args) {
155 this->log(LogLevel::Warning, format, std::forward<TArgs>(args)...);
156 }
157
158 template<typename ...TArgs>
159 inline void error(StringView format, TArgs&&... args) {
160 this->log(LogLevel::Error, format, std::forward<TArgs>(args)...);
161 }
162
163 template<typename ...TArgs>
164 inline void fatal(StringView format, TArgs&&... args) {
165 this->log(LogLevel::Fatal, format, std::forward<TArgs>(args)...);
166 }
167 };
168
169 class LITEFX_LOGGING_API Logger {
170 LITEFX_IMPLEMENTATION(LoggerImpl);
171
172 public:
173 Logger(Logger&&) = delete;
174 Logger(const Logger&) = delete;
175 Logger& operator=(const Logger&) = delete;
176 virtual ~Logger() noexcept;
177
178 private:
179 Logger() noexcept;
180
181 public:
182 // TODO: Cache logs by name and return them, instead of re-creating them with each call.
183 static Log get(StringView name);
184 static void sinkTo(const ISink* sink);
185 };
186
187}
188
189#ifndef NDEBUG
190#define LITEFX_TRACE(log, format, ...) LiteFX::Logging::Logger::get(log).trace(format, __VA_ARGS__)
191#define LITEFX_DEBUG(log, format, ...) LiteFX::Logging::Logger::get(log).debug(format, __VA_ARGS__)
192#else
193#define LITEFX_TRACE(log, format, ...)
194#define LITEFX_DEBUG(log, format, ...)
195#endif
196
197#define LITEFX_INFO(log, format, ...) LiteFX::Logging::Logger::get(log).info(format, __VA_ARGS__)
198#define LITEFX_WARNING(log, format, ...) LiteFX::Logging::Logger::get(log).warning(format, __VA_ARGS__)
199#define LITEFX_ERROR(log, format, ...) LiteFX::Logging::Logger::get(log).error(format, __VA_ARGS__)
200#define LITEFX_FATAL_ERROR(log, format, ...) LiteFX::Logging::Logger::get(log).fatal(format, __VA_ARGS__)
Definition: logging.hpp:58
virtual ~ConsoleSink() noexcept
ConsoleSink(const ConsoleSink &)=delete
ConsoleSink(ConsoleSink &&)=delete
Definition: logging.hpp:36
virtual String getName() const =0
Gets the name of the sink.
virtual spdlog::sink_ptr get() const =0
virtual String getPattern() const =0
Gets the pattern used to format the messages for the sink.
virtual LogLevel getLevel() const =0
Gets the minimum log level for messages to get written to the log.
Definition: logging.hpp:110
Log(const Log &)=delete
void error(StringView format, TArgs &&... args)
Definition: logging.hpp:159
void fatal(StringView format, TArgs &&... args)
Definition: logging.hpp:164
void debug(StringView format, TArgs &&... args)
Definition: logging.hpp:142
Log(Log &&)=delete
virtual ~Log() noexcept
void trace(StringView format, TArgs &&... args)
Definition: logging.hpp:135
void warning(StringView format, TArgs &&... args)
Definition: logging.hpp:154
void info(StringView format, TArgs &&... args)
Definition: logging.hpp:149
Definition: logging.hpp:169
virtual ~Logger() noexcept
Logger(Logger &&)=delete
Logger(const Logger &)=delete
Logger & operator=(const Logger &)=delete
Definition: logging.hpp:81
RollingFileSink(const RollingFileSink &)=delete
virtual ~RollingFileSink() noexcept
RollingFileSink(RollingFileSink &&)=delete
Definition: logger.cpp:10
Definition: logging.hpp:22
Definition: app.hpp:6
std::string String
Definition: string.hpp:19
std::string_view StringView
Definition: string.hpp:21
auto format(LiteFX::Platform t, FormatContext &ctx) const
Definition: app_formatters.hpp:8