From a572a79ea9657a7e3a2886e22a1751493a90edfb Mon Sep 17 00:00:00 2001 From: Emilio <Emilio.Meschi@cern.ch> Date: Mon, 24 Oct 2022 00:59:40 +0200 Subject: [PATCH] class to delegate file output file actions --- src/OutputFileHandler.h | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/OutputFileHandler.h diff --git a/src/OutputFileHandler.h b/src/OutputFileHandler.h new file mode 100644 index 00000000..922f8d43 --- /dev/null +++ b/src/OutputFileHandler.h @@ -0,0 +1,61 @@ +#ifndef OUTPUTFILEHANDLER_H +#define OUTPUTFILEHANDLER_H + +#include <cstdint> +#include <cstdio> +#include <string> + +class OutputFileHandler { + +public: + OutputFileHandler(const std::string filename_base_path, + const std::string filename_prefix) + : filename_base_path_(filename_base_path), + filename_prefix_(filename_prefix), + working_files_basepath_(filename_base_path_ + "/" + working_dir_) { + create_output_directory_maybe(working_files_basepath_); + } + virtual ~OutputFileHandler() {} + + void open_file(uint32_t index, + uint32_t run); // Used for fixedNorbits per file option + { + // Create a new file + std::string output_directory = my_output_filename_base + "/" + working_dir; + create_output_directory(output_directory); + std::string current_filename_a = + output_directory + "/" + + format_run_file_stem(my_output_filename_prefix, current_run_number, + index_); + std::string filename = output_directory + "/" + + format_run_file_stem(my_output_filename_prefix, + control.run_number, index_); + LOG(INFO) << "opening file with index " << index_; + outFile.setFile(fopen(filename.c_str(), "w")); + outFile.setIndex(index_); + if (outFile.getFile() == NULL) { + std::string err = tools::strerror("ERROR when creating file '" + + current_filename_a + "'"); + LOG(ERROR) << err; + throw std::runtime_error(err); + } + } + +private: + void create_output_directory_maybe(std::string &output_directory); + + // name of subdir where the files are stored before they are moved to the + // final destination + static constexpr std::string working_dir_{"in_progress"}; + // name of the journal file. Note: Filename prefix is added making the final + // filename + static constexpr std::string journal_file_{"index.journal"}; + + std::string filename_base_path_; + std::string filename_prefix_; + std::string working_files_basepath_; + uint32_t current_run_number_; + FILE *current_file_; + int32_t current_index_; +}; +#endif -- GitLab