VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
StatusService.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <functional>
5#include <optional>
6#include <string>
7#include <string_view>
8#include <utility>
9#include <vector>
10
11namespace VoltMod
12{
13
14/**
15 * @brief Build text and JSON diagnostics from named providers.
16 *
17 * Providers run on demand in registration order. VoltMod registers framework sections during
18 * load; plugins add their sections in Load and expose the combined report with
19 * @ref InstallCommand. A provider returns compact JSON text, normally built with
20 * `VoltMod::Json::Write` from `<VoltMod/Core/Text/Json.hpp>`.
21 */
23{
24public:
25 /** @brief Returns this section's payload as JSON text (an object or scalar). */
26 using Provider = std::function<std::string()>;
27
28 /** @brief Optional plugin-specific health predicate. */
29 using HealthCheck = std::function<bool()>;
30
31 /** @brief Add a section, replacing any existing one with the same name. */
32 void RegisterSection(std::string name, Provider provider);
33
34 /** @brief The registered health result, or true when no predicate is registered. */
35 bool IsHealthy() const;
36
37 /** @brief One JSON object with a key per section, plus a top-level `healthy` flag. */
38 std::string BuildJson() const;
39
40 /** @brief Human-readable multi-line rendering of the same sections. */
41 std::string BuildText() const;
42
43 /**
44 * @brief Install the server command that reports this status.
45 *
46 * `<name>` prints @ref BuildText. `<name> json` emits @ref BuildJson as one `STATUS_JSON {...}`
47 * line for RCON tooling. The command unregisters when this service is destroyed.
48 */
49 void InstallCommand(std::string_view name, std::string_view helpText, HealthCheck healthy = {});
50
51private:
52 std::vector<std::pair<std::string, Provider>> _sections;
53 HealthCheck _healthy;
54 std::optional<ServerCommand> _command;
55};
56
57} // namespace VoltMod
Build text and JSON diagnostics from named providers.
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.
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56