Tag Parser  7.1.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
diagnostics.h
Go to the documentation of this file.
1 #ifndef TAGPARSER_DIAGNOSTICS_H
2 #define TAGPARSER_DIAGNOSTICS_H
3 
4 #include "./global.h"
5 
6 #include <c++utilities/chrono/datetime.h>
7 
8 #include <string>
9 #include <vector>
10 
11 namespace TagParser {
12 
16 enum class DiagLevel {
17  None = 0,
18  Debug = 1,
19  Information = 2,
20  Warning = 3,
21  Critical = 4,
22  Fatal = 5,
23 };
24 
27 
28 TAG_PARSER_EXPORT const char *diagLevelName(DiagLevel diagLevel);
29 
33 inline DiagLevel &operator|=(DiagLevel &lhs, const DiagLevel &rhs)
34 {
35  if (lhs < rhs) {
36  lhs = rhs;
37  }
38  return lhs;
39 }
40 
41 class DiagMessage {
42 public:
43  DiagMessage(DiagLevel level, const std::string &message, const std::string &context);
44  DiagMessage(DiagLevel level, std::string &&message, const std::string &context);
45  DiagMessage(DiagLevel level, const std::string &message, std::string &&context);
46  DiagMessage(DiagLevel level, std::string &&message, std::string &&context);
47 
48  DiagLevel level() const;
49  const char *levelName() const;
50  const std::string &message() const;
51  const std::string &context() const;
53  bool operator==(const DiagMessage &other) const;
54 
55 private:
56  DiagLevel m_level;
57  std::string m_message;
58  std::string m_context;
59  ChronoUtilities::DateTime m_creationTime;
60 };
61 
65 inline DiagMessage::DiagMessage(DiagLevel level, const std::string &message, const std::string &context)
66  : m_level(level)
67  , m_message(message)
68  , m_context(context)
69  , m_creationTime(ChronoUtilities::DateTime::gmtNow())
70 {
71 }
72 
76 inline DiagMessage::DiagMessage(DiagLevel level, std::string &&message, const std::string &context)
77  : m_level(level)
78  , m_message(message)
79  , m_context(context)
80  , m_creationTime(ChronoUtilities::DateTime::gmtNow())
81 {
82 }
83 
87 inline DiagMessage::DiagMessage(DiagLevel level, const std::string &message, std::string &&context)
88  : m_level(level)
89  , m_message(message)
90  , m_context(context)
91  , m_creationTime(ChronoUtilities::DateTime::gmtNow())
92 {
93 }
94 
98 inline DiagMessage::DiagMessage(DiagLevel level, std::string &&message, std::string &&context)
99  : m_level(level)
100  , m_message(message)
101  , m_context(context)
102  , m_creationTime(ChronoUtilities::DateTime::gmtNow())
103 {
104 }
105 
110 {
111  return m_level;
112 }
113 
117 inline const char *DiagMessage::levelName() const
118 {
119  return diagLevelName(m_level);
120 }
121 
125 inline const std::string &DiagMessage::message() const
126 {
127  return m_message;
128 }
129 
133 inline const std::string &DiagMessage::context() const
134 {
135  return m_context;
136 }
137 
142 {
143  return m_creationTime;
144 }
145 
149 inline bool DiagMessage::operator==(const DiagMessage &other) const
150 {
151  return m_level == other.m_level && m_message == other.m_message && m_context == other.m_context;
152 }
153 
154 class TAG_PARSER_EXPORT Diagnostics : public std::vector<DiagMessage> {
155 public:
156  Diagnostics() = default;
157  Diagnostics(std::initializer_list<DiagMessage> list);
158 
159  bool has(DiagLevel level) const;
160  DiagLevel level() const;
161 };
162 
166 inline Diagnostics::Diagnostics(std::initializer_list<DiagMessage> list)
167  : std::vector<DiagMessage>(list)
168 {
169 }
170 
176 class DiagPtr {
177 public:
178  DiagPtr();
179  DiagPtr(Diagnostics &diag);
181  const Diagnostics *operator->() const;
182 
183 private:
184  Diagnostics *m_ptr;
185 };
186 
188  : m_ptr(nullptr)
189 {
190 }
191 
193  : m_ptr(&diag)
194 {
195 }
196 
198 {
199  return m_ptr;
200 }
201 
202 inline const Diagnostics *DiagPtr::operator->() const
203 {
204  return m_ptr;
205 }
206 
207 } // namespace TagParser
208 
209 #endif // TAGPARSER_DIAGNOSTICS_H
const char * levelName() const
Returns the string representation of the level().
Definition: diagnostics.h:117
DiagMessage(DiagLevel level, const std::string &message, const std::string &context)
Constructs a new DiagMessage.
Definition: diagnostics.h:65
constexpr auto worstDiagLevel
The worst diag level.
Definition: diagnostics.h:26
STL namespace.
const ChronoUtilities::DateTime & creationTime() const
Returns the creation time (using GMT timezone).
Definition: diagnostics.h:141
DiagLevel level() const
Returns the level.
Definition: diagnostics.h:109
Diagnostics * operator->()
Definition: diagnostics.h:197
const std::string & context() const
Returns the context.
Definition: diagnostics.h:133
The DiagMessage class holds an information, warning or error gathered during parsing or making...
Definition: diagnostics.h:41
const std::string & message() const
Returns the message.
Definition: diagnostics.h:125
TAG_PARSER_EXPORT const char * diagLevelName(DiagLevel diagLevel)
Returns the string representation of the specified diagLevel.
Definition: diagnostics.cpp:22
bool operator==(const DiagMessage &other) const
Returns whether the current instance equals other.
Definition: diagnostics.h:149
DiagLevel & operator|=(DiagLevel &lhs, const DiagLevel &rhs)
Sets lhs to rhs if rhs is more critical than lhs and returns lhs.
Definition: diagnostics.h:33
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:154
The DiagPtr class is a leftover from development which should have been removed.
Definition: diagnostics.h:176
DiagLevel
Specifies the level of the diagnostic message.
Definition: diagnostics.h:16