Split error macro into two versions (format vs non-formating) to allow format checking at compile-time

This commit is contained in:
SirLynix
2023-11-02 15:18:03 +01:00
parent 8fb53f467b
commit 4b8a475bbd
133 changed files with 570 additions and 557 deletions

View File

@@ -105,7 +105,7 @@ namespace Nz
using Param = std::decay_t<decltype(arg)>;
if constexpr (std::is_base_of_v<VirtualDirectory::DirectoryEntry, Param>)
{
NazaraError("{} is a directory", assetPath);
NazaraErrorFmt("{} is a directory", assetPath);
return false;
}
else if constexpr (std::is_same_v<Param, VirtualDirectory::FileEntry>)
@@ -136,7 +136,7 @@ namespace Nz
using Param = std::decay_t<decltype(arg)>;
if constexpr (std::is_base_of_v<VirtualDirectory::DirectoryEntry, Param>)
{
NazaraError("{} is a directory", assetPath);
NazaraErrorFmt("{} is a directory", assetPath);
return false;
}
else if constexpr (std::is_same_v<Param, VirtualDirectory::FileEntry>)

View File

@@ -171,7 +171,7 @@ namespace Nz
OnEmptyStream();
if (!Unserialize(m_context, &value))
NazaraError("Failed to serialize value");
NazaraError("failed to serialize value");
return *this;
}
@@ -192,7 +192,7 @@ namespace Nz
OnEmptyStream();
if (!Serialize(m_context, value))
NazaraError("Failed to serialize value");
NazaraError("failed to serialize value");
return *this;
}

View File

@@ -14,14 +14,18 @@
#include <string>
#if NAZARA_CORE_ENABLE_ASSERTS || defined(NAZARA_DEBUG)
#define NazaraAssert(a, ...) if NAZARA_UNLIKELY(!(a)) Nz::Error::Trigger(Nz::ErrorType::AssertFailed, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, __VA_ARGS__)
#define NazaraAssert(a, err) if NAZARA_UNLIKELY(!(a)) Nz::Error::Trigger(Nz::ErrorType::AssertFailed, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, err)
#define NazaraAssertFmt(a, fmt, ...) if NAZARA_UNLIKELY(!(a)) Nz::Error::Trigger(Nz::ErrorType::AssertFailed, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, Nz::Format(NAZARA_FORMAT(fmt), __VA_ARGS__))
#else
#define NazaraAssert(a, ...) for (;;) break
#define NazaraAssertFmt(a, fmt, ...) for (;;) break
#endif
#define NazaraError(...) Nz::Error::Trigger(Nz::ErrorType::Normal, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, __VA_ARGS__)
#define NazaraInternalError(...) Nz::Error::Trigger(Nz::ErrorType::Internal, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, __VA_ARGS__)
#define NazaraWarning(...) Nz::Error::Trigger(Nz::ErrorType::Warning, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, __VA_ARGS__)
#define NazaraError(err) Nz::Error::Trigger(Nz::ErrorType::Normal, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, err)
#define NazaraErrorFmt(fmt, ...) Nz::Error::Trigger(Nz::ErrorType::Normal, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, Nz::Format(NAZARA_FORMAT(fmt), __VA_ARGS__))
#define NazaraInternalError(err) Nz::Error::Trigger(Nz::ErrorType::Internal, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, err)
#define NazaraInternalErrorFmt(fmt, ...) Nz::Error::Trigger(Nz::ErrorType::Internal, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, Nz::Format(NAZARA_FORMAT(fmt), __VA_ARGS__))
#define NazaraWarning(err) Nz::Error::Trigger(Nz::ErrorType::Warning, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, err)
#define NazaraWarningFmt(fmt, ...) Nz::Error::Trigger(Nz::ErrorType::Warning, __LINE__, __FILE__, NAZARA_PRETTY_FUNCTION, Nz::Format(NAZARA_FORMAT(fmt), __VA_ARGS__))
namespace Nz
{
@@ -41,8 +45,8 @@ namespace Nz
static ErrorModeFlags SetFlags(ErrorModeFlags flags);
template<typename... Args> static void Trigger(ErrorType type, std::string_view error, Args&&... args);
template<typename... Args> static void Trigger(ErrorType type, unsigned int line, std::string_view file, std::string_view function, std::string_view error, Args&&... args);
static inline void Trigger(ErrorType type, std::string error);
static inline void Trigger(ErrorType type, unsigned int line, std::string_view file, std::string_view function, std::string error);
private:
static void TriggerInternal(ErrorType type, std::string error, unsigned int line, std::string_view file, std::string_view function);

View File

@@ -15,15 +15,13 @@ namespace Nz
return file;
}
template<typename... Args>
void Error::Trigger(ErrorType type, std::string_view error, Args&&... args)
inline void Error::Trigger(ErrorType type, std::string error)
{
return TriggerInternal(type, Format(error, std::forward<Args>(args)...), 0, {}, {});
return TriggerInternal(type, std::move(error), 0, {}, {});
}
template<typename... Args>
void Error::Trigger(ErrorType type, unsigned int line, std::string_view file, std::string_view function, std::string_view error, Args&&... args)
inline void Error::Trigger(ErrorType type, unsigned int line, std::string_view file, std::string_view function, std::string error)
{
return TriggerInternal(type, Format(error, std::forward<Args>(args)...), line, GetCurrentFileRelativeToEngine(file), function);
return TriggerInternal(type, std::move(error), line, GetCurrentFileRelativeToEngine(file), function);
}
}

View File

