diff options
| author | Stefan Boberg <[email protected]> | 2023-11-06 20:36:30 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-11-06 20:36:30 +0100 |
| commit | 07f288d6e119fc6bb524fb634bc9094109a2ab05 (patch) | |
| tree | 8531bd38d0d8c3567ba3d0a5603f549faf99632b /src/zenhttp | |
| parent | gc v2 tests (#512) (diff) | |
| download | zen-07f288d6e119fc6bb524fb634bc9094109a2ab05.tar.xz zen-07f288d6e119fc6bb524fb634bc9094109a2ab05.zip | |
spdlog implementation hiding (#498)
this change aims to hide logging internals from client code, in order to make it easier to extend and take more control over the logging process in the future.
As a bonus side effect, the generated code is much tighter (net delta around 2.5% on the resulting executable which includes lots of thirdparty code) and should take less time to compile and link.
Client usage via macros is pretty much unchanged. The main exposure client code had to spdlog internals before was the use of custom loggers per subsystem, where it would be common to have `spdlog::logger` references to keep a reference to a logger within a class. This is now replaced by `zen::LoggerRef` which currently simply encapsulates an actual `spdlog::logger` instance, but this is intended to be an implementation detail which will change in the future.
The way the change works is that we now handle any formatting of log messages in the zencore logging subsystem instead of relying on `spdlog` to manage this. We use the `fmt` library to do the formatting which means the client usage is identical to using `spdlog`. The formatted message is then forwarded onto any sinks etc which are still implememted via `spdlog`.
Diffstat (limited to 'src/zenhttp')
| -rw-r--r-- | src/zenhttp/auth/authmgr.cpp | 4 | ||||
| -rw-r--r-- | src/zenhttp/httpclient.cpp | 16 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpclient.h | 9 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpstats.h | 6 | ||||
| -rw-r--r-- | src/zenhttp/include/zenhttp/httpstatus.h | 6 | ||||
| -rw-r--r-- | src/zenhttp/servers/httpasio.cpp | 12 | ||||
| -rw-r--r-- | src/zenhttp/servers/httpmulti.cpp | 4 | ||||
| -rw-r--r-- | src/zenhttp/servers/httpnull.cpp | 4 | ||||
| -rw-r--r-- | src/zenhttp/servers/httpplugin.cpp | 4 | ||||
| -rw-r--r-- | src/zenhttp/servers/httpsys.cpp | 14 |
10 files changed, 39 insertions, 40 deletions
diff --git a/src/zenhttp/auth/authmgr.cpp b/src/zenhttp/auth/authmgr.cpp index f75e4edb2..e84f63a3f 100644 --- a/src/zenhttp/auth/authmgr.cpp +++ b/src/zenhttp/auth/authmgr.cpp @@ -493,10 +493,10 @@ private: using OpenIdProviderMap = std::unordered_map<std::string, std::unique_ptr<OpenIdProvider>>; using OpenIdTokenMap = std::unordered_map<std::string, OpenIdToken>; - spdlog::logger& Log() { return m_Log; } + LoggerRef Log() { return m_Log; } AuthConfig m_Config; - spdlog::logger& m_Log; + LoggerRef m_Log; BackgroundThread m_BackgroundThread; OpenIdProviderMap m_OpenIdProviders; OpenIdTokenMap m_OpenIdTokens; diff --git a/src/zenhttp/httpclient.cpp b/src/zenhttp/httpclient.cpp index caefce5f4..1299d51c1 100644 --- a/src/zenhttp/httpclient.cpp +++ b/src/zenhttp/httpclient.cpp @@ -22,6 +22,12 @@ ZEN_THIRD_PARTY_INCLUDES_START #include <cpr/cpr.h> ZEN_THIRD_PARTY_INCLUDES_END +#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC +# include <fcntl.h> +# include <sys/stat.h> +# include <unistd.h> +#endif + static std::atomic<uint32_t> HttpClientRequestIdCounter{0}; namespace zen { @@ -121,7 +127,7 @@ CommonResponse(cpr::Response&& HttpResponse, IoBuffer&& Payload = {}) struct HttpClient::Impl : public RefCounted { - Impl(spdlog::logger& Log); + Impl(LoggerRef Log); ~Impl(); // Session allocation @@ -169,7 +175,7 @@ struct HttpClient::Impl : public RefCounted return Result; } - spdlog::logger& Logger() { return Outer->Logger(); } + LoggerRef Logger() { return Outer->Logger(); } private: Impl* Outer; @@ -185,17 +191,17 @@ struct HttpClient::Impl : public RefCounted const KeyValueMap& AdditionalHeader, const KeyValueMap& Parameters); - spdlog::logger& Logger() { return m_Log; } + LoggerRef Logger() { return m_Log; } private: - spdlog::logger& m_Log; + LoggerRef m_Log; RwLock m_SessionLock; std::vector<cpr::Session*> m_Sessions; void ReleaseSession(cpr::Session*); }; -HttpClient::Impl::Impl(spdlog::logger& Log) : m_Log(Log) +HttpClient::Impl::Impl(LoggerRef Log) : m_Log(Log) { } diff --git a/src/zenhttp/include/zenhttp/httpclient.h b/src/zenhttp/include/zenhttp/httpclient.h index ae1e2bf20..9de5c7cce 100644 --- a/src/zenhttp/include/zenhttp/httpclient.h +++ b/src/zenhttp/include/zenhttp/httpclient.h @@ -5,16 +5,13 @@ #include "zenhttp.h" #include <zencore/iobuffer.h> +#include <zencore/logbase.h> #include <zencore/uid.h> #include <zenhttp/httpcommon.h> #include <optional> #include <unordered_map> -namespace spdlog { -class logger; -} // namespace spdlog - namespace zen { class CbPackage; @@ -148,13 +145,13 @@ public: return std::make_pair("Accept", MapContentTypeToString(ContentType)); } - spdlog::logger& Logger() { return m_Log; } + LoggerRef Logger() { return m_Log; } std::string_view GetBaseUri() const { return m_BaseUri; } private: struct Impl; - spdlog::logger& m_Log; + LoggerRef m_Log; std::string m_BaseUri; std::string m_SessionId; const HttpClientSettings m_ConnectionSettings; diff --git a/src/zenhttp/include/zenhttp/httpstats.h b/src/zenhttp/include/zenhttp/httpstats.h index 4a1cdcb20..e6fea6765 100644 --- a/src/zenhttp/include/zenhttp/httpstats.h +++ b/src/zenhttp/include/zenhttp/httpstats.h @@ -21,13 +21,13 @@ public: virtual void UnregisterHandler(std::string_view Id, IHttpStatsProvider& Provider) override; private: - spdlog::logger& m_Log; + LoggerRef m_Log; HttpRequestRouter m_Router; - inline spdlog::logger& Log() { return m_Log; } + inline LoggerRef Log() { return m_Log; } RwLock m_Lock; std::map<std::string, IHttpStatsProvider*> m_Providers; }; -} // namespace zen
\ No newline at end of file +} // namespace zen diff --git a/src/zenhttp/include/zenhttp/httpstatus.h b/src/zenhttp/include/zenhttp/httpstatus.h index b04e45324..093f62481 100644 --- a/src/zenhttp/include/zenhttp/httpstatus.h +++ b/src/zenhttp/include/zenhttp/httpstatus.h @@ -26,13 +26,13 @@ public: void UnregisterHandler(std::string_view Id, IHttpStatusProvider& Provider); private: - spdlog::logger& m_Log; + LoggerRef m_Log; HttpRequestRouter m_Router; RwLock m_Lock; std::map<std::string, IHttpStatusProvider*> m_Providers; - inline spdlog::logger& Log() { return m_Log; } + inline LoggerRef Log() { return m_Log; } }; -} // namespace zen
\ No newline at end of file +} // namespace zen diff --git a/src/zenhttp/servers/httpasio.cpp b/src/zenhttp/servers/httpasio.cpp index 0c6b189f9..44398d92d 100644 --- a/src/zenhttp/servers/httpasio.cpp +++ b/src/zenhttp/servers/httpasio.cpp @@ -47,18 +47,18 @@ static constinit uint32_t HashSession = HashStringAsLowerDjb2("UE-Session"sv); static constinit uint32_t HashRequest = HashStringAsLowerDjb2("UE-Request"sv); static constinit uint32_t HashRange = HashStringAsLowerDjb2("Range"sv); -inline spdlog::logger& +inline LoggerRef InitLogger() { - spdlog::logger& Logger = logging::Get("asio"); + LoggerRef Logger = logging::Get("asio"); // Logger.set_level(spdlog::level::trace); return Logger; } -inline spdlog::logger& +inline LoggerRef Log() { - static spdlog::logger& g_Logger = InitLogger(); + static LoggerRef g_Logger = InitLogger(); return g_Logger; } @@ -1007,7 +1007,7 @@ HttpAsioServer::Run(bool IsInteractive) #if ZEN_PLATFORM_WINDOWS if (TestMode == false) { - zen::logging::ConsoleLog().info("Zen Server running (asio HTTP). Press ESC or Q to quit"); + ZEN_CONSOLE("Zen Server running (asio HTTP). Press ESC or Q to quit"); } do @@ -1027,7 +1027,7 @@ HttpAsioServer::Run(bool IsInteractive) #else if (TestMode == false) { - zen::logging::ConsoleLog().info("Zen Server running (asio HTTP). Ctrl-C to quit"); + ZEN_CONSOLE("Zen Server running (asio HTTP). Ctrl-C to quit"); } do diff --git a/src/zenhttp/servers/httpmulti.cpp b/src/zenhttp/servers/httpmulti.cpp index 6b7060d5e..d8ebdc9c0 100644 --- a/src/zenhttp/servers/httpmulti.cpp +++ b/src/zenhttp/servers/httpmulti.cpp @@ -63,7 +63,7 @@ HttpMultiServer::Run(bool IsInteractiveSession) #if ZEN_PLATFORM_WINDOWS if (TestMode == false) { - zen::logging::ConsoleLog().info("Zen Server running (multi server). Press ESC or Q to quit"); + ZEN_CONSOLE("Zen Server running (multi server). Press ESC or Q to quit"); } do @@ -83,7 +83,7 @@ HttpMultiServer::Run(bool IsInteractiveSession) #else if (TestMode == false) { - zen::logging::ConsoleLog().info("Zen Server running (null HTTP). Ctrl-C to quit"); + ZEN_CONSOLE("Zen Server running (null HTTP). Ctrl-C to quit"); } do diff --git a/src/zenhttp/servers/httpnull.cpp b/src/zenhttp/servers/httpnull.cpp index 658f51831..7d3e9079a 100644 --- a/src/zenhttp/servers/httpnull.cpp +++ b/src/zenhttp/servers/httpnull.cpp @@ -44,7 +44,7 @@ HttpNullServer::Run(bool IsInteractiveSession) #if ZEN_PLATFORM_WINDOWS if (TestMode == false) { - zen::logging::ConsoleLog().info("Zen Server running (null HTTP). Press ESC or Q to quit"); + ZEN_CONSOLE("Zen Server running (null HTTP). Press ESC or Q to quit"); } do @@ -64,7 +64,7 @@ HttpNullServer::Run(bool IsInteractiveSession) #else if (TestMode == false) { - zen::logging::ConsoleLog().info("Zen Server running (null HTTP). Ctrl-C to quit"); + ZEN_CONSOLE("Zen Server running (null HTTP). Ctrl-C to quit"); } do diff --git a/src/zenhttp/servers/httpplugin.cpp b/src/zenhttp/servers/httpplugin.cpp index 3c727763b..4ae7cd87a 100644 --- a/src/zenhttp/servers/httpplugin.cpp +++ b/src/zenhttp/servers/httpplugin.cpp @@ -651,7 +651,7 @@ HttpPluginServerImpl::Run(bool IsInteractive) # if ZEN_PLATFORM_WINDOWS if (TestMode == false) { - zen::logging::ConsoleLog().info("Zen Server running (plugin HTTP). Press ESC or Q to quit"); + ZEN_CONSOLE("Zen Server running (plugin HTTP). Press ESC or Q to quit"); } do @@ -671,7 +671,7 @@ HttpPluginServerImpl::Run(bool IsInteractive) # else if (TestMode == false) { - zen::logging::ConsoleLog().info("Zen Server running (plugin HTTP). Ctrl-C to quit"); + ZEN_CONSOLE("Zen Server running (plugin HTTP). Ctrl-C to quit"); } do diff --git a/src/zenhttp/servers/httpsys.cpp b/src/zenhttp/servers/httpsys.cpp index f1cd3e3fc..4eeab6662 100644 --- a/src/zenhttp/servers/httpsys.cpp +++ b/src/zenhttp/servers/httpsys.cpp @@ -23,10 +23,6 @@ # include <http.h> -namespace spdlog { -class logger; -} - namespace zen { /** @@ -70,9 +66,9 @@ private: void UnregisterService(const char* Endpoint, HttpService& Service); private: - spdlog::logger& m_Log; - spdlog::logger& m_RequestLog; - spdlog::logger& Log() { return m_Log; } + LoggerRef m_Log; + LoggerRef m_RequestLog; + LoggerRef Log() { return m_Log; } bool m_IsOk = false; bool m_IsHttpInitialized = false; @@ -1239,7 +1235,7 @@ HttpSysServer::Run(bool IsInteractive) { if (IsInteractive) { - zen::logging::ConsoleLog().info("Zen Server running (http.sys). Press ESC or Q to quit"); + ZEN_CONSOLE("Zen Server running (http.sys). Press ESC or Q to quit"); } do @@ -1525,7 +1521,7 @@ HttpSysTransaction::HandleCompletion(ULONG IoResult, ULONG_PTR NumberOfBytesTran { if (m_HandlerRequest.has_value()) { - m_HttpServer.m_RequestLog.info("{} {}", ToString(m_HandlerRequest->RequestVerb()), m_HandlerRequest->RelativeUri()); + ZEN_LOG_INFO(m_HttpServer.m_RequestLog, "{} {}", ToString(m_HandlerRequest->RequestVerb()), m_HandlerRequest->RelativeUri()); } } |