7#include <tier0/convar.h>
15 for (
auto& [existing, existingProvider] : _sections)
19 existingProvider = std::move(provider);
23 _sections.emplace_back(std::move(name), std::move(provider));
28 return !_healthy || _healthy();
34 std::map<std::string, glz::raw_json> out;
35 for (
const auto& [name, provider] : _sections)
37 std::string section = provider();
38 out[name] = glz::validate_json(section) ? glz::raw_json{R
"({"error":"provider returned invalid JSON"})"}
39 : glz::raw_json{std::move(section)};
41 out["healthy"] = glz::raw_json{
IsHealthy() ?
"true" :
"false"};
48 for (
const auto& [name, provider] : _sections)
55 out +=
" <invalid JSON from provider>\n";
57 else if (section->is_object())
59 for (
const auto& [key, value] : section->get_object())
61 std::string rendered = value.is_string() ? value.get_string() : value.dump().value_or(std::string{});
62 out += std::format(
" {}: {}\n", key, rendered);
67 out += std::format(
" {}\n", section->dump().value_or(std::string{}));
70 if (!out.empty() && out.back() ==
'\n')
79 _healthy = std::move(healthy);
81 _command.emplace(name, helpText, [
this, name = std::string(name)](
const CCommand& args,
int) {
82 if (args.ArgC() > 1 && std::string_view(args.Arg(1)) ==
"json")
84 Msg(
"STATUS_JSON %s\n",
BuildJson().c_str());
87 Msg(
"=== %s (healthy: %s) ===\n%s\n", name.c_str(),
IsHealthy() ?
"yes" :
"no",
BuildText().c_str());
static std::string Write(const T &value)
Serialize value as compact JSON.
static Result< glz::generic > ParseDocument(std::string_view text)
Parse free-form JSON whose shape is not known at compile time.
std::function< std::string()> Provider
Returns this section's payload as JSON text (an object or scalar).
void InstallCommand(std::string_view name, std::string_view helpText, HealthCheck healthy={})
Install the server command that reports this status.
std::string BuildJson() const
One JSON object with a key per section, plus a top-level healthy flag.
bool IsHealthy() const
The registered health result, or true when no predicate is registered.
std::function< bool()> HealthCheck
Optional plugin-specific health predicate.
std::string BuildText() const
Human-readable multi-line rendering of the same sections.
void RegisterSection(std::string name, Provider provider)
Add a section, replacing any existing one with the same name.