-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: log fatal exceptions to the api
- Loading branch information
Showing
13 changed files
with
246 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "ApiLogger.h" | ||
#include "api/ApiRequestFactory.h" | ||
#include "api/ApiRequestException.h" | ||
#include "update/PluginVersion.h" | ||
|
||
namespace UKControllerPluginUtils::Log { | ||
|
||
struct ApiLogger::Impl | ||
{ | ||
[[nodiscard]] auto | ||
CreatePayloadNoMetadata(const std::string& type, const std::string& message) const -> nlohmann::json | ||
{ | ||
return {{"type", type}, {"message", message}, {"metadata", PluginVersionMetadata().dump()}}; | ||
} | ||
|
||
[[nodiscard]] auto CreatePayload( | ||
const std::string& type, const std::string& message, const nlohmann::json& metadata) const -> nlohmann::json | ||
{ | ||
auto metadataWithVersion = PluginVersionMetadata(); | ||
metadataWithVersion.update(metadata); | ||
return {{"type", type}, {"message", message}, {"metadata", metadataWithVersion.dump()}}; | ||
} | ||
|
||
[[nodiscard]] auto PluginVersionMetadata() const -> nlohmann::json | ||
{ | ||
return {{"plugin_version", UKControllerPlugin::Plugin::PluginVersion::version}}; | ||
} | ||
|
||
void WriteLog(const nlohmann::json& data) | ||
{ | ||
ApiRequest() | ||
.Post("plugin/logs", data) | ||
.Catch([](const Api::ApiRequestException& exception) { | ||
LogError( | ||
"Failed to send log to API, status code was " + | ||
std::to_string(static_cast<uint64_t>(exception.StatusCode()))); | ||
}) | ||
.Await(); | ||
} | ||
|
||
void WriteLogAsync(const nlohmann::json& data) | ||
{ | ||
ApiRequest().Post("plugin/logs", data).Catch([](const Api::ApiRequestException& exception) { | ||
LogError( | ||
"Failed to send log to API, status code was " + | ||
std::to_string(static_cast<uint64_t>(exception.StatusCode()))); | ||
}); | ||
} | ||
}; | ||
|
||
ApiLogger::ApiLogger() : impl(std::make_unique<Impl>()) | ||
{ | ||
} | ||
|
||
ApiLogger::~ApiLogger() = default; | ||
|
||
void ApiLogger::Log(const std::string& type, const std::string& message) const | ||
{ | ||
impl->WriteLog(impl->CreatePayloadNoMetadata(type, message)); | ||
} | ||
|
||
void ApiLogger::Log(const std::string& type, const std::string& message, const nlohmann::json& metadata) const | ||
{ | ||
impl->WriteLog(impl->CreatePayload(type, message, metadata)); | ||
} | ||
|
||
void ApiLogger::LogAsync(const std::string& type, const std::string& message) const | ||
{ | ||
impl->WriteLogAsync(impl->CreatePayloadNoMetadata(type, message)); | ||
} | ||
|
||
void ApiLogger::LogAsync(const std::string& type, const std::string& message, const nlohmann::json& metadata) const | ||
{ | ||
impl->WriteLogAsync(impl->CreatePayload(type, message, metadata)); | ||
} | ||
} // namespace UKControllerPluginUtils::Log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
#include "ApiLoggerInterface.h" | ||
|
||
namespace UKControllerPluginUtils::Log { | ||
class ApiLogger : public ApiLoggerInterface | ||
{ | ||
public: | ||
ApiLogger(); | ||
~ApiLogger() override; | ||
void Log(const std::string& type, const std::string& message) const override; | ||
void Log(const std::string& type, const std::string& message, const nlohmann::json& metadata) const override; | ||
void LogAsync(const std::string& type, const std::string& message) const override; | ||
void | ||
LogAsync(const std::string& type, const std::string& message, const nlohmann::json& metadata) const override; | ||
|
||
private: | ||
struct Impl; | ||
std::unique_ptr<Impl> impl; | ||
}; | ||
} // namespace UKControllerPluginUtils::Log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
namespace UKControllerPluginUtils::Log { | ||
/** | ||
* An interface for logging things to the API where we need more information | ||
* or just want to know what's going on. | ||
*/ | ||
class ApiLoggerInterface | ||
{ | ||
public: | ||
virtual ~ApiLoggerInterface() = default; | ||
virtual void Log(const std::string& type, const std::string& message) const = 0; | ||
virtual void Log(const std::string& type, const std::string& message, const nlohmann::json& metadata) const = 0; | ||
virtual void LogAsync(const std::string& type, const std::string& message) const = 0; | ||
virtual void | ||
LogAsync(const std::string& type, const std::string& message, const nlohmann::json& metadata) const = 0; | ||
}; | ||
} // namespace UKControllerPluginUtils::Log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "log/ApiLogger.h" | ||
#include "test/ApiTestCase.h" | ||
|
||
namespace UKControllerPluginUtilsTest::Api { | ||
class ApiLoggerTest : public UKControllerPluginTest::ApiTestCase | ||
{ | ||
public: | ||
ApiLoggerTest() : ApiTestCase() | ||
{ | ||
} | ||
|
||
UKControllerPluginUtils::Log::ApiLogger logger; | ||
}; | ||
|
||
TEST_F(ApiLoggerTest, ItLogsSync) | ||
{ | ||
const nlohmann::json expectedPayload = {{"type", "type"}, {"message", "message"}}; | ||
|
||
this->ExpectApiRequest()->Post().To("plugin/logs").WithBody(expectedPayload).WillReturnCreated(); | ||
logger.Log("type", "message"); | ||
} | ||
|
||
TEST_F(ApiLoggerTest, ItLogsSyncWithMetadata) | ||
{ | ||
const nlohmann::json metadata = {{"key", "value"}}; | ||
const nlohmann::json expectedPayload = { | ||
{"type", "type"}, {"message", "message"}, {"metadata", metadata.dump()}}; | ||
|
||
this->ExpectApiRequest()->Post().To("plugin/logs").WithBody(expectedPayload).WillReturnCreated(); | ||
logger.Log("type", "message", metadata); | ||
} | ||
|
||
TEST_F(ApiLoggerTest, ItLogsAsync) | ||
{ | ||
const nlohmann::json expectedPayload = {{"type", "type"}, {"message", "message"}}; | ||
|
||
this->ExpectApiRequest()->Post().To("plugin/logs").WithBody(expectedPayload).WillReturnCreated(); | ||
logger.LogAsync("type", "message"); | ||
} | ||
|
||
TEST_F(ApiLoggerTest, ItLogsAsyncWithMetadata) | ||
{ | ||
const nlohmann::json metadata = {{"key", "value"}}; | ||
const nlohmann::json expectedPayload = { | ||
{"type", "type"}, {"message", "message"}, {"metadata", metadata.dump()}}; | ||
|
||
this->ExpectApiRequest()->Post().To("plugin/logs").WithBody(expectedPayload).WillReturnCreated(); | ||
logger.LogAsync("type", "message", metadata); | ||
} | ||
} // namespace UKControllerPluginUtilsTest::Api |