3#include <litefx/config.h>
5#if !defined (LITEFX_LOGGING_API)
6# if defined(LiteFX_Logging_EXPORTS) && (defined _WIN32 || defined WINCE)
7# define LITEFX_LOGGING_API __declspec(dllexport)
8# elif (defined(LiteFX_Logging_EXPORTS) || defined(__APPLE__)) && defined __GNUC__ && __GNUC__ >= 4
9# define LITEFX_LOGGING_API __attribute__ ((visibility ("default")))
10# elif !defined(LiteFX_Logging_EXPORTS) && (defined _WIN32 || defined WINCE)
11# define LITEFX_LOGGING_API __declspec(dllimport)
15#ifndef LITEFX_LOGGING_API
16# define LITEFX_LOGGING_API
19#include <litefx/core.h>
20#include <spdlog/spdlog.h>
21#include <spdlog/sinks/sink.h>
36 Off = SPDLOG_LEVEL_OFF,
43 class LITEFX_LOGGING_API
ISink {
52 virtual ~
ISink() noexcept = default;
71 virtual spdlog::sink_ptr
get() const = 0;
105 spdlog::sink_ptr
get() const override;
160 spdlog::sink_ptr
get() const override;
204 spdlog::sink_ptr
get() const override;
214 class LITEFX_LOGGING_API
Log {
215 LITEFX_IMPLEMENTATION(
LogImpl);
227 auto operator=(
Log&&) noexcept = delete;
228 auto operator=(const
Log&) = delete;
245 template<typename ...TArgs>
246 inline
void log(
LogLevel level, std::format_string<TArgs...> format, TArgs&&... args) {
247 this->
log(level, std::format(format, std::forward<TArgs>(args)...));
254 template<
typename ...TArgs>
255 inline void trace([[maybe_unused]] std::format_string<TArgs...> format, [[maybe_unused]] TArgs&&... args) {
265 template<
typename ...TArgs>
266 inline void debug([[maybe_unused]] std::format_string<TArgs...> format, [[maybe_unused]] TArgs&&... args) {
276 template<
typename ...TArgs>
277 inline void info(std::format_string<TArgs...> format, TArgs&&... args) {
285 template<
typename ...TArgs>
286 inline void warning(std::format_string<TArgs...> format, TArgs&&... args) {
294 template<
typename ...TArgs>
295 inline void error(std::format_string<TArgs...> format, TArgs&&... args) {
303 template<
typename ...TArgs>
304 inline void fatal(std::format_string<TArgs...> format, TArgs&&... args) {
312 class LITEFX_LOGGING_API Logger {
319 Logger(Logger&&) noexcept = delete;
320 Logger(const Logger&) = delete;
321 auto operator=(const Logger&) = delete;
322 auto operator=(Logger&&) noexcept = delete;
346#define LITEFX_TRACE(log, format, ...) LiteFX::Logging::Logger::get(log).trace(format, ##__VA_ARGS__)
347#define LITEFX_DEBUG(log, format, ...) LiteFX::Logging::Logger::get(log).debug(format, ##__VA_ARGS__)
349#define LITEFX_TRACE(log, format, ...)
350#define LITEFX_DEBUG(log, format, ...)
353#define LITEFX_INFO(log, format, ...) LiteFX::Logging::Logger::get(log).info(format, ##__VA_ARGS__)
354#define LITEFX_WARNING(log, format, ...) LiteFX::Logging::Logger::get(log).warning(format, ##__VA_ARGS__)
355#define LITEFX_ERROR(log, format, ...) LiteFX::Logging::Logger::get(log).error(format, ##__VA_ARGS__)
356#define LITEFX_FATAL_ERROR(log, format, ...) LiteFX::Logging::Logger::get(log).fatal(format, ##__VA_ARGS__)
Definition console.cpp:10
spdlog::sink_ptr get() const override
Definition console.cpp:55
String getPattern() const override
Gets the pattern used to format the messages for the sink.
Definition console.cpp:50
~ConsoleSink() noexcept override
LogLevel getLevel() const override
Gets the minimum log level for messages to get written to the log.
Definition console.cpp:45
ConsoleSink(LogLevel level=LogLevel::Info, const String &pattern="%+")
Creates a new console sink instance.
Definition console.cpp:33
String getName() const override
Gets the name of the sink.
Definition console.cpp:40
Interface for a class that receives log messages.
Definition logging.hpp:43
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.
friend class Logger
Definition logging.hpp:70
A log to which messages are written to.
Definition logging.hpp:214
void debug(std::format_string< TArgs... > format, TArgs &&... args)
Logs a debug message with format .
Definition logging.hpp:266
void trace(std::format_string< TArgs... > format, TArgs &&... args)
Logs a trace message with format .
Definition logging.hpp:255
void error(std::format_string< TArgs... > format, TArgs &&... args)
Logs an error message with format .
Definition logging.hpp:295
virtual void log(LogLevel level, StringView message)
Definition logger.cpp:38
Log(const String &name)
Creates a new log instance.
Definition logger.cpp:26
void fatal(std::format_string< TArgs... > format, TArgs &&... args)
Logs a fatal error message with format .
Definition logging.hpp:304
void warning(std::format_string< TArgs... > format, TArgs &&... args)
Logs a warning message with format .
Definition logging.hpp:286
virtual const String & getName() const noexcept
Gets the name of the logger.
Definition logger.cpp:33
void info(std::format_string< TArgs... > format, TArgs &&... args)
Logs an info message with format .
Definition logging.hpp:277
virtual ~Logger() noexcept
static void sinkTo(const ISink *sink)
Allows a log to write messages to sink .
Definition logger_factory.cpp:38
static Log get(StringView name)
Retrieves a log from name .
Definition logger_factory.cpp:14
virtual int getMaxFiles() const
Returns the maximum number of log files to keep.
Definition rolling_file.cpp:68
String getPattern() const override
Gets the pattern used to format the messages for the sink.
Definition rolling_file.cpp:58
virtual String getFileName() const
Gets the file name of the log file.
Definition rolling_file.cpp:53
RollingFileSink(const String &fileName, LogLevel level=LogLevel::Info, const String &pattern="%+", bool truncate=false, int maxFiles=0)
Creates a new rolling file sink instance.
Definition rolling_file.cpp:36
virtual bool getTruncate() const
Returns true, if the log messages should be truncated to the contents of the file,...
Definition rolling_file.cpp:63
LogLevel getLevel() const override
Gets the minimum log level for messages to get written to the log.
Definition rolling_file.cpp:48
~RollingFileSink() noexcept override
spdlog::sink_ptr get() const override
Definition rolling_file.cpp:73
String getName() const override
Gets the name of the sink.
Definition rolling_file.cpp:43
static constexpr std::uint32_t DEFAULT_TERMINATION_STATUS
The default status code used to exit the application.
Definition logging.hpp:177
TerminationSink(const LogLevel &level=LogLevel::Info, int status=static_cast< int >(DEFAULT_TERMINATION_STATUS))
Creates a new exception sink instance.
Definition termination.cpp:73
String getName() const override
Gets the name of the sink.
Definition termination.cpp:80
String getPattern() const override
Gets the pattern used to format the messages for the sink.
Definition termination.cpp:90
LogLevel getLevel() const override
Gets the minimum log level for messages to get written to the log.
Definition termination.cpp:85
~TerminationSink() noexcept override
spdlog::sink_ptr get() const override
Definition termination.cpp:95
Definition rolling_file.cpp:11
Definition termination.cpp:52
Definition logging.hpp:23
LogLevel
Defines the various log levels.
Definition logging.hpp:29
@ Warning
Definition logging.hpp:33
@ Info
Definition logging.hpp:32
@ Invalid
Definition logging.hpp:37
@ Fatal
Definition logging.hpp:35
@ Error
Definition logging.hpp:34
@ Debug
Definition logging.hpp:31
@ Off
Definition logging.hpp:36
@ Trace
Definition logging.hpp:30
std::string String
Definition string.hpp:24
std::string_view StringView
Definition string.hpp:26