Skip to content

Commit

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

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

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<char> 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;
}
}
}
14 changes: 9 additions & 5 deletions ecal/core/src/logging/ecal_file_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,30 @@
#pragma once

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

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

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

private:
std::fstream m_file;
std::unique_ptr<FILE, FileDeleter> 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(), std::ios::out);
file_opened = m_logfile.fopen(m_logfile_name.c_str(), "w");
}
else
{
Expand Down

0 comments on commit 0623939

Please sign in to comment.