qxLib
cout_logger_stream.inl
Go to the documentation of this file.
1 /**
2 
3  @file cout_logger_stream.inl
4  @author Khrapov
5  @date 30.07.2021
6  @copyright (c) Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 
10 namespace qx
11 {
12 
13 inline cout_logger_stream::cout_logger_stream(std::optional<config> optStreamConfig) noexcept
14  : base_standard_streams_stream(optStreamConfig ? *optStreamConfig : config())
15 {
16  const config streamConfig = optStreamConfig ? *optStreamConfig : config();
17 
18  if (streamConfig.bDisableStdioSync)
19  {
20  std::ios_base::sync_with_stdio(false);
21  }
22 
23  if (streamConfig.bUntieCin)
24  {
25  std::cout.tie(nullptr);
26  std::cerr.tie(nullptr);
27  }
28 }
29 
31 {
32  std::cout << std::flush;
33  std::cerr << std::flush;
34 }
35 
36 inline void cout_logger_stream::cout_colorized(verbosity eVerbosity, string_view svMessage, const color& rangeColor)
37 {
38  check_previous_message(eVerbosity);
39 
40  std::ostream& outputStream = !is_error(eVerbosity) ? std::cout : std::cerr;
41  const cstring_view svUtf8 = to_utf8(svMessage);
42  outputStream << terminal_color::font(rangeColor);
43  outputStream.write(svUtf8.data(), static_cast<std::streamsize>(svUtf8.size()));
44  outputStream << terminal_color::reset();
45 }
46 
47 inline void cout_logger_stream::cout_common(verbosity eVerbosity, string_view svMessage)
48 {
49  check_previous_message(eVerbosity);
50 
51  std::ostream& outputStream = !is_error(eVerbosity) ? std::cout : std::cerr;
52  const cstring_view svUtf8 = to_utf8(svMessage);
53  outputStream.write(svUtf8.data(), static_cast<std::streamsize>(svUtf8.size()));
54 }
55 
56 } // namespace qx
static cstring_view to_utf8(string_view svMessage)
Encode a message as UTF-8 for a byte output.
A base class for logger streams based on standard streams.
void check_previous_message(verbosity eCurrentMessageVerbosity)
Check the previous message type and flush if needed.
RGBA color.
Definition: color.h:196
cout_logger_stream(std::optional< config > optStreamConfig={}) noexcept
cout_logger_stream object constructor
virtual void do_flush() override
Flush the stream.
static constexpr terminal_color reset() noexcept
Reset all colors.
static constexpr terminal_color font(const color &fontColor) noexcept
Set font color.
constexpr bool is_error(verbosity eVerbosity) noexcept
Check if the verbosity level is an error.
Definition: verbosity.h:37