From 60905253a910403076e5d6e8b5b3681a24312b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20KUBLER?= <gregoire.kubler@proton.me> Date: Tue, 29 Oct 2024 10:52:14 +0100 Subject: [PATCH] feat : added half_float support to fmt lib --- include/aidge/data/half_fmt.hpp | 18 ++++++ include/aidge/utils/Log.hpp | 105 ++++++++++++++------------------ 2 files changed, 63 insertions(+), 60 deletions(-) create mode 100644 include/aidge/data/half_fmt.hpp diff --git a/include/aidge/data/half_fmt.hpp b/include/aidge/data/half_fmt.hpp new file mode 100644 index 000000000..5e2072038 --- /dev/null +++ b/include/aidge/data/half_fmt.hpp @@ -0,0 +1,18 @@ +#include "aidge/data/half.hpp" +#include <fmt/core.h> + +// Specialize fmt::formatter for half_float::half +template <> +struct fmt::formatter<half_float::half> : fmt::formatter<float> { + // Parses the format specifications and stores them in the base formatter + template <typename ParseContext> + constexpr auto parse(ParseContext& ctx) { + return fmt::formatter<float>::parse(ctx); + } + + // Formats the half type by first converting it to float + template <typename FormatContext> + auto format(const half_float::half& value, FormatContext& ctx) const { + return fmt::formatter<float>::format(static_cast<float>(value), ctx); + } +}; diff --git a/include/aidge/utils/Log.hpp b/include/aidge/utils/Log.hpp index cb9348dc2..794f14124 100644 --- a/include/aidge/utils/Log.hpp +++ b/include/aidge/utils/Log.hpp @@ -9,7 +9,6 @@ * ********************************************************************************/ - #ifndef AIDGE_LOG_H_ #define AIDGE_LOG_H_ @@ -19,44 +18,36 @@ #include <fmt/format.h> #include <fmt/ranges.h> +#include "aidge/data/half_fmt.hpp" + #include "aidge/utils/Attributes.hpp" namespace Aidge { /** * Helper to define a context anywhere, hidding the scoped variable name * which has no relevance. -*/ -#define AIDGE_LOG_CONTEXT(...) const Log::Context logContext_##__LINE__(__VA_ARGS__) + */ +#define AIDGE_LOG_CONTEXT(...) \ + const Log::Context logContext_##__LINE__(__VA_ARGS__) - -template<class U> -static void discard_args(U parg) { +template <class U> static void discard_args(U parg) { (void)parg; } -template<class U, class... Us> -static void discard_args(U parg, Us... pargs) { +template <class U, class... Us> static void discard_args(U parg, Us... pargs) { (void)parg; discard_args(pargs...); } /** * Aidge logging class, for displaying and file logging of events. -*/ + */ class Log { -public: - enum Level { - Debug = 0, - Info, - Notice, - Warn, - Error, - Fatal - }; + public: + enum Level { Debug = 0, Info, Notice, Warn, Error, Fatal }; class Context { - public: - template <typename... Args> - Context(Args&&... args) { + public: + template <typename... Args> Context(Args &&...args) { Log::mContext.push_back(fmt::format(std::forward<Args>(args)...)); } @@ -68,13 +59,12 @@ public: /** * Detailed messages for debugging purposes, providing information helpful * for developers to trace and identify issues. - * Detailed insights of what is appening in an operation, not useful for the - * end-user. The operation is performed nominally. + * Detailed insights of what is appening in an operation, not useful for + * the end-user. The operation is performed nominally. * @note This level is disabled at compile time for Release, therefore * inducing no runtime overhead for Release. - */ - template <typename... Args> - static void debug(Args&&... args) { + */ + template <typename... Args> static void debug(Args &&...args) { #ifndef NDEBUG // only when compiled in Debug log(Debug, fmt::format(std::forward<Args>(args)...)); @@ -86,22 +76,19 @@ public: /** * Messages that provide a record of the normal operation, about * the application's state, progress, or important events. - * Reports normal start, end and key steps in an operation. The operation is - * performed nominally. - */ - template <typename... Args> - static void info(Args&&... args) { + * Reports normal start, end and key steps in an operation. The operation + * is performed nominally. + */ + template <typename... Args> static void info(Args &&...args) { log(Info, fmt::format(std::forward<Args>(args)...)); } /** - * Applies to normal but significant conditions that may require monitoring, - * like unusual or normal fallback events. - * Reports specific paths in an operation. The operation can still be - * performed normally. - */ - template <typename... Args> - static void notice(Args&&... args) { + * Applies to normal but significant conditions that may require + * monitoring, like unusual or normal fallback events. Reports specific + * paths in an operation. The operation can still be performed normally. + */ + template <typename... Args> static void notice(Args &&...args) { log(Notice, fmt::format(std::forward<Args>(args)...)); } @@ -110,9 +97,8 @@ public: * not necessarily cause immediate problems. * Some specific steps of the operation could not be performed, but it can * still provide an exploitable result. - */ - template <typename... Args> - static void warn(Args&&... args) { + */ + template <typename... Args> static void warn(Args &&...args) { log(Warn, fmt::format(std::forward<Args>(args)...)); } @@ -121,26 +107,24 @@ public: * recover from, but attention is needed to prevent further issues. * The operation could not be performed, but it does not prevent potential * further operations. - */ - template <typename... Args> - static void error(Args&&... args) { + */ + template <typename... Args> static void error(Args &&...args) { log(Error, fmt::format(std::forward<Args>(args)...)); } /** - * Represents a critical error or condition that leads to the termination of - * the application, indicating a severe and unrecoverable problem. - * The operation could not be performed and any further operation is + * Represents a critical error or condition that leads to the termination + * of the application, indicating a severe and unrecoverable problem. The + * operation could not be performed and any further operation is * impossible. - */ - template <typename... Args> - static void fatal(Args&&... args) { + */ + template <typename... Args> static void fatal(Args &&...args) { log(Fatal, fmt::format(std::forward<Args>(args)...)); } /** * Set the minimum log level displayed in the console. - */ + */ static void setConsoleLevel(Level level) { mConsoleLevel = level; } @@ -148,14 +132,14 @@ public: /** * Set or disable colors on console. * Initial value should be assumed true. - */ + */ static void setConsoleColor(bool enabled) { mConsoleColor = enabled; } /** * Set the minimum log level saved in the log file. - */ + */ constexpr static void setFileLevel(Level level) { mFileLevel = level; } @@ -164,8 +148,8 @@ public: * Set the log file name. * Close the current log file and open the one with the new file name. * If empty, stop logging into a file. - */ - static void setFileName(const std::string& fileName) { + */ + static void setFileName(const std::string &fileName) { if (fileName != mFileName) { mFileName = fileName; mFile.release(); @@ -187,8 +171,8 @@ public: * warnings. */ struct fcloseDeleter { - void operator()(FILE *f) const noexcept { - std::fclose(f); + void operator()(FILE *f) const noexcept { + std::fclose(f); } }; @@ -203,11 +187,12 @@ private: static std::unique_ptr<FILE, fcloseDeleter> mFile; static std::vector<std::string> mContext; }; -} +} // namespace Aidge namespace { template <> -const char *const EnumStrings<Aidge::Log::Level>::data[] = {"Debug", "Info", "Notice", "Warn", "Error", "Fatal"}; +const char *const EnumStrings<Aidge::Log::Level>::data[] = + {"Debug", "Info", "Notice", "Warn", "Error", "Fatal"}; } -#endif //AIDGE_LOG_H_ +#endif // AIDGE_LOG_H_ -- GitLab