forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostic_policy.h
85 lines (62 loc) · 2.67 KB
/
diagnostic_policy.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
#pragma once
#include <functional>
#include <optional>
#include <string>
#include "drake/common/drake_copyable.h"
namespace drake {
namespace internal {
/* Captures details about a warning or error condition.
Additional member fields might be added in future revisions. */
struct DiagnosticDetail {
std::optional<std::string> filename;
std::optional<int> line;
std::string message;
/* @return a formatted diagnostic message like one of:
file.txt:35: severity: some message
file.txt: severity: some message
severity: some message
depending on which fields are populated.
@pre severity must not be empty.
*/
std::string Format(const std::string& severity) const;
/* @return the result of Format("warning"). */
std::string FormatWarning() const;
/* @return the result of Format("error"). */
std::string FormatError() const;
};
/* A structured mechanism for functions that accept untrustworthy data to
report problems back to their caller.
By default, warnings will merely be logged into drake::log(), and errors will
throw exceptions. The user can customize this behavior via the Set methods.
Note in particular that the user's replacement error callback is not required
to throw. Code that calls policy.Error must be prepared to receive control
back from that method; typically, this means "return;" should follow its use.
*/
class DiagnosticPolicy {
public:
DRAKE_DEFAULT_COPY_AND_MOVE_AND_ASSIGN(DiagnosticPolicy);
DiagnosticPolicy() = default;
/* Triggers a warning. */
void Warning(std::string message) const;
/* Triggers an error. */
void Error(std::string message) const;
/* (Advanced) Triggers a warning. */
void Warning(const DiagnosticDetail& detail) const;
/* (Advanced) Triggers an error. */
void Error(const DiagnosticDetail& detail) const;
/* (Advanced) Processes a warning using default behavior. */
static void WarningDefaultAction(const DiagnosticDetail& detail);
/* (Advanced) Processes an error using default behavior. */
[[noreturn]] static void ErrorDefaultAction(const DiagnosticDetail& detail);
/* Replaces the behavior for warnings with the supplied functor.
Setting to nullptr restores the default behavior (i.e., to log). */
void SetActionForWarnings(std::function<void(const DiagnosticDetail&)>);
/* Replaces the behavior for errors with the supplied functor.
Setting to nullptr restores the default behavior (i.e., to throw). */
void SetActionForErrors(std::function<void(const DiagnosticDetail&)>);
private:
std::function<void(const DiagnosticDetail&)> on_warning_;
std::function<void(const DiagnosticDetail&)> on_error_;
};
} // namespace internal
} // namespace drake