@@ -11,9 +11,29 @@
#include <Nazara/Core/Config.hpp>
#include <string>
#ifdef NAZARA_BUILD
#include <fmt/compile.h>
#include <fmt/format.h>
#include <fmt/std.h>
#define NAZARA_FORMAT(s) FMT_STRING(s)
#else
#define NAZARA_FORMAT(s) s
#endif
namespace Nz
{
template<typename... Args> std::string Format(std::string_view str, Args&&... args);
#ifdef NAZARA_BUILD
template<typename... Args> using FormatString = fmt::format_string<Args...>;
#else
template<typename... Args> using FormatString = std::string_view;
#endif
template<typename... Args> std::string Format(FormatString<Args...> str, Args&&... args);
}
#include <Nazara/Core/Format.inl>

View File

@@ -2,11 +2,6 @@
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#ifdef NAZARA_BUILD
#include <fmt/format.h>
#include <fmt/std.h>
#endif
#include <Nazara/Core/Debug.hpp>
namespace Nz
@@ -23,8 +18,8 @@ namespace Nz
NAZARA_CORE_API std::string FormatFromStore(std::string_view str);
}
template<typename ...Args>
std::string Format(std::string_view str, Args&&... args)
template<typename... Args>
std::string Format(FormatString<Args...> str, Args&&... args)
{
#ifdef NAZARA_BUILD
return fmt::format(str, std::forward<Args>(args)...);

View File

@@ -35,7 +35,7 @@ namespace Nz
{
std::shared_ptr<Type> ref = Query(name);
if (!ref)
NazaraError("Object \"{}\" is not present", name);
NazaraErrorFmt("Object \"{}\" is not present", name);
return ref;
}

View File

@@ -61,12 +61,12 @@ namespace Nz
template<typename Type, typename Parameters>
std::shared_ptr<Type> ResourceLoader<Type, Parameters>::LoadFromFile(const std::filesystem::path& filePath, const Parameters& parameters) const
{
NazaraAssert(parameters.IsValid(), "Invalid parameters");
NazaraAssert(parameters.IsValid(), "invalid parameters");
std::string ext = ToLower(PathToString(filePath.extension()));
if (ext.empty())
{
NazaraError("failed to get file extension from \"{0}\"", filePath);
NazaraErrorFmt("failed to get file extension from \"{0}\"", filePath);
return nullptr;
}
@@ -93,7 +93,7 @@ namespace Nz
{
if (!file.Open(OpenMode::ReadOnly))
{
NazaraError("failed to load resource: unable to open \"{0}\"", filePath);
NazaraErrorFmt("failed to load resource: unable to open \"{0}\"", filePath);
return nullptr;
}
}
@@ -121,9 +121,9 @@ namespace Nz
}
if (found)
NazaraError("failed to load resource from file \"{0}}\": all loaders failed", filePath);
NazaraErrorFmt("failed to load resource from file \"{0}\": all loaders failed", filePath);
else
NazaraError("failed to load resource from file \"{0}}\": no loader found for extension \"{1}\"", filePath, ext);
NazaraErrorFmt("failed to load resource from file \"{0}\": no loader found for extension \"{1}\"", filePath, ext);
return nullptr;
}
@@ -196,8 +196,8 @@ namespace Nz
template<typename Type, typename Parameters>
std::shared_ptr<Type> ResourceLoader<Type, Parameters>::LoadFromStream(Stream& stream, const Parameters& parameters) const
{
NazaraAssert(stream.GetCursorPos() < stream.GetSize(), "No data to load");
NazaraAssert(parameters.IsValid(), "Invalid parameters");
NazaraAssert(stream.GetCursorPos() < stream.GetSize(), "no data to load");
NazaraAssert(parameters.IsValid(), "invalid parameters");
// Retrieve extension from stream (if any)
std::string ext = ToLower(PathToString(stream.GetPath().extension()));
@@ -236,7 +236,7 @@ namespace Nz
if (found)
NazaraError("failed to load resource from stream: all loaders failed");
else
NazaraError("Failed to load resource from from stream: no loader found");
NazaraError("failed to load resource from from stream: no loader found");
return nullptr;
}

View File

@@ -50,7 +50,7 @@ namespace Nz
std::shared_ptr<Type> resource = m_loader.LoadFromFile(absolutePath, GetDefaultParameters());
if (!resource)
{
NazaraError("failed to load resource from file: {0}", absolutePath);
NazaraErrorFmt("failed to load resource from file: {0}", absolutePath);
return std::shared_ptr<Type>();
}

View File

@@ -69,7 +69,7 @@ namespace Nz
std::string extension = ToLower(PathToString(filePath.extension()));
if (extension.empty())
{
NazaraError("failed to get file extension from \"{0}\"", filePath);
NazaraErrorFmt("failed to get file extension from \"{0}\"", filePath);
return false;
}
@@ -93,7 +93,7 @@ namespace Nz
if (!file.Open(OpenMode::WriteOnly | OpenMode::Truncate))
{
NazaraError("failed to save to file: unable to open \"{0}\" in write mode", filePath);
NazaraErrorFmt("failed to save to file: unable to open \"{0}\" in write mode", filePath);
return false;
}
@@ -107,7 +107,7 @@ namespace Nz
if (found)
NazaraError("failed to save resource: all savers failed");
else
NazaraError("failed to save resource: no saver found for extension \"extension\"", extension);
NazaraErrorFmt("failed to save resource: no saver found for extension \"extension\"", extension);
return false;
}
@@ -152,7 +152,7 @@ namespace Nz
if (found)
NazaraError("failed to save resource: all savers failed");
else
NazaraError("failed to save resource: no saver found for format \"{0}\"", format);
NazaraErrorFmt("failed to save resource: no saver found for format \"{0}\"", format);
return false;
}