forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiagnostic_policy.cc
83 lines (68 loc) · 1.89 KB
/
diagnostic_policy.cc
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
#include "drake/common/diagnostic_policy.h"
#include <sstream>
#include <stdexcept>
#include <utility>
#include "drake/common/drake_assert.h"
#include "drake/common/text_logging.h"
namespace drake {
namespace internal {
std::string DiagnosticDetail::Format(const std::string& severity) const {
DRAKE_DEMAND(!severity.empty());
std::stringstream ss;
if (filename.has_value()) {
ss << *filename << ":";
if (line.has_value()) {
ss << *line << ":";
}
ss << " ";
}
ss << severity << ": ";
ss << message;
return ss.str();
}
std::string DiagnosticDetail::FormatWarning() const {
return Format("warning");
}
std::string DiagnosticDetail::FormatError() const {
return Format("error");
}
void DiagnosticPolicy::SetActionForWarnings(
std::function<void(const DiagnosticDetail&)> functor) {
on_warning_ = std::move(functor);
}
void DiagnosticPolicy::SetActionForErrors(
std::function<void(const DiagnosticDetail&)> functor) {
on_error_ = std::move(functor);
}
void DiagnosticPolicy::Warning(std::string message) const {
DiagnosticDetail d;
d.message = std::move(message);
this->Warning(d);
}
void DiagnosticPolicy::Error(std::string message) const {
DiagnosticDetail d;
d.message = std::move(message);
this->Error(d);
}
void DiagnosticPolicy::WarningDefaultAction(const DiagnosticDetail& detail) {
log()->warn(detail.FormatWarning());
}
void DiagnosticPolicy::ErrorDefaultAction(const DiagnosticDetail& detail) {
throw std::runtime_error(detail.FormatError());
}
void DiagnosticPolicy::Warning(const DiagnosticDetail& detail) const {
if (on_warning_ == nullptr) {
WarningDefaultAction(detail);
} else {
on_warning_(detail);
}
}
void DiagnosticPolicy::Error(const DiagnosticDetail& detail) const {
if (on_error_ == nullptr) {
ErrorDefaultAction(detail);
} else {
on_error_(detail);
}
}
} // namespace internal
} // namespace drake