Skip to content
Snippets Groups Projects
Commit 60905253 authored by Grégoire Kubler's avatar Grégoire Kubler Committed by Maxence Naud
Browse files

feat : added half_float support to fmt lib

parent 246a0c8b
No related branches found
No related tags found
No related merge requests found
#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);
}
};
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
* *
********************************************************************************/ ********************************************************************************/
#ifndef AIDGE_LOG_H_ #ifndef AIDGE_LOG_H_
#define AIDGE_LOG_H_ #define AIDGE_LOG_H_
...@@ -19,44 +18,36 @@ ...@@ -19,44 +18,36 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <fmt/ranges.h> #include <fmt/ranges.h>
#include "aidge/data/half_fmt.hpp"
#include "aidge/utils/Attributes.hpp" #include "aidge/utils/Attributes.hpp"
namespace Aidge { namespace Aidge {
/** /**
* Helper to define a context anywhere, hidding the scoped variable name * Helper to define a context anywhere, hidding the scoped variable name
* which has no relevance. * 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; (void)parg;
} }
template<class U, class... Us> template <class U, class... Us> static void discard_args(U parg, Us... pargs) {
static void discard_args(U parg, Us... pargs) {
(void)parg; (void)parg;
discard_args(pargs...); discard_args(pargs...);
} }
/** /**
* Aidge logging class, for displaying and file logging of events. * Aidge logging class, for displaying and file logging of events.
*/ */
class Log { class Log {
public: public:
enum Level { enum Level { Debug = 0, Info, Notice, Warn, Error, Fatal };
Debug = 0,
Info,
Notice,
Warn,
Error,
Fatal
};
class Context { class Context {
public: public:
template <typename... Args> template <typename... Args> Context(Args &&...args) {
Context(Args&&... args) {
Log::mContext.push_back(fmt::format(std::forward<Args>(args)...)); Log::mContext.push_back(fmt::format(std::forward<Args>(args)...));
} }
...@@ -68,13 +59,12 @@ public: ...@@ -68,13 +59,12 @@ public:
/** /**
* Detailed messages for debugging purposes, providing information helpful * Detailed messages for debugging purposes, providing information helpful
* for developers to trace and identify issues. * for developers to trace and identify issues.
* Detailed insights of what is appening in an operation, not useful for the * Detailed insights of what is appening in an operation, not useful for
* end-user. The operation is performed nominally. * the end-user. The operation is performed nominally.
* @note This level is disabled at compile time for Release, therefore * @note This level is disabled at compile time for Release, therefore
* inducing no runtime overhead for Release. * inducing no runtime overhead for Release.
*/ */
template <typename... Args> template <typename... Args> static void debug(Args &&...args) {
static void debug(Args&&... args) {
#ifndef NDEBUG #ifndef NDEBUG
// only when compiled in Debug // only when compiled in Debug
log(Debug, fmt::format(std::forward<Args>(args)...)); log(Debug, fmt::format(std::forward<Args>(args)...));
...@@ -86,22 +76,19 @@ public: ...@@ -86,22 +76,19 @@ public:
/** /**
* Messages that provide a record of the normal operation, about * Messages that provide a record of the normal operation, about
* the application's state, progress, or important events. * the application's state, progress, or important events.
* Reports normal start, end and key steps in an operation. The operation is * Reports normal start, end and key steps in an operation. The operation
* performed nominally. * is performed nominally.
*/ */
template <typename... Args> template <typename... Args> static void info(Args &&...args) {
static void info(Args&&... args) {
log(Info, fmt::format(std::forward<Args>(args)...)); log(Info, fmt::format(std::forward<Args>(args)...));
} }
/** /**
* Applies to normal but significant conditions that may require monitoring, * Applies to normal but significant conditions that may require
* like unusual or normal fallback events. * monitoring, like unusual or normal fallback events. Reports specific
* Reports specific paths in an operation. The operation can still be * paths in an operation. The operation can still be performed normally.
* performed normally. */
*/ template <typename... Args> static void notice(Args &&...args) {
template <typename... Args>
static void notice(Args&&... args) {
log(Notice, fmt::format(std::forward<Args>(args)...)); log(Notice, fmt::format(std::forward<Args>(args)...));
} }
...@@ -110,9 +97,8 @@ public: ...@@ -110,9 +97,8 @@ public:
* not necessarily cause immediate problems. * not necessarily cause immediate problems.
* Some specific steps of the operation could not be performed, but it can * Some specific steps of the operation could not be performed, but it can
* still provide an exploitable result. * still provide an exploitable result.
*/ */
template <typename... Args> template <typename... Args> static void warn(Args &&...args) {
static void warn(Args&&... args) {
log(Warn, fmt::format(std::forward<Args>(args)...)); log(Warn, fmt::format(std::forward<Args>(args)...));
} }
...@@ -121,26 +107,24 @@ public: ...@@ -121,26 +107,24 @@ public:
* recover from, but attention is needed to prevent further issues. * recover from, but attention is needed to prevent further issues.
* The operation could not be performed, but it does not prevent potential * The operation could not be performed, but it does not prevent potential
* further operations. * further operations.
*/ */
template <typename... Args> template <typename... Args> static void error(Args &&...args) {
static void error(Args&&... args) {
log(Error, fmt::format(std::forward<Args>(args)...)); log(Error, fmt::format(std::forward<Args>(args)...));
} }
/** /**
* Represents a critical error or condition that leads to the termination of * Represents a critical error or condition that leads to the termination
* the application, indicating a severe and unrecoverable problem. * of the application, indicating a severe and unrecoverable problem. The
* The operation could not be performed and any further operation is * operation could not be performed and any further operation is
* impossible. * impossible.
*/ */
template <typename... Args> template <typename... Args> static void fatal(Args &&...args) {
static void fatal(Args&&... args) {
log(Fatal, fmt::format(std::forward<Args>(args)...)); log(Fatal, fmt::format(std::forward<Args>(args)...));
} }
/** /**
* Set the minimum log level displayed in the console. * Set the minimum log level displayed in the console.
*/ */
static void setConsoleLevel(Level level) { static void setConsoleLevel(Level level) {
mConsoleLevel = level; mConsoleLevel = level;
} }
...@@ -148,14 +132,14 @@ public: ...@@ -148,14 +132,14 @@ public:
/** /**
* Set or disable colors on console. * Set or disable colors on console.
* Initial value should be assumed true. * Initial value should be assumed true.
*/ */
static void setConsoleColor(bool enabled) { static void setConsoleColor(bool enabled) {
mConsoleColor = enabled; mConsoleColor = enabled;
} }
/** /**
* Set the minimum log level saved in the log file. * Set the minimum log level saved in the log file.
*/ */
constexpr static void setFileLevel(Level level) { constexpr static void setFileLevel(Level level) {
mFileLevel = level; mFileLevel = level;
} }
...@@ -164,8 +148,8 @@ public: ...@@ -164,8 +148,8 @@ public:
* Set the log file name. * Set the log file name.
* Close the current log file and open the one with the new file name. * Close the current log file and open the one with the new file name.
* If empty, stop logging into a file. * If empty, stop logging into a file.
*/ */
static void setFileName(const std::string& fileName) { static void setFileName(const std::string &fileName) {
if (fileName != mFileName) { if (fileName != mFileName) {
mFileName = fileName; mFileName = fileName;
mFile.release(); mFile.release();
...@@ -187,8 +171,8 @@ public: ...@@ -187,8 +171,8 @@ public:
* warnings. * warnings.
*/ */
struct fcloseDeleter { struct fcloseDeleter {
void operator()(FILE *f) const noexcept { void operator()(FILE *f) const noexcept {
std::fclose(f); std::fclose(f);
} }
}; };
...@@ -203,11 +187,12 @@ private: ...@@ -203,11 +187,12 @@ private:
static std::unique_ptr<FILE, fcloseDeleter> mFile; static std::unique_ptr<FILE, fcloseDeleter> mFile;
static std::vector<std::string> mContext; static std::vector<std::string> mContext;
}; };
} } // namespace Aidge
namespace { namespace {
template <> 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_
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment