Skip to content

Commit

Permalink
Reworked file resource to fstream instead of FILE*.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peguen committed Sep 4, 2024
1 parent ae0f716 commit df25cf8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
58 changes: 34 additions & 24 deletions ecal/core/src/logging/ecal_file_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,57 +20,67 @@
#include "ecal_file_resource.h"

#include <cstdarg>
#include <sstream>
#include <vector>

namespace eCAL
{
namespace Logging
{
void FileDeleter::operator()(FILE* file) const
bool FileResource::fopen(const std::string& filename, std::ios::openmode mode)
{
if (file) {
fclose(file);
}
}
m_file.open(filename, mode);

FileResource::FileResource() : m_file(nullptr)
{
return m_file.is_open();
}

bool FileResource::fopen(const char* filename, const char* mode)
void FileResource::fprintf(const char* format, ...)
{
m_file.reset(std::fopen(filename, mode));

if (m_file == nullptr)
if (!m_file.is_open())
{
return false;
}
else
{
return true;
}
}
return;
}

void FileResource::fprintf(const char* format, ...)
{
va_list args;
va_start(args, format);
vfprintf(m_file.get(), format, args);

// 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);

if (size < 0)
{
va_end(args);
return;
}

// Create a buffer and format the string into it
std::vector<char> buffer(size + 1);
vsnprintf(buffer.data(), buffer.size(), format, args);
va_end(args);

// Write the formatted string to the file
m_file << buffer.data();
}

void FileResource::fflush()
{
std::fflush(m_file.get());
if (m_file.is_open())
{
m_file.flush();
}
}

void FileResource::fclose()
{
m_file.reset();
m_file.close();
}

bool FileResource::isOpen() const
{
return m_file != nullptr;
return m_file.is_open();
}
}
}
14 changes: 5 additions & 9 deletions ecal/core/src/logging/ecal_file_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,26 @@
#pragma once

#include <cstdio>
#include <memory>
#include <fstream>
#include <string>

namespace eCAL
{
namespace Logging
{
// Custom deleter for FILE*
struct FileDeleter {
void operator()(FILE* file) const;
};

class FileResource {
public:
FileResource();
FileResource() = default;

bool fopen(const char* filename, const char* mode);
bool fopen(const std::string& filename, std::ios::openmode mode);
bool isOpen() const;
void fclose();

void fprintf(const char* format, ...);
void fflush();

private:
std::unique_ptr<FILE, FileDeleter> m_file;
std::fstream m_file;
};
}
}
2 changes: 1 addition & 1 deletion ecal/core/src/logging/ecal_log_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(), "w");
file_opened = m_logfile.fopen(m_logfile_name.c_str(), std::ios::out);
}
else
{
Expand Down

0 comments on commit df25cf8

Please sign in to comment.