osmanip
Library with useful output stream tools like: color and style manipulators, progress bars and terminal graphics.
output_redirector.hpp
Go to the documentation of this file.
1 //====================================================
2 // File data
3 //====================================================
12 //====================================================
13 // Preprocessor settings
14 //====================================================
15 #pragma once
16 #ifndef OSMANIP_UTILITY_OUTPUTREDIRECTOR_HPP
17 #define OSMANIP_UTILITY_OUTPUTREDIRECTOR_HPP
18 
19 //====================================================
20 // Headers
21 //====================================================
22 
23 // My headers
25 
26 // STD headers
27 #include <stdint.h>
28 
29 #include <atomic>
30 #include <filesystem>
31 #include <fstream>
32 #include <string>
33 
34 namespace osm {
35 
36  //====================================================
37  // Aliases
38  //====================================================
39  namespace fs = std::filesystem;
40 
41  //====================================================
42  // OutputRedirector
43  //====================================================
49  class OutputRedirector : public std::ostream, public Stringbuf {
50  public:
51 
52  // Constructors and destructor
54  explicit OutputRedirector(std::string filename);
55  ~OutputRedirector() override;
56 
57  // Setters
58  void setFilename(std::string_view filename);
59 
60  // Getters
61  [[nodiscard]] std::string &getFilename();
62  [[nodiscard]] std::string &getFilepath();
63 
64  // Methods
65  void end();
66  void begin();
67  void touch();
68  bool isEnabled();
69 
70  // Members
71  static const std::string DEFAULT_FILE_DIR;
72  static const std::string DEFAULT_FILEPATH;
73  static const std::string DEFAULT_FILENAME;
74 
75  private:
76 
77  // Methods
78  int32_t sync() override;
79  void write_output();
80  void prepare_output();
81  void read_file();
82  void sanity_check(const std::string &func_name);
83  void exception_file_not_found();
84 
85  // Members
86  std::atomic<bool> enabled_;
87  std::string file_dir_;
88  std::string filename_;
89  std::string filepath_;
90  std::fstream fstream_;
91  std::stringstream output_str_;
92  int32_t last_ansi_str_index_;
93  int32_t last_ansi_str_size_;
94  };
95 
96 } // namespace osm
97 
98 #endif
This class is used to redirected output to a file.
Definition: output_redirector.hpp:49
static const std::string DEFAULT_FILEPATH
Definition: output_redirector.hpp:72
std::string & getFilepath()
Get the name of the path to the output file.
Definition: output_redirector.cpp:135
void setFilename(std::string_view filename)
Set the filename of the output file. The filename must be relative to the working directory.
Definition: output_redirector.cpp:102
OutputRedirector()
Construct a new OutputRedirector object. Default constructor will set the main attributes to default ...
Definition: output_redirector.cpp:53
bool isEnabled()
Returns the current state of the output redirection object.
Definition: output_redirector.cpp:195
static const std::string DEFAULT_FILE_DIR
Definition: output_redirector.hpp:71
std::string & getFilename()
Get the filename of the output file.
Definition: output_redirector.cpp:123
static const std::string DEFAULT_FILENAME
Definition: output_redirector.hpp:73
void begin()
Enables output redirection.
Definition: output_redirector.cpp:151
void touch()
Opens the file, if present. Otherwise, creates the file.
Definition: output_redirector.cpp:176
void end()
Flushes the buffer and disables output redirection.
Definition: output_redirector.cpp:163
~OutputRedirector() override
Destructs OutputRedirector object. If redirection is still enabled, the buffer is flushed before bein...
Definition: output_redirector.cpp:84
This class inherits std::stringbuf and adds additional functionality and thread safety.
Definition: sstream.hpp:40
Definition: canvas.cpp:30