-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdiagnostics.h
175 lines (148 loc) · 4.42 KB
/
diagnostics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef TAGPARSER_DIAGNOSTICS_H
#define TAGPARSER_DIAGNOSTICS_H
#include "./global.h"
#include <c++utilities/chrono/datetime.h>
#include <string>
#include <vector>
namespace TagParser {
/*!
* \brief Specifies the level of the diagnostic message.
*/
enum class DiagLevel {
None = 0, /**< indicates that no diagnostic messages are present; should not be used when constructing a diagnostic message */
Debug = 1, /**< indicates a debbuging message */
Information = 2, /**< indicates an informal message */
Warning = 3, /**< indicates a warning */
Critical = 4, /**< indicates a critical error */
Fatal = 5, /**< indicates a fatal error (note that this level is currently not used) */
};
/// \brief The worst diag level.
constexpr auto worstDiagLevel = DiagLevel::Fatal;
TAG_PARSER_EXPORT std::string_view diagLevelName(DiagLevel diagLevel);
/*!
* \brief Sets \a lhs to \a rhs if \a rhs is more critical than \a lhs and returns \a lhs.
*/
constexpr DiagLevel &operator|=(DiagLevel &lhs, const DiagLevel &rhs)
{
if (lhs < rhs) {
lhs = rhs;
}
return lhs;
}
class DiagMessage {
public:
DiagMessage(DiagLevel level, const std::string &message, const std::string &context);
DiagMessage(DiagLevel level, std::string &&message, const std::string &context);
DiagMessage(DiagLevel level, const std::string &message, std::string &&context);
DiagMessage(DiagLevel level, std::string &&message, std::string &&context);
DiagLevel level() const;
std::string_view levelName() const;
const std::string &message() const;
const std::string &context() const;
const CppUtilities::DateTime &creationTime() const;
bool operator==(const DiagMessage &other) const;
static std::string formatList(const std::vector<std::string> &values);
private:
DiagLevel m_level;
std::string m_message;
std::string m_context;
CppUtilities::DateTime m_creationTime;
};
/*!
* \brief Constructs a new DiagMessage.
*/
inline DiagMessage::DiagMessage(DiagLevel level, const std::string &message, const std::string &context)
: m_level(level)
, m_message(message)
, m_context(context)
, m_creationTime(CppUtilities::DateTime::gmtNow())
{
}
/*!
* \brief Constructs a new DiagMessage.
*/
inline DiagMessage::DiagMessage(DiagLevel level, std::string &&message, const std::string &context)
: m_level(level)
, m_message(message)
, m_context(context)
, m_creationTime(CppUtilities::DateTime::gmtNow())
{
}
/*!
* \brief Constructs a new DiagMessage.
*/
inline DiagMessage::DiagMessage(DiagLevel level, const std::string &message, std::string &&context)
: m_level(level)
, m_message(message)
, m_context(context)
, m_creationTime(CppUtilities::DateTime::gmtNow())
{
}
/*!
* \brief Constructs a new DiagMessage.
*/
inline DiagMessage::DiagMessage(DiagLevel level, std::string &&message, std::string &&context)
: m_level(level)
, m_message(message)
, m_context(context)
, m_creationTime(CppUtilities::DateTime::gmtNow())
{
}
/*!
* \brief Returns the level.
*/
inline DiagLevel DiagMessage::level() const
{
return m_level;
}
/*!
* \brief Returns the string representation of the level().
*/
inline std::string_view DiagMessage::levelName() const
{
return diagLevelName(m_level);
}
/*!
* \brief Returns the message.
*/
inline const std::string &DiagMessage::message() const
{
return m_message;
}
/*!
* \brief Returns the context.
*/
inline const std::string &DiagMessage::context() const
{
return m_context;
}
/*!
* \brief Returns the creation time (using GMT timezone).
*/
inline const CppUtilities::DateTime &DiagMessage::creationTime() const
{
return m_creationTime;
}
/*!
* \brief Returns whether the current instance equals \a other. Everything but the creationTime() is considered.
*/
inline bool DiagMessage::operator==(const DiagMessage &other) const
{
return m_level == other.m_level && m_message == other.m_message && m_context == other.m_context;
}
class TAG_PARSER_EXPORT Diagnostics : public std::vector<DiagMessage> {
public:
Diagnostics() = default;
Diagnostics(std::initializer_list<DiagMessage> list);
bool has(DiagLevel level) const;
DiagLevel level() const;
};
/*!
* \brief Constructs a new container with the specified messages.
*/
inline Diagnostics::Diagnostics(std::initializer_list<DiagMessage> list)
: std::vector<DiagMessage>(list)
{
}
} // namespace TagParser
#endif // TAGPARSER_DIAGNOSTICS_H