BioCMAMC-ST
console.hpp
1#ifndef __LOGGER_CONSOLE_HPP__
2#define __LOGGER_CONSOLE_HPP__
3
4#include <common/logger.hpp>
5#include <memory>
6#include <optional>
7#include <ostream>
8#include <source_location>
9#include <sstream>
10#include <streambuf>
11
12namespace IO
13{
14
20 class Console final : public Logger
21 {
22 public:
23 Console();
24 ~Console() override = default;
25
26 Console(const Console&) = delete;
27 Console(Console&&) = delete;
28 Console& operator=(const Console&) = delete;
30
31 void debug(std::string_view message) noexcept final;
32
33 void print(std::string_view prefix,
34 std::string_view message) noexcept final;
35 void alert(std::string_view prefix,
36 std::string_view message) noexcept final;
37
38 void error(std::string_view message,
39 std::source_location location
40 = std::source_location::current()) noexcept final;
41
42 void raw_log(std::string_view message) noexcept final;
43
44 void toggle_debug() noexcept final;
45 void toggle_print() noexcept final;
46 void toggle_alert() noexcept final;
47 void toggle_error() noexcept final;
48 void toggle_all() noexcept final;
49
50 // std::ostream& get_output()
51 // {
52 // return output;
53 // };
54
55 private:
56 uint32_t flags;
57 std::ostream& output;
58 std::ostream& err_output;
59
61 };
62
63 // TODO move elsewhere
64
70
71 class RedirectGuard;
72
74 {
75 public:
76 RedirectHandle(std::shared_ptr<RedirectGuard> owner,
77 std::shared_ptr<bool> active_flag,
78 bool owns,
79 RedirectionType _type);
80
82
85
88
89 private:
90 std::shared_ptr<RedirectGuard> owner_ptr;
91 std::shared_ptr<bool> active_flag;
92 bool owns_guard = false;
94 };
95
96 class RedirectGuard : public std::enable_shared_from_this<RedirectGuard>
97 {
98 public:
100
101 [[nodiscard("Handle must be held")]] RedirectHandle redirect();
102
103 [[nodiscard("Handle must be held")]] RedirectHandle redirect_to_file();
104
105 void restore();
106
107 std::optional<std::string> getCapturedOutput() const;
108
109 private:
110 friend class RedirectHandle;
111
113 std::streambuf* coutbuf = nullptr;
114 std::stringstream buffer;
116
117 std::shared_ptr<bool> active_flag;
118 };
119} // namespace IO
120
121#endif
~Console() override=default
Console & operator=(const Console &)=delete
void error(std::string_view message, std::source_location location=std::source_location::current()) noexcept final
Log an error message with optional location details.
Definition logger.cpp:240
void toggle_error() noexcept final
Toggle the error logging behavior on or off.
Definition logger.cpp:291
void toggle_print() noexcept final
Toggle the print logging behavior on or off.
Definition logger.cpp:279
void toggle_alert() noexcept final
Toggle the alert logging behavior on or off.
Definition logger.cpp:285
void alert(std::string_view prefix, std::string_view message) noexcept final
Log an alert message with a prefix.
Definition logger.cpp:227
std::ostream & err_output
Definition console.hpp:58
void debug(std::string_view message) noexcept final
Log a debug message.
Definition logger.cpp:201
Console & operator=(Console &&)=delete
void raw_log(std::string_view message) noexcept final
Log a raw message with no additional formatting.
Definition logger.cpp:255
bool do_flush
Definition console.hpp:60
void print(std::string_view prefix, std::string_view message) noexcept final
Log a regular print message with a prefix.
Definition logger.cpp:214
void toggle_all() noexcept final
Toggle all logging types on or off.
Definition logger.cpp:273
uint32_t flags
Definition console.hpp:56
Console()
Definition logger.cpp:182
std::ostream & output
Definition console.hpp:57
void toggle_debug() noexcept final
Toggle the debug logging behavior on or off.
Definition logger.cpp:266
Console(const Console &)=delete
Console(Console &&)=delete
Logger()=default
std::shared_ptr< bool > active_flag
Definition console.hpp:117
RedirectHandle redirect_to_file()
Definition logger.cpp:90
void restore()
Definition logger.cpp:123
bool has_been_redirected
Definition console.hpp:112
std::optional< std::string > getCapturedOutput() const
Definition logger.cpp:151
std::stringstream buffer
Definition console.hpp:114
RedirectGuard()
Definition logger.cpp:58
int original_stdout_fd
Definition console.hpp:115
std::streambuf * coutbuf
Definition console.hpp:113
RedirectHandle redirect()
Definition logger.cpp:63
friend class RedirectHandle
Definition console.hpp:110
RedirectionType type
Definition console.hpp:93
RedirectHandle(RedirectHandle &&)=default
RedirectHandle & operator=(const RedirectHandle &)=delete
std::shared_ptr< RedirectGuard > owner_ptr
Definition console.hpp:90
std::shared_ptr< bool > active_flag
Definition console.hpp:91
RedirectHandle(const RedirectHandle &)=delete
bool owns_guard
Definition console.hpp:92
RedirectHandle(std::shared_ptr< RedirectGuard > owner, std::shared_ptr< bool > active_flag, bool owns, RedirectionType _type)
Definition logger.cpp:49
RedirectHandle & operator=(RedirectHandle &&)=default
Definition impl_post_process.hpp:11
RedirectionType
Definition console.hpp:66
@ File
Definition console.hpp:68
@ Buffer
Definition console.hpp:67