载入中...
搜索中...
未找到
Diagnostic.h
浏览该文件的文档.
1#pragma once
2
8#include "common/Export.h"
9
10#include <cstdint>
11#include <ostream>
12#include <string>
13#include <string_view>
14#include <utility>
15#include <vector>
16
17namespace eve {
18
20enum class Severity : uint8_t {
21 Info,
22 Warning,
23 Error,
24 Fatal,
25};
26
28[[nodiscard]] constexpr std::string_view severityName(Severity severity) noexcept {
29 switch (severity) {
30 case Severity::Info: return "info";
31 case Severity::Warning: return "warning";
32 case Severity::Error: return "error";
33 case Severity::Fatal: return "fatal";
34 }
35 return "unknown";
36}
37
39inline std::ostream& operator<<(std::ostream& stream, Severity severity) { return stream << severityName(severity); }
40
47enum class DiagnosticCode : uint32_t {
48 None = 0,
51 NotFound = 3,
52 AlreadyExists = 4,
53 Conflict = 5,
54 Unsupported = 6,
55 Cancelled = 7,
56 StaleHandle = 8,
57 ParseError = 9,
60 Failed = 12,
61 HashMismatch = 13,
62 UnknownVersion = 14,
63 CallbackFailure = 15,
75 TypeMismatch = 21,
76};
77
85using DiagnosticDetail = std::pair<std::string, std::string>;
86
88using DiagnosticDetails = std::vector<DiagnosticDetail>;
89
94class [[nodiscard("Diagnostic should be inspected or passed to a Result")]] EVENGINE_API_FOUNDATION Diagnostic {
95public:
97 Diagnostic() = default;
98
109 Diagnostic(DiagnosticCode code, Severity severity, std::string message, std::string path = {},
110 DiagnosticDetails details = {}, std::string source = {})
112 : code_(code),
114 severity_(severity),
116 message_(std::move(message)),
118 path_(std::move(path)),
120 details_(std::move(details)),
122 source_(std::move(source)) {}
123
125 static Diagnostic error(DiagnosticCode code, std::string message, std::string path = {},
126 DiagnosticDetails details = {}, std::string source = {}) {
128 return Diagnostic(code, Severity::Error, std::move(message), std::move(path), std::move(details),
130 std::move(source));
131 }
132
134 static Diagnostic warning(DiagnosticCode code, std::string message, std::string path = {},
135 DiagnosticDetails details = {}, std::string source = {}) {
137 return Diagnostic(code, Severity::Warning, std::move(message), std::move(path), std::move(details),
139 std::move(source));
140 }
141
143 DiagnosticCode code() const noexcept { return code_; }
145 Severity severity() const noexcept { return severity_; }
147 const std::string& message() const noexcept { return message_; }
149 const std::string& path() const noexcept { return path_; }
151 const DiagnosticDetails& details() const noexcept { return details_; }
153 const std::string& source() const noexcept { return source_; }
154
156 void addDetail(std::string key, std::string value) { details_.emplace_back(std::move(key), std::move(value)); }
157
159 bool isError() const noexcept { return severity_ == Severity::Error || severity_ == Severity::Fatal; }
160
161private:
162 DiagnosticCode code_ = DiagnosticCode::None;
163 Severity severity_ = Severity::Info;
164 std::string message_;
165 std::string path_;
166 DiagnosticDetails details_;
167 std::string source_;
168};
169
176[[nodiscard]] constexpr std::string_view diagnosticCodeName(DiagnosticCode code) noexcept {
177 switch (code) {
178 case DiagnosticCode::None: return "none";
179 case DiagnosticCode::InvalidArgument: return "invalid_argument";
180 case DiagnosticCode::PreconditionViolation: return "precondition_violation";
181 case DiagnosticCode::NotFound: return "not_found";
182 case DiagnosticCode::AlreadyExists: return "already_exists";
183 case DiagnosticCode::Conflict: return "conflict";
184 case DiagnosticCode::Unsupported: return "unsupported";
185 case DiagnosticCode::Cancelled: return "cancelled";
186 case DiagnosticCode::StaleHandle: return "stale_handle";
187 case DiagnosticCode::ParseError: return "parse_error";
188 case DiagnosticCode::SerializationError: return "serialization_error";
189 case DiagnosticCode::InvariantViolation: return "invariant_violation";
190 case DiagnosticCode::Failed: return "failed";
191 case DiagnosticCode::HashMismatch: return "hash_mismatch";
192 case DiagnosticCode::UnknownVersion: return "unknown_version";
193 case DiagnosticCode::CallbackFailure: return "callback_failure";
194 case DiagnosticCode::DialogueNotWaitingForChoice: return "dialogue_not_waiting_for_choice";
195 case DiagnosticCode::DialogueNotWaitingForCommand: return "dialogue_not_waiting_for_command";
196 case DiagnosticCode::DialogueConditionRejected: return "dialogue_condition_rejected";
197 case DiagnosticCode::DialogueRouteNotFound: return "dialogue_route_not_found";
198 case DiagnosticCode::ProcgenGroupDataInvalid: return "procgen_group_data_invalid";
199 case DiagnosticCode::TypeMismatch: return "type_mismatch";
200 }
201 return "unknown";
202}
203
205inline std::ostream& operator<<(std::ostream& stream, DiagnosticCode code) {
207 return stream << diagnosticCodeName(code);
208}
209
210} // namespace eve
double value
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::string message
DiagnosticCode code
std::uint32_t key
std::string path
Definition PlayHost.cpp:110
const UnitySourceAsset & source
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
DiagnosticCode code() const noexcept
Stable machine-readable code.
Definition Diagnostic.h:143
const std::string & path() const noexcept
Optional field, property, URI, or logical path.
Definition Diagnostic.h:149
Diagnostic()=default
Construct an empty informational diagnostic.
void addDetail(std::string key, std::string value)
Add structured context without changing the stable code.
Definition Diagnostic.h:156
Diagnostic(DiagnosticCode code, Severity severity, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct a diagnostic with its stable code and human text.
Definition Diagnostic.h:109
static Diagnostic warning(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct a warning diagnostic.
Definition Diagnostic.h:134
bool isError() const noexcept
Whether the diagnostic represents an error or fatal condition.
Definition Diagnostic.h:159
Severity severity() const noexcept
Severity for logging and presentation.
Definition Diagnostic.h:145
const std::string & message() const noexcept
Human-readable explanation.
Definition Diagnostic.h:147
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
const DiagnosticDetails & details() const noexcept
Structured context in insertion order.
Definition Diagnostic.h:151
const std::string & source() const noexcept
Optional producer or subsystem that emitted the diagnostic.
Definition Diagnostic.h:153
eve::Diagnostic Diagnostic
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
std::vector< DiagnosticDetail > DiagnosticDetails
Owning collection of diagnostic details with stable insertion order.
Definition Diagnostic.h:88
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
@ ProcgenGroupDataInvalid
Mesh group sidecar data is inconsistent with its dense streams.
@ TypeMismatch
A stable reference resolved to a different canonical domain type.
@ DialogueRouteNotFound
A requested dialogue route does not exist on the current node.
@ DialogueNotWaitingForChoice
Dialogue runner is not positioned at a choice suspension point.
@ DialogueConditionRejected
A dialogue condition rejected the requested route.
@ DialogueNotWaitingForCommand
Dialogue runner is not positioned at an asynchronous command.
std::ostream & operator<<(std::ostream &stream, Severity severity)
Writes a severity for diagnostics and test output.
Definition Diagnostic.h:39
constexpr std::string_view severityName(Severity severity) noexcept
Returns the stable lowercase severity spelling.
Definition Diagnostic.h:28
std::pair< std::string, std::string > DiagnosticDetail
A structured detail attached to a Diagnostic.
Definition Diagnostic.h:85
Severity
Stable severity of a diagnostic, independent of its display text.
Definition Diagnostic.h:20
constexpr std::string_view diagnosticCodeName(DiagnosticCode code) noexcept
Return a stable, compact spelling useful for logs and diagnostics.
Definition Diagnostic.h:176