diff options
Diffstat (limited to 'kernel/src/test_support/kapi/cio.cpp')
| -rw-r--r-- | kernel/src/test_support/kapi/cio.cpp | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/kernel/src/test_support/kapi/cio.cpp b/kernel/src/test_support/kapi/cio.cpp index 35452d4..4a1cd1f 100644 --- a/kernel/src/test_support/kapi/cio.cpp +++ b/kernel/src/test_support/kapi/cio.cpp @@ -1,43 +1,54 @@ +#include "kernel/test_support/cio.hpp" + #include <kapi/cio.hpp> #include "kernel/test_support/log_buffer.hpp" -#include <iostream> -#include <string> -#include <string_view> +#include <atomic> +#include <optional> +#include <stdexcept> + +namespace +{ + + auto constinit is_initialized = std::atomic_flag{}; + auto constinit device = std::optional<kernel::tests::cio::output_device>{}; + +} // namespace namespace kapi::cio { - namespace + auto init() -> void { - - class test_output_device : public output_device + if (is_initialized.test_and_set()) { - public: - test_output_device() = default; - - auto write(output_stream stream, std::string_view text) -> void override - { - auto & standard_stream = stream == output_stream::stdout ? std::cout : std::cerr; - standard_stream << text; - if (text != "\n") - { - kernel::tests::log_buffer::append(std::string{text}); - } - } - } device{}; - - } // namespace + throw std::logic_error("kapi::cio::init() called more than once"); + } - auto init() -> void + device.emplace(); + set_output_device(*device); + } + +} // namespace kapi::cio + +namespace kernel::tests::cio +{ + + auto deinit() -> void { - set_output_device(device); + if (!is_initialized.test()) + { + throw std::logic_error("kapi::cio::deinit() called before kapi::cio::init()"); + } + + device.reset(); + is_initialized.clear(); } - auto reset() -> void + auto log_buffer() -> kernel::tests::log_buffer & { - kernel::tests::log_buffer::clear(); + return device->log_buffer(); } -} // namespace kapi::cio
\ No newline at end of file +} // namespace kernel::tests::cio
\ No newline at end of file |
