Skip to content
Snippets Groups Projects
Commit 804f702d authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Merge branch 'wignored-warning' into 'dev'

fix: Warning for ignored attribute when using function pointer as unique_ptr deleter

See merge request !260
parents 41e3501d b0b0f2da
No related branches found
No related tags found
2 merge requests!279v0.4.0,!260fix: Warning for ignored attribute when using function pointer as unique_ptr deleter
Pipeline #60052 failed
......@@ -176,6 +176,22 @@ public:
}
}
/**
* @struct fcloseDeleter
* @brief Custom deleter to prevent compiler warnings when using
* std::unique_ptr with FILE*.
*
* Using function pointers with attributes (e.g., __attribute__((nonnull)))
* as deleters can trigger warnings like -Wignored-attributes. By defining a
* custom deleter struct, these attributes are preserved, eliminating the
* warnings.
*/
struct fcloseDeleter {
void operator()(FILE *f) const noexcept {
std::fclose(f);
}
};
private:
static void log(Level level, const std::string& msg);
static void initFile(const std::string& fileName);
......@@ -184,7 +200,7 @@ private:
static bool mConsoleColor;
static Level mFileLevel;
static std::string mFileName;
static std::unique_ptr<FILE, decltype(&std::fclose)> mFile;
static std::unique_ptr<FILE, fcloseDeleter> mFile;
static std::vector<std::string> mContext;
};
}
......
......@@ -56,13 +56,13 @@ std::string Aidge::Log::mFileName = []() {
}
return std::string();
}();
std::unique_ptr<FILE, decltype(&std::fclose)> Aidge::Log::mFile {nullptr, nullptr};
std::unique_ptr<FILE, Aidge::Log::fcloseDeleter> Aidge::Log::mFile {nullptr, Aidge::Log::fcloseDeleter()};
std::vector<std::string> Aidge::Log::mContext;
void Aidge::Log::log(Level level, const std::string& msg) {
if (level >= mConsoleLevel) {
// Apply log level style only for console.
// Styles that were already applied to msg with fmt are kept also in
// Styles that were already applied to msg with fmt are kept also in
// the log file.
const auto modifier
= !mConsoleColor ? fmt::text_style()
......@@ -94,14 +94,17 @@ void Aidge::Log::log(Level level, const std::string& msg) {
}
void Aidge::Log::initFile(const std::string& fileName) {
mFile = std::unique_ptr<FILE, decltype(&std::fclose)>(std::fopen(fileName.c_str(), "a"), &std::fclose);
FILE* rawFile = std::fopen(fileName.c_str(), "a");
if (!mFile) {
if (!rawFile) {
mFileName.clear(); // prevents AIDGE_THROW_OR_ABORT() to try to log into file
AIDGE_THROW_OR_ABORT(std::runtime_error,
"Could not create log file: {}", fileName);
}
// Assign the new FILE* with the deleter
mFile = std::unique_ptr<FILE, fcloseDeleter>(rawFile, fcloseDeleter());
const std::time_t t = std::time(nullptr);
fmt::println(mFile.get(), "###### {:%Y-%m-%d %H:%M:%S} ######", fmt::localtime(t));
}
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