diff --git a/ecal/core/src/logging/ecal_file_resource.cpp b/ecal/core/src/logging/ecal_file_resource.cpp index eba72a6cff..3f969ba849 100644 --- a/ecal/core/src/logging/ecal_file_resource.cpp +++ b/ecal/core/src/logging/ecal_file_resource.cpp @@ -20,67 +20,57 @@ #include "ecal_file_resource.h" #include -#include -#include namespace eCAL { namespace Logging { - bool FileResource::fopen(const std::string& filename, std::ios::openmode mode) + void FileDeleter::operator()(FILE* file) const { - m_file.open(filename, mode); - - return m_file.is_open(); + if (file) { + fclose(file); + } } - void FileResource::fprintf(const char* format, ...) + FileResource::FileResource() : m_file(nullptr) { - if (!m_file.is_open()) - { - return; - } - - va_list args; - va_start(args, format); + } - // Determine the size of the formatted string - va_list args_copy; - va_copy(args_copy, args); - int size = vsnprintf(nullptr, 0, format, args_copy); - va_end(args_copy); + bool FileResource::fopen(const char* filename, const char* mode) + { + m_file.reset(std::fopen(filename, mode)); - if (size < 0) + if (m_file == nullptr) { - va_end(args); - return; + return false; } + else + { + return true; + } + } - // Create a buffer and format the string into it - std::vector buffer(size + 1); - vsnprintf(buffer.data(), buffer.size(), format, args); + void FileResource::fprintf(const char* format, ...) + { + va_list args; + va_start(args, format); + vfprintf(m_file.get(), format, args); va_end(args); - - // Write the formatted string to the file - m_file << buffer.data(); } void FileResource::fflush() { - if (m_file.is_open()) - { - m_file.flush(); - } + std::fflush(m_file.get()); } void FileResource::fclose() { - m_file.close(); + m_file.reset(); } bool FileResource::isOpen() const { - return m_file.is_open(); + return m_file != nullptr; } } } diff --git a/ecal/core/src/logging/ecal_file_resource.h b/ecal/core/src/logging/ecal_file_resource.h index 645b6502fd..c41dddd54d 100644 --- a/ecal/core/src/logging/ecal_file_resource.h +++ b/ecal/core/src/logging/ecal_file_resource.h @@ -24,18 +24,22 @@ #pragma once #include -#include -#include +#include namespace eCAL { namespace Logging { + // Custom deleter for FILE* + struct FileDeleter { + void operator()(FILE* file) const; + }; + class FileResource { public: - FileResource() = default; + FileResource(); - bool fopen(const std::string& filename, std::ios::openmode mode); + bool fopen(const char* filename, const char* mode); bool isOpen() const; void fclose(); @@ -43,7 +47,7 @@ namespace eCAL void fflush(); private: - std::fstream m_file; + std::unique_ptr m_file; }; } } \ No newline at end of file diff --git a/ecal/core/src/logging/ecal_log_impl.cpp b/ecal/core/src/logging/ecal_log_impl.cpp index bdfc746a1d..120aae060f 100644 --- a/ecal/core/src/logging/ecal_log_impl.cpp +++ b/ecal/core/src/logging/ecal_log_impl.cpp @@ -168,7 +168,7 @@ namespace eCAL const std::string tstring = get_time_str(); m_logfile_name = m_attributes.file.path + tstring + "_" + m_attributes.unit_name + "_" + std::to_string(m_attributes.process_id) + ".log"; - file_opened = m_logfile.fopen(m_logfile_name.c_str(), std::ios::out); + file_opened = m_logfile.fopen(m_logfile_name.c_str(), "w"); } else {