|
| 1 | +#include "ApiLogger.h" |
| 2 | +#include "api/ApiRequestFactory.h" |
| 3 | +#include "api/ApiRequestException.h" |
| 4 | +#include "update/PluginVersion.h" |
| 5 | + |
| 6 | +namespace UKControllerPluginUtils::Log { |
| 7 | + |
| 8 | + struct ApiLogger::Impl |
| 9 | + { |
| 10 | + [[nodiscard]] auto CreatePayloadNoMetadata(const std::string& type, const std::string& message) const |
| 11 | + -> nlohmann::json |
| 12 | + { |
| 13 | + return {{"type", type}, {"message", message}, {"metadata", PluginVersionMetadata().dump()}}; |
| 14 | + } |
| 15 | + |
| 16 | + [[nodiscard]] auto |
| 17 | + CreatePayload(const std::string& type, const std::string& message, const nlohmann::json& metadata) const |
| 18 | + -> nlohmann::json |
| 19 | + { |
| 20 | + auto metadataWithVersion = PluginVersionMetadata(); |
| 21 | + metadataWithVersion.update(metadata); |
| 22 | + return {{"type", type}, {"message", message}, {"metadata", metadataWithVersion.dump()}}; |
| 23 | + } |
| 24 | + |
| 25 | + [[nodiscard]] auto PluginVersionMetadata() const -> nlohmann::json |
| 26 | + { |
| 27 | + return {{"plugin_version", UKControllerPlugin::Plugin::PluginVersion::version}}; |
| 28 | + } |
| 29 | + |
| 30 | + void WriteLog(const nlohmann::json& data) |
| 31 | + { |
| 32 | + ApiRequest() |
| 33 | + .Post("plugin/logs", data) |
| 34 | + .Then([](const UKControllerPluginUtils::Api::Response& response) { |
| 35 | + const auto data = response.Data(); |
| 36 | + if (!data.is_object() || !data.contains("id") || !data["id"].is_string()) { |
| 37 | + LogError("Failed to send log to API, response was not as expected"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + LogInfo("Log sent to API with ID " + data["id"].get<std::string>()); |
| 42 | + }) |
| 43 | + .Catch([](const Api::ApiRequestException& exception) { |
| 44 | + LogError( |
| 45 | + "Failed to send log to API, status code was " + |
| 46 | + std::to_string(static_cast<uint64_t>(exception.StatusCode()))); |
| 47 | + }) |
| 48 | + .Await(); |
| 49 | + } |
| 50 | + |
| 51 | + void WriteLogAsync(const nlohmann::json& data) |
| 52 | + { |
| 53 | + ApiRequest().Post("plugin/logs", data).Catch([](const Api::ApiRequestException& exception) { |
| 54 | + LogError( |
| 55 | + "Failed to send log to API, status code was " + |
| 56 | + std::to_string(static_cast<uint64_t>(exception.StatusCode()))); |
| 57 | + }); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + ApiLogger::ApiLogger() : impl(std::make_unique<Impl>()) |
| 62 | + { |
| 63 | + } |
| 64 | + |
| 65 | + ApiLogger::~ApiLogger() = default; |
| 66 | + |
| 67 | + void ApiLogger::Log(const std::string& type, const std::string& message) const |
| 68 | + { |
| 69 | + impl->WriteLog(impl->CreatePayloadNoMetadata(type, message)); |
| 70 | + } |
| 71 | + |
| 72 | + void ApiLogger::Log(const std::string& type, const std::string& message, const nlohmann::json& metadata) const |
| 73 | + { |
| 74 | + impl->WriteLog(impl->CreatePayload(type, message, metadata)); |
| 75 | + } |
| 76 | + |
| 77 | + void ApiLogger::LogAsync(const std::string& type, const std::string& message) const |
| 78 | + { |
| 79 | + impl->WriteLogAsync(impl->CreatePayloadNoMetadata(type, message)); |
| 80 | + } |
| 81 | + |
| 82 | + void ApiLogger::LogAsync(const std::string& type, const std::string& message, const nlohmann::json& metadata) const |
| 83 | + { |
| 84 | + impl->WriteLogAsync(impl->CreatePayload(type, message, metadata)); |
| 85 | + } |
| 86 | +} // namespace UKControllerPluginUtils::Log |
0 commit comments