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 © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 
10 namespace qx
11 {
12 
14  : base_logger_stream(config.bAlwaysFlush)
15  , m_bUsingColors(config.bUseColors)
16  , m_bDuplicateErrorsToCout(config.bDuplicateErrorsToCout)
17 {
18  if (config.bDisableStdioSync)
19  {
20  // Optimization
21  // Don't synchronize to the standard C streams after each input/output operation
22  std::ios_base::sync_with_stdio(false);
23  }
24 
25  if (config.bUntieCin)
26  {
27  // This unties cin from cout.
28  // Tied streams ensure that one stream is flushed automatically
29  // before each I/O operation on the other stream
30  std::wcin.tie(nullptr);
31  std::wcout.tie(nullptr);
32  }
33 }
34 
36 {
37  QX_PERF_SCOPE(CatLogger, "Flush to cout");
38 
39  std::wcout << std::flush;
40 }
41 
43  string_view svMessage,
44  const log_unit& logUnit,
45  const std::vector<logger_color_range>& colors,
46  verbosity eVerbosity)
47 {
48  QX_PERF_SCOPE(CatLogger, "Log to cout");
49 
50  thread_local wstring sWideMessage;
51  sWideMessage = to_wstring(svMessage);
52 
53  if (m_bUsingColors && eVerbosity < verbosity::error)
54  {
55  color commonColor = color::white();
56  switch (eVerbosity)
57  {
58  case verbosity::very_verbose:
59  case verbosity::verbose:
60  commonColor = color::gray();
61  break;
62 
63  case verbosity::log:
64  commonColor = color::white();
65  break;
66 
67  case verbosity::important:
68  commonColor = color::khaki();
69  break;
70 
71  case verbosity::warning:
72  commonColor = color::orange();
73  break;
74 
75  case verbosity::error:
76  commonColor = color::crimson();
77  break;
78 
79  case verbosity::critical:
80  commonColor = color::dark_red();
81  break;
82  }
83 
84  auto cout_colorized = [](size_t nStart, size_t nEnd, const color& rangeColor)
85  {
86  std::wcout << terminal_color::font(rangeColor)
87  << qx::wstring_view { sWideMessage.data() + nStart, nEnd - nStart } << terminal_color::reset();
88  };
89 
90  cout_colorized(0, colors.empty() ? sWideMessage.size() : colors.front().range.first, commonColor);
91 
92  for (size_t i = 0; i < colors.size(); ++i)
93  {
94  cout_colorized(colors[i].range.first, colors[i].range.second, colors[i].rangeColor);
95  cout_colorized(
96  colors[i].range.second,
97  i + 1 < colors.size() ? colors[i + 1].range.first : sWideMessage.size(),
98  commonColor);
99  }
100  }
101  else if (eVerbosity < verbosity::error)
102  {
103  std::wcout << sWideMessage;
104  }
105  else
106  {
107  std::wcerr << sWideMessage;
108 
109  if (m_bDuplicateErrorsToCout)
110  {
111  std::wcout << sWideMessage;
112  flush();
113  }
114  }
115 }
116 
117 inline void cout_logger_stream::set_using_colors(bool bUsingColors) noexcept
118 {
119  m_bUsingColors = bUsingColors;
120 }
121 
122 } // namespace qx
Base class for logger streams.
String class.
Definition: string.h:77
RGBA color.
Definition: color.h:193
virtual void flush() override
Flush stream.
virtual void do_log(string_view svMessage, const log_unit &logUnit, const std::vector< logger_color_range > &colors, verbosity eVerbosity) override
Proceed stream logging.
void set_using_colors(bool bUsingColors) noexcept
Set whether cout output should be colored.
cout_logger_stream(cout_logger_config config=cout_logger_config())
cout_logger_stream object constructor
static constexpr terminal_color reset() noexcept
Reset all colors.
static constexpr terminal_color font(const color &fontColor) noexcept
Set font color.
wstring to_wstring(cstring_view stringView, const std::locale &locale=std::locale())
convert cstring to wstring