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 };
60
61 // TODO move elsewhere
62
68
69 class RedirectGuard;
70
72 {
73 public:
74 RedirectHandle(std::shared_ptr<RedirectGuard> owner,
75 std::shared_ptr<bool> active_flag,
76 bool owns,
77 RedirectionType _type);
78
80
83
86
87 private:
88 std::shared_ptr<RedirectGuard> owner_ptr;
89 std::shared_ptr<bool> active_flag;
90 bool owns_guard = false;
92 };
93
94 class RedirectGuard : public std::enable_shared_from_this<RedirectGuard>
95 {
96 public:
98
99 [[nodiscard("Handle must be held")]] RedirectHandle redirect();
100
101 [[nodiscard("Handle must be held")]] RedirectHandle redirect_to_file();
102
103 void restore();
104
105 std::optional<std::string> getCapturedOutput() const;
106
107 private:
108 friend class RedirectHandle;
109
111 std::streambuf* coutbuf = nullptr;
112 std::stringstream buffer;
114
115 std::shared_ptr<bool> active_flag;
116 };
117} // namespace IO
118
119#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:198
void toggle_error() noexcept final
Toggle the error logging behavior on or off.
Definition logger.cpp:238
void toggle_print() noexcept final
Toggle the print logging behavior on or off.
Definition logger.cpp:228
void toggle_alert() noexcept final
Toggle the alert logging behavior on or off.
Definition logger.cpp:233
void alert(std::string_view prefix, std::string_view message) noexcept final
Log an alert message with a prefix.
Definition logger.cpp:188
std::ostream & err_output
Definition console.hpp:58
void debug(std::string_view message) noexcept final
Log a debug message.
Definition logger.cpp:168
Console & operator=(Console &&)=delete
void raw_log(std::string_view message) noexcept final
Log a raw message with no additional formatting.
Definition logger.cpp:209
void print(std::string_view prefix, std::string_view message) noexcept final
Log a regular print message with a prefix.
Definition logger.cpp:178
void toggle_all() noexcept final
Toggle all logging types on or off.
Definition logger.cpp:223
uint32_t flags
Definition console.hpp:56
Console()
CONSOLE.
Definition logger.cpp:151
std::ostream & output
Definition console.hpp:57
void toggle_debug() noexcept final
Toggle the debug logging behavior on or off.
Definition logger.cpp:217
Console(const Console &)=delete
Console(Console &&)=delete
Logger()=default
std::shared_ptr< bool > active_flag
Definition console.hpp:115
RedirectHandle redirect_to_file()
Definition logger.cpp:73
void restore()
Definition logger.cpp:105
bool has_been_redirected
Definition console.hpp:110
std::optional< std::string > getCapturedOutput() const
Definition logger.cpp:132
std::stringstream buffer
Definition console.hpp:112
RedirectGuard()
Definition logger.cpp:45
int original_stdout_fd
Definition console.hpp:113
std::streambuf * coutbuf
Definition console.hpp:111
RedirectHandle redirect()
Definition logger.cpp:49
friend class RedirectHandle
Definition console.hpp:108
RedirectionType type
Definition console.hpp:91
RedirectHandle(RedirectHandle &&)=default
RedirectHandle & operator=(const RedirectHandle &)=delete
std::shared_ptr< RedirectGuard > owner_ptr
Definition console.hpp:88
std::shared_ptr< bool > active_flag
Definition console.hpp:89
RedirectHandle(const RedirectHandle &)=delete
bool owns_guard
Definition console.hpp:90
RedirectHandle(std::shared_ptr< RedirectGuard > owner, std::shared_ptr< bool > active_flag, bool owns, RedirectionType _type)
Definition logger.cpp:36
RedirectHandle & operator=(RedirectHandle &&)=default
Definition impl_post_process.hpp:11
RedirectionType
Definition console.hpp:64
@ File
Definition console.hpp:66
@ Buffer
Definition console.hpp:65