qxLib
verbosity.h
Go to the documentation of this file.
1 /**
2 
3  @file verbosity.h
4  @author Khrapov
5  @date 23.07.2023
6  @copyright (c) Nick Khrapov, 2023. All right reserved.
7 
8 **/
9 #pragma once
10 
12 
13 #ifndef QX_CONF_COMPILE_TIME_VERBOSITY
14  #define QX_CONF_COMPILE_TIME_VERBOSITY qx::verbosity::detailed
15 #endif
16 
17 namespace qx
18 {
19 
20 enum class verbosity
21 {
22  detailed, // very frequently repeated messages, for example, on every update
23  verbose, // messages you don't want to be displayed by default
24  log, // default level
25  important, // same as log but highlighted if possible
26  warning, // not yet an error, but something to look out for
27  error, // an error after which it is possible to continue the program
28  critical, // an error that makes it impossible to continue the program
29  none, // message is not displayed
30 };
31 
32 /**
33  @brief Check if the verbosity level is an error
34  @param eVerbosity - verbosity level to check
35  @retval - true if the verbosity level is error or critical, false otherwise
36 **/
37 constexpr bool is_error(verbosity eVerbosity) noexcept
38 {
39  return eVerbosity == verbosity::error || eVerbosity == verbosity::critical;
40 }
41 
42 } // namespace qx
43 
44 template<class char_t>
45 struct QX_FMT_NS::formatter<qx::verbosity, char_t>
46 {
47  bool bLog = false;
48 
49  template<class format_parse_context_t>
50  constexpr auto parse(format_parse_context_t& ctx)
51  {
52  auto it = ctx.begin();
53 
54  // format for logs. [X] form.
55  if (it != ctx.end() && *it == QX_CHAR_PREFIX(char_t, 'l'))
56  {
57  ++it;
58  bLog = true;
59  }
60 
61  if (it != ctx.end() && *it != QX_CHAR_PREFIX(char_t, '}'))
62  throw QX_FMT_NS::format_error("unknown spec");
63 
64  return it;
65  }
66 
67  template<class format_context_type>
68  constexpr auto format(const qx::verbosity& value, format_context_type& ctx) const
69  {
70  auto get_log_name = [value]() -> basic_string_view<char_t>
71  {
72  switch (value)
73  {
74  case qx::verbosity::detailed:
75  return QX_STR_PREFIX(char_t, "[D]");
76  case qx::verbosity::verbose:
77  return QX_STR_PREFIX(char_t, "[V]");
78  case qx::verbosity::log:
79  return QX_STR_PREFIX(char_t, " ");
80  case qx::verbosity::important:
81  return QX_STR_PREFIX(char_t, "[I]");
82  case qx::verbosity::warning:
83  return QX_STR_PREFIX(char_t, "[W]");
84  case qx::verbosity::error:
85  return QX_STR_PREFIX(char_t, "[E]");
86  case qx::verbosity::critical:
87  return QX_STR_PREFIX(char_t, "[C]");
88  case qx::verbosity::none:
89  return QX_STR_PREFIX(char_t, " ");
90  }
91 
92  return QX_STR_PREFIX(char_t, " ");
93  };
94 
95  auto get_full_name = [value]() -> basic_string_view<char_t>
96  {
97  switch (value)
98  {
99  case qx::verbosity::detailed:
100  return QX_STR_PREFIX(char_t, "detailed");
101  case qx::verbosity::verbose:
102  return QX_STR_PREFIX(char_t, "verbose");
103  case qx::verbosity::log:
104  return QX_STR_PREFIX(char_t, "log");
105  case qx::verbosity::important:
106  return QX_STR_PREFIX(char_t, "important");
107  case qx::verbosity::warning:
108  return QX_STR_PREFIX(char_t, "warning");
109  case qx::verbosity::error:
110  return QX_STR_PREFIX(char_t, "error");
111  case qx::verbosity::critical:
112  return QX_STR_PREFIX(char_t, "critical");
113  case qx::verbosity::none:
114  return QX_STR_PREFIX(char_t, "none");
115  }
116 
117  return QX_STR_PREFIX(char_t, "");
118  };
119 
120  return QX_FMT_NS::format_to(ctx.out(), QX_STR_PREFIX(char_t, "{}"), bLog ? get_log_name() : get_full_name());
121  }
122 };
#define QX_CHAR_PREFIX(value_t, ch)
Chose witch of prefixes add to char : L or none.
Definition: string_utils.h:255
#define QX_STR_PREFIX(value_t, str)
Chose witch of prefixes add to string : L or none.
Definition: string_utils.h:247
constexpr bool is_error(verbosity eVerbosity) noexcept
Check if the verbosity level is an error.
Definition: verbosity.h:37