Skip to content

Commit

Permalink
refactor logger class by std::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
sksat committed Feb 19, 2024
1 parent ff55356 commit 34eac33
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 65 deletions.
74 changes: 24 additions & 50 deletions src/library/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,40 @@

#include <ctime>
#include <sstream>
#ifdef _WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#endif

std::vector<ILoggable *> log_list_;
bool Logger::is_directory_created_ = false;

Logger::Logger(const std::string &file_name, const std::string &data_path, const std::string &ini_file_name, const bool is_ini_save_enabled,
namespace fs = std::filesystem;

Logger::Logger(const std::string &file_name, const fs::path &data_path, const fs::path &ini_file_name, const bool is_ini_save_enabled,
const bool is_enabled)
: is_enabled_(is_enabled), is_ini_save_enabled_(is_ini_save_enabled) {
is_file_opened_ = false;
if (is_enabled_ == false) return;

// Get current time to append it to the filename
// Set current time to filename prefix
time_t timer = time(NULL);
struct tm *now;
now = localtime(&timer);
char start_time_c[64];
strftime(start_time_c, 64, "%y%m%d_%H%M%S", now);

const auto file_prefix = std::string(start_time_c) + "_";

// Create directory
if (is_ini_save_enabled_ == true || is_directory_created_ == false) {
directory_path_ = CreateDirectory(data_path, start_time_c);
} else {
directory_path_ = data_path;
}

// Create File
std::stringstream file_path;
file_path << directory_path_ << start_time_c << "_" << file_name;
fs::path file_path = directory_path_ / (file_prefix + file_name);
if (is_enabled_) {
csv_file_.open(file_path.str());
csv_file_.open(file_path.string());
is_file_opened_ = csv_file_.is_open();
if (!is_file_opened_) std::cerr << "Error opening log file: " << file_path.str() << std::endl;
if (!is_file_opened_) std::cerr << "Error opening log file: " << file_path << std::endl;
}

// Copy SimBase.ini
Expand Down Expand Up @@ -82,50 +81,25 @@ void Logger::AddLogList(ILoggable *loggable) { log_list_.push_back(loggable); }

void Logger::ClearLogList() { log_list_.clear(); }

std::string Logger::CreateDirectory(const std::string &data_path, const std::string &time) {
std::string directory_path_tmp_ = data_path + "/logs_" + time + "/";
// Make directory
int rtn_mkdir = 0;
#ifdef WIN32
rtn_mkdir = _mkdir(directory_path_tmp_.c_str());
#else
rtn_mkdir = mkdir(directory_path_tmp_.c_str(), 0777);
#endif
if (rtn_mkdir == 0) {
} else {
std::cerr << "Error making directory: " << directory_path_tmp_ << std::endl;
return data_path;
}
is_directory_created_ = true;
return directory_path_tmp_;
}
fs::path Logger::CreateDirectory(const fs::path &data_path, const std::string &time) {
fs::path log_dir_ = data_path;
log_dir_.append(std::string("logs_") + time);

void Logger::CopyFileToLogDirectory(const std::string &ini_file_name) {
using std::ios;
fs::create_directories(log_dir_);

if (is_ini_save_enabled_ == false) return;
// Copy files to the directory
std::string file_name = GetFileName(ini_file_name);
std::string to_file_name = directory_path_ + file_name;
std::ifstream is(ini_file_name, ios::in | ios::binary);
std::ofstream os(to_file_name, ios::out | ios::binary);
os << is.rdbuf();

return;
return log_dir_;
}

std::string Logger::GetFileName(const std::string &path) {
size_t pos1;

pos1 = path.rfind('\\');
if (pos1 != std::string::npos) {
return path.substr(pos1 + 1, path.size() - pos1 - 1);
}
void Logger::CopyFileToLogDirectory(const fs::path &ini_file_name) {
if (is_ini_save_enabled_ == false) return;
// Copy files to the directory
fs::path to_file_name = directory_path_ / ini_file_name.filename();

pos1 = path.rfind('/');
if (pos1 != std::string::npos) {
return path.substr(pos1 + 1, path.size() - pos1 - 1);
if (fs::exists(to_file_name)) {
std::cout << "File " << to_file_name << " already exists. Skip copy from" << ini_file_name << std::endl;
return;
}

return path;
fs::copy_file(ini_file_name, to_file_name);
return;
}
23 changes: 8 additions & 15 deletions src/library/logger/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#define _CRT_SECURE_NO_WARNINGS

#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
Expand All @@ -29,8 +30,8 @@ class Logger {
* @param [in] is_ini_save_enabled: Enable flag to save ini files
* @param [in] is_enabled: Enable flag for logging
*/
Logger(const std::string &file_name, const std::string &data_path, const std::string &ini_file_name, const bool is_ini_save_enabled,
const bool is_enabled = true);
Logger(const std::string &file_name, const std::filesystem::path &data_path, const std::filesystem::path &ini_file_name,
const bool is_ini_save_enabled, const bool is_enabled = true);
/**
* @fn ~Logger
* @brief Destructor
Expand Down Expand Up @@ -72,7 +73,7 @@ class Logger {
* @brief Copy a file (e.g., ini file) into the log directory
* @param [in] ini_file_name: The path to the target file to copy
*/
void CopyFileToLogDirectory(const std::string &ini_file_name);
void CopyFileToLogDirectory(const std::filesystem::path &ini_file_name);

// Getter
/**
Expand All @@ -84,7 +85,7 @@ class Logger {
* @fn GetLogPath
* @brief Return the path to the directory for log files
*/
inline std::string GetLogPath() const { return directory_path_; }
inline std::filesystem::path GetLogPath() const { return directory_path_; }

private:
std::ofstream csv_file_; //!< CSV file stream
Expand All @@ -93,8 +94,8 @@ class Logger {
static bool is_directory_created_; //!< Is the log output directory is created in the scenario
std::vector<ILoggable *> log_list_; //!< Log list

bool is_ini_save_enabled_; //!< Enable flag to save ini files
std::string directory_path_; //!< Path to the directory for log files
bool is_ini_save_enabled_; //!< Enable flag to save ini files
std::filesystem::path directory_path_; //!< Path to the directory for log files

/**
* @fn Write
Expand All @@ -117,15 +118,7 @@ class Logger {
* @param[in] time: Time stamp (YYYYMMDD_hhmmss)
* @return Path to the created directory
*/
std::string CreateDirectory(const std::string &data_path, const std::string &time);

/**
* @fn GetFileName
* @brief Extract the file name from the name with directory path
* @param [in] path: Directory path including the file name
* @return The extracted file name
*/
std::string GetFileName(const std::string &path);
std::filesystem::path CreateDirectory(const std::filesystem::path &data_path, const std::string &time);
};

#endif // S2E_LIBRARY_LOGGER_LOGGER_HPP_

0 comments on commit 34eac33

Please sign in to comment.