Move Singleton lifetime + refactor logs

This commit is contained in:
2026-05-16 15:41:05 +02:00
parent e3c55cc359
commit 2c83dcc0bc
18 changed files with 258 additions and 220 deletions
@@ -6,7 +6,7 @@
*********************************************************************/
#ifndef BIGFOOT_UTILS_ASSERT_HPP
#define BIGFOOT_UTILS_ASSERT_HPP
#include <Utils/Log/Log.hpp>
#include <Utils/Log/LogMacros.hpp>
#if defined BIGFOOT_NOT_OPTIMIZED
@@ -10,7 +10,6 @@
#if defined BIGFOOT_NOT_OPTIMIZED
#include <Utils/Log/Log_generated.hpp>
#include <Utils/Singleton.hpp>
#include <EASTL/array.h>
@@ -92,96 +91,5 @@ class Log
eastl::array<std::shared_ptr<quill::Sink>, 1> m_sinks;
};
} // namespace Bigfoot
#define BIGFOOT_LOG_DEBUG(loggerName, fmt, ...) \
do \
{ \
if (quill::Logger* logger = Bigfoot::Singleton<Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_DEBUG(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_DEBUG(Bigfoot::Singleton<Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_TRACE(loggerName, fmt, ...) \
do \
{ \
if (quill::Logger* logger = Bigfoot::Singleton<Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_TRACE_L3(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_TRACE_L3(Bigfoot::Singleton<Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_INFO(loggerName, fmt, ...) \
do \
{ \
if (quill::Logger* logger = Bigfoot::Singleton<Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_INFO(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_INFO(Bigfoot::Singleton<Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_WARN(loggerName, fmt, ...) \
do \
{ \
if (quill::Logger* logger = Bigfoot::Singleton<Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_WARNING(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_WARNING(Bigfoot::Singleton<Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_ERROR(loggerName, fmt, ...) \
do \
{ \
if (quill::Logger* logger = Bigfoot::Singleton<Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_ERROR(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_ERROR(Bigfoot::Singleton<Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_FATAL(loggerName, fmt, ...) \
do \
{ \
if (quill::Logger* logger = Bigfoot::Singleton<Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_CRITICAL(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_CRITICAL(Bigfoot::Singleton<Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#else
#define BIGFOOT_LOG_DEBUG(loggerName, fmt, ...)
#define BIGFOOT_LOG_TRACE(loggerName, fmt, ...)
#define BIGFOOT_LOG_INFO(loggerName, fmt, ...)
#define BIGFOOT_LOG_WARN(loggerName, fmt, ...)
#define BIGFOOT_LOG_ERROR(loggerName, fmt, ...)
#define BIGFOOT_LOG_FATAL(loggerName, fmt, ...)
#endif
#endif
@@ -0,0 +1,104 @@
/*********************************************************************
* \file LogMacros.hpp
*
* \author Romain BOULLARD
* \date May 2026
*********************************************************************/
#ifndef BIGFOOT_UTILS_LOG_LOGMACROS_HPP
#define BIGFOOT_UTILS_LOG_LOGMACROS_HPP
#include <Utils/Log/Log.hpp>
#include <Utils/Singleton.hpp>
#if defined BIGFOOT_NOT_OPTIMIZED
#define BIGFOOT_LOG_DEBUG(loggerName, fmt, ...) \
do \
{ \
if (::quill::Logger* logger = ::Bigfoot::Singleton<::Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_DEBUG(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_DEBUG(::Bigfoot::Singleton<::Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_TRACE(loggerName, fmt, ...) \
do \
{ \
if (::quill::Logger* logger = ::Bigfoot::Singleton<::Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_TRACE_L3(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_TRACE_L3(::Bigfoot::Singleton<::Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_INFO(loggerName, fmt, ...) \
do \
{ \
if (::quill::Logger* logger = ::Bigfoot::Singleton<::Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_INFO(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_INFO(::Bigfoot::Singleton<::Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_WARN(loggerName, fmt, ...) \
do \
{ \
if (::quill::Logger* logger = ::Bigfoot::Singleton<::Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_WARNING(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_WARNING(::Bigfoot::Singleton<::Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_ERROR(loggerName, fmt, ...) \
do \
{ \
if (::quill::Logger* logger = ::Bigfoot::Singleton<::Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_ERROR(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_ERROR(::Bigfoot::Singleton<::Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define BIGFOOT_LOG_FATAL(loggerName, fmt, ...) \
do \
{ \
if (::quill::Logger* logger = ::Bigfoot::Singleton<::Bigfoot::Log>::Instance().GetLogger(loggerName)) \
{ \
QUILL_LOG_CRITICAL(logger, fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
else \
{ \
QUILL_LOG_CRITICAL(::Bigfoot::Singleton<::Bigfoot::Log>::Instance().RegisterLogger(loggerName), \
fmt __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#else
#define BIGFOOT_LOG_DEBUG(loggerName, fmt, ...)
#define BIGFOOT_LOG_TRACE(loggerName, fmt, ...)
#define BIGFOOT_LOG_INFO(loggerName, fmt, ...)
#define BIGFOOT_LOG_WARN(loggerName, fmt, ...)
#define BIGFOOT_LOG_ERROR(loggerName, fmt, ...)
#define BIGFOOT_LOG_FATAL(loggerName, fmt, ...)
#endif
#endif
@@ -6,7 +6,7 @@
*********************************************************************/
#ifndef BIGFOOT_@LOGGER_FILENAME_UPPER@_GENERATED_HPP
#define BIGFOOT_@LOGGER_FILENAME_UPPER@_GENERATED_HPP
#include <Utils/Log/Log.hpp>
#include <Utils/Log/LogMacros.hpp>
#if defined BIGFOOT_NOT_OPTIMIZED
@@ -6,7 +6,6 @@
*********************************************************************/
#ifndef BIGFOOT_UTILS_SINGLETON_HPP
#define BIGFOOT_UTILS_SINGLETON_HPP
#include <EASTL/array.h>
#include <EASTL/bit.h>
#include <EASTL/optional.h>
@@ -52,6 +51,9 @@ class Singleton
/**
* Constructor.
*
* Initializes the singleton; this lifetime owns the instance and
* finalizes it on destruction.
*
* \param p_args Arguments for the singleton
*/
template<typename... ARGS>
@@ -60,16 +62,53 @@ class Singleton
Initialize(std::forward<ARGS>(p_args)...);
}
struct EmptyLifetime
{
};
static inline constexpr EmptyLifetime EMPTY_LIFETIME;
/**
* Non-owning constructor.
*
* Does not initialize the singleton and owns nothing. Such a lifetime
* only becomes an owner if it later receives ownership through move
* assignment.
*/
explicit Lifetime(EmptyLifetime) noexcept:
m_owner(false)
{
}
Lifetime(const Lifetime& p_lifetime) = delete;
Lifetime(Lifetime&& p_lifetime) = delete;
explicit Lifetime(Lifetime&& p_lifetime) noexcept:
m_owner(eastl::exchange(p_lifetime.m_owner, false))
{
}
~Lifetime()
{
Finalize();
if (m_owner)
{
Finalize();
}
}
Lifetime& operator=(const Lifetime& p_lifetime) = delete;
Lifetime& operator=(Lifetime&& p_lifetime) = delete;
Lifetime& operator=(Lifetime&& p_lifetime) noexcept
{
m_owner = eastl::exchange(p_lifetime.m_owner, false);
return *this;
}
private:
/**
* Whether this lifetime owns the singleton instance or not.
*/
bool m_owner = true;
};
Singleton& operator=(const Singleton& p_singleton) = delete;
@@ -7,15 +7,12 @@
#ifndef BIGFOOT_UTILS_UTILSASSERTHANDLER_HPP
#define BIGFOOT_UTILS_UTILSASSERTHANDLER_HPP
#include <Utils/Assert.hpp>
#include <Utils/Log/Log.hpp>
#include <Utils/UtilsLogger_generated.hpp>
#if defined BIGFOOT_NOT_OPTIMIZED
#include <EASTL/utility.h>
#include <format>
#include <source_location>
#include <string_view>
namespace Bigfoot
{
@@ -6,7 +6,7 @@
*********************************************************************/
#ifndef BIGFOOT_UTILSLOGGER_GENERATED_HPP
#define BIGFOOT_UTILSLOGGER_GENERATED_HPP
#include <Utils/Log/Log.hpp>
#include <Utils/Log/LogMacros.hpp>
#if defined BIGFOOT_NOT_OPTIMIZED