From f3f1ac56c025dc90be9ac268c8ef4b0dac4fd4ec Mon Sep 17 00:00:00 2001 From: sha512sum Date: Tue, 2 Jul 2024 16:28:32 +0000 Subject: [PATCH] Add logger --- examples/src/http.cpp | 3 + examples/src/log.cpp | 23 ++++++ .../components/loggable_component_base.hpp | 24 ++++++ include/cserver/components/logger.hpp | 78 +++++++++++++++++++ .../server/handlers/http_handler_base.hpp | 14 ++-- 5 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 examples/src/log.cpp create mode 100644 include/cserver/components/loggable_component_base.hpp create mode 100644 include/cserver/components/logger.hpp diff --git a/examples/src/http.cpp b/examples/src/http.cpp index 1228d51..dc393e0 100644 --- a/examples/src/http.cpp +++ b/examples/src/http.cpp @@ -17,9 +17,12 @@ struct SomeComponent : public cserver::server::handlers::HttpHandlerBaseWithAdde auto main() -> int { cserver::ServiceContextBuilder{} .AppendConfigParam<"threads", 8>() + .AppendConfigParam<"logging", cserver::ConstexprConfig{} + .Append<"level">(cserver::LoggingLevel::kWarning)>() .AppendConfigParam<"server", cserver::ConstexprConfig{} .Append<"taskProcessor">(utempl::ConstexprString{"basicTaskProcessor"}) .Append<"port">(55555)>() + .Append() .Append>() .Append() .Sort() diff --git a/examples/src/log.cpp b/examples/src/log.cpp new file mode 100644 index 0000000..8000096 --- /dev/null +++ b/examples/src/log.cpp @@ -0,0 +1,23 @@ +#include + + +struct SomeComponent : cserver::ComponentBase { + static constexpr utempl::ConstexprString kName = "component"; + using cserver::ComponentBase::ComponentBase; + + constexpr auto Run() -> void { + LOG_DEBUG<"Hello {}">("world!"); + }; +}; + + +auto main() -> int { + cserver::ServiceContextBuilder{} + .AppendConfigParam<"threads", 8>() + .AppendConfigParam<"logging", cserver::ConstexprConfig{} + .Append<"level">(cserver::LoggingLevel::kTrace)>() + .Append() + .Append() + .Sort() + .Run(); +}; diff --git a/include/cserver/components/loggable_component_base.hpp b/include/cserver/components/loggable_component_base.hpp new file mode 100644 index 0000000..93d3569 --- /dev/null +++ b/include/cserver/components/loggable_component_base.hpp @@ -0,0 +1,24 @@ +#pragma once +#include +#include + +namespace cserver { + +struct ComponentBase { + Logging& logging; + template + constexpr ComponentBase(T& context) : + logging(context.template FindComponent<"logging">()) {}; +}; + + +#define LOG_TRACE if(this->logging.level == ::cserver::LoggingLevel::kTrace) this->logging.Trace +#define LOG_DEBUG if(this->logging.level <= ::cserver::LoggingLevel::kDebug) this->logging.Debug +#define LOG_INFO if(this->logging.level <= ::cserver::LoggingLevel::kInfo) this->logging.Info +#define LOG_WARNING if(this->logging.level <= ::cserver::LoggingLevel::kWarning) this->logging.Warning +#define LOG_ERROR if(this->logging.level <= ::cserver::LoggingLevel::kError) this->logging.Error + + + + +} // namespace cserver diff --git a/include/cserver/components/logger.hpp b/include/cserver/components/logger.hpp new file mode 100644 index 0000000..03f0c7b --- /dev/null +++ b/include/cserver/components/logger.hpp @@ -0,0 +1,78 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace cserver { + + +enum class LoggingLevel { + kTrace, + kDebug, + kInfo, + kWarning, + kError +}; + +struct Logging { + static constexpr utempl::ConstexprString kName = "logging"; + LoggingLevel level; + template + constexpr Logging(T&) : level(T::kConfig.template Get().template Get<"level">()) { + std::ios::sync_with_stdio(false); + }; + + constexpr auto SetLoggingLevel(LoggingLevel level) { + this->level = level; + }; + + + template + static consteval auto GetFormatStringFor(utempl::ConstexprString fmt, utempl::ConstexprString level) { + constexpr auto f = FMT_COMPILE("[{}]: {}\n"); + constexpr auto size = N + NN + 6; + char data[size]{}; + fmt::format_to(data, f, static_cast(level), static_cast(fmt)); + return utempl::ConstexprString(data); + }; + + constexpr auto Log(std::string_view data) { + std::cout << data; + }; + + + template + constexpr auto Debug(Ts&&... args) { + static constexpr auto fmt = GetFormatStringFor(Fmt, utempl::ConstexprString{"DEBUG"}); + Log(fmt::format(FMT_COMPILE(fmt.data.begin()), std::forward(args)...)); + }; + + template + constexpr auto Info(Ts&&... args) { + static constexpr auto fmt = GetFormatStringFor(Fmt, utempl::ConstexprString{"INFO"}); + Log(fmt::format(FMT_COMPILE(fmt.data.begin()), std::forward(args)...)); + }; + + template + constexpr auto Trace(Ts&&... args) { + static constexpr auto fmt = GetFormatStringFor(Fmt, utempl::ConstexprString{"TRACE"}); + Log(fmt::format(FMT_COMPILE(fmt.data.begin()), std::forward(args)...)); + }; + + template + constexpr auto Error(Ts&&... args) { + static constexpr auto fmt = GetFormatStringFor(Fmt, utempl::ConstexprString{"ERROR"}); + Log(fmt::format(FMT_COMPILE(fmt.data.begin()), std::forward(args)...)); + }; + + template + constexpr auto Warning(Ts&&... args) { + static constexpr auto fmt = GetFormatStringFor(Fmt, utempl::ConstexprString{"WARNING"}); + Log(fmt::format(FMT_COMPILE(fmt.data.begin()), std::forward(args)...)); + }; +}; + + +} // namespace cserver diff --git a/include/cserver/server/handlers/http_handler_base.hpp b/include/cserver/server/handlers/http_handler_base.hpp index 5fa1139..cd08a8b 100644 --- a/include/cserver/server/handlers/http_handler_base.hpp +++ b/include/cserver/server/handlers/http_handler_base.hpp @@ -4,10 +4,11 @@ #include #include #include +#include namespace cserver::server::handlers { -struct HttpHandlerBase { +struct HttpHandlerBase : ComponentBase { template static consteval auto HttpHandlerAdder(const auto& context) { return context.TransformComponents([](const ComponentConfig) { @@ -21,9 +22,9 @@ struct HttpHandlerBase { try { co_return co_await std::forward(self).HandleRequestThrow(std::move(request)); } catch(const std::exception& err) { - fmt::println("Error in handler with default name {}: {}", T::kName, err.what()); + self.logging.template Warning<"Error in handler with default name {}: {}">(T::kName, err.what()); } catch(...) { - fmt::println("Error in handler with default name {}: Unknown Error", T::kName); + self.logging.template Warning<"Error in handler with default name {}: Unknown Error">(T::kName); }; co_return http::HttpResponse{.statusCode = 500, .statusMessage = "Internal Server Error", .body = "Internal Server Error"}; }; @@ -34,13 +35,14 @@ struct HttpHandlerBase { try { co_await std::forward(self).HandleRequestStreamThrow(std::move(request), stream); } catch(const std::exception& err) { - fmt::println("Error in handler with default name {}: {}", T::kName, err.what()); + self.logging.template Warning<"Error in handler with default name {}: {}">(T::kName, err.what()); } catch(...) { - fmt::println("Error in handler with default name {}: Unknown Error", T::kName); + self.logging.template Warning<"Error in handler with default name {}: Unknown Error">(T::kName); }; co_await stream.Close(); }; - inline constexpr HttpHandlerBase(auto&) {}; + inline constexpr HttpHandlerBase(auto& context) : + ComponentBase(context) {}; }; template struct HttpHandlerAdderType {