Skip to content

Commit cce9352

Browse files
committed
Swift: add visibility customization to diagnostics
1 parent dedbd9a commit cce9352

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

swift/logging/SwiftDiagnostics.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point
1919
}},
2020
{"visibility",
2121
{
22-
{"statusPage", true},
23-
{"cliSummaryTable", true},
24-
{"telemetry", true},
22+
{"statusPage", has(Visibility::statusPage)},
23+
{"cliSummaryTable", has(Visibility::cliSummaryTable)},
24+
{"telemetry", has(Visibility::telemetry)},
2525
}},
2626
{"severity", "error"},
2727
{"helpLinks", std::vector<std::string_view>(absl::StrSplit(helpLinks, ' '))},
@@ -41,6 +41,10 @@ std::string SwiftDiagnostic::abbreviation() const {
4141
return std::string{id};
4242
}
4343

44+
bool SwiftDiagnostic::has(SwiftDiagnostic::Visibility v) const {
45+
return (visibility & v) != Visibility::none;
46+
}
47+
4448
nlohmann::json SwiftDiagnosticsLocation::json() const {
4549
nlohmann::json ret{{"file", file}};
4650
if (startLine) ret["startLine"] = startLine;

swift/logging/SwiftDiagnostics.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,34 @@ struct SwiftDiagnosticsLocation {
2626
unsigned endColumn;
2727

2828
nlohmann::json json() const;
29-
3029
std::string str() const;
3130
};
3231

3332
// Models a diagnostic source for Swift, holding static information that goes out into a diagnostic
3433
// These are internally stored into a map on id's. A specific error log can use binlog's category
3534
// as id, which will then be used to recover the diagnostic source while dumping.
3635
struct SwiftDiagnostic {
36+
enum class Visibility : unsigned char {
37+
none = 0b000,
38+
statusPage = 0b001,
39+
cliSummaryTable = 0b010,
40+
telemetry = 0b100,
41+
all = 0b111,
42+
};
43+
3744
std::string_view id;
3845
std::string_view name;
3946
static constexpr std::string_view extractorName = "swift";
4047
std::string_view action;
4148
// space separated if more than 1. Not a vector to allow constexpr
4249
// TODO(C++20) with vector going constexpr this can be turned to `std::vector<std::string_view>`
4350
std::string_view helpLinks;
44-
45-
std::optional<SwiftDiagnosticsLocation> location;
4651
// for the moment, we only output errors, so no need to store the severity
4752

53+
Visibility visibility{Visibility::all};
54+
55+
std::optional<SwiftDiagnosticsLocation> location{};
56+
4857
// create a JSON diagnostics for this source with the given timestamp and message to out
4958
// A plaintextMessage is used that includes both the message and the action to take. Dots are
5059
// appended to both. The id is used to construct the source id in the form
@@ -64,8 +73,23 @@ struct SwiftDiagnostic {
6473
ret.location = SwiftDiagnosticsLocation{file, startLine, startColumn, endLine, endColumn};
6574
return ret;
6675
}
76+
77+
private:
78+
bool has(Visibility v) const;
6779
};
6880

81+
inline constexpr SwiftDiagnostic::Visibility operator|(SwiftDiagnostic::Visibility lhs,
82+
SwiftDiagnostic::Visibility rhs) {
83+
return static_cast<SwiftDiagnostic::Visibility>(static_cast<unsigned char>(lhs) |
84+
static_cast<unsigned char>(rhs));
85+
}
86+
87+
inline constexpr SwiftDiagnostic::Visibility operator&(SwiftDiagnostic::Visibility lhs,
88+
SwiftDiagnostic::Visibility rhs) {
89+
return static_cast<SwiftDiagnostic::Visibility>(static_cast<unsigned char>(lhs) &
90+
static_cast<unsigned char>(rhs));
91+
}
92+
6993
constexpr SwiftDiagnostic internalError{
7094
"internal-error",
7195
"Internal error",

0 commit comments

Comments
 (0)