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/zenserver | |
| 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/zenserver')
| -rw-r--r-- | src/zenserver/admin/admin.cpp | 15 | ||||
| -rw-r--r-- | src/zenserver/cache/httpstructuredcache.h | 8 | ||||
| -rw-r--r-- | src/zenserver/cache/structuredcachestore.cpp | 5 | ||||
| -rw-r--r-- | src/zenserver/config.cpp | 12 | ||||
| -rw-r--r-- | src/zenserver/diag/diagsvcs.cpp | 5 | ||||
| -rw-r--r-- | src/zenserver/diag/logging.cpp | 4 | ||||
| -rw-r--r-- | src/zenserver/main.cpp | 4 | ||||
| -rw-r--r-- | src/zenserver/projectstore/httpprojectstore.h | 4 | ||||
| -rw-r--r-- | src/zenserver/projectstore/projectstore.cpp | 4 | ||||
| -rw-r--r-- | src/zenserver/projectstore/projectstore.h | 9 | ||||
| -rw-r--r-- | src/zenserver/sentryintegration.cpp | 20 | ||||
| -rw-r--r-- | src/zenserver/sentryintegration.h | 5 | ||||
| -rw-r--r-- | src/zenserver/upstream/jupiter.h | 12 | ||||
| -rw-r--r-- | src/zenserver/upstream/upstreamcache.cpp | 13 | ||||
| -rw-r--r-- | src/zenserver/upstream/zen.h | 12 | ||||
| -rw-r--r-- | src/zenserver/zenserver.h | 2 |
16 files changed, 71 insertions, 63 deletions
diff --git a/src/zenserver/admin/admin.cpp b/src/zenserver/admin/admin.cpp index 83f33d4ea..00a5c79ed 100644 --- a/src/zenserver/admin/admin.cpp +++ b/src/zenserver/admin/admin.cpp @@ -5,6 +5,7 @@ #include <zencore/compactbinarybuilder.h> #include <zencore/fmtutils.h> #include <zencore/jobqueue.h> +#include <zencore/logging.h> #include <zencore/string.h> #if ZEN_WITH_TRACE @@ -23,10 +24,6 @@ #include <chrono> -ZEN_THIRD_PARTY_INCLUDES_START -#include <spdlog/spdlog.h> -ZEN_THIRD_PARTY_INCLUDES_END - namespace zen { HttpAdminService::HttpAdminService(GcScheduler& Scheduler, @@ -563,8 +560,8 @@ HttpAdminService::HttpAdminService(GcScheduler& Scheduler, m_Router.RegisterRoute( "logs", [this](HttpRouterRequest& Req) { - CbObjectWriter Obj; - spdlog::string_view_t LogLevel = spdlog::level::to_string_view(spdlog::get_level()); + CbObjectWriter Obj; + auto LogLevel = logging::level::ToStringView(logging::GetLogLevel()); Obj.AddString("loglevel", std::string_view(LogLevel.data(), LogLevel.size())); Obj.AddString("Logfile", PathToUtf8(m_LogPaths.AbsLogPath)); Obj.BeginObject("cache"); @@ -615,15 +612,15 @@ HttpAdminService::HttpAdminService(GcScheduler& Scheduler, } if (std::string Param(Params.GetValue("loglevel")); Param.empty() == false) { - spdlog::level::level_enum NewLevel = spdlog::level::from_str(Param); - spdlog::string_view_t LogLevel = spdlog::level::to_string_view(NewLevel); + logging::level::LogLevel NewLevel = logging::level::ParseLogLevelString(Param); + std::string_view LogLevel = logging::level::ToStringView(NewLevel); if (LogLevel != Param) { return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest, HttpContentType::kText, fmt::format("Invalid log level '{}'", Param)); } - spdlog::set_level(NewLevel); + logging::SetLogLevel(NewLevel); if (StringBuilder.Size() > 0) { StringBuilder.Append(", "); diff --git a/src/zenserver/cache/httpstructuredcache.h b/src/zenserver/cache/httpstructuredcache.h index 75ae1711f..57a533029 100644 --- a/src/zenserver/cache/httpstructuredcache.h +++ b/src/zenserver/cache/httpstructuredcache.h @@ -12,10 +12,6 @@ #include <memory> #include <vector> -namespace spdlog { -class logger; -} - namespace zen { struct CacheChunkRequest; @@ -178,8 +174,8 @@ private: bool AreDiskWritesAllowed() const; - spdlog::logger& Log() { return m_Log; } - spdlog::logger& m_Log; + LoggerRef Log() { return m_Log; } + LoggerRef m_Log; ZenCacheStore& m_CacheStore; HttpStatsService& m_StatsService; HttpStatusService& m_StatusService; diff --git a/src/zenserver/cache/structuredcachestore.cpp b/src/zenserver/cache/structuredcachestore.cpp index db6156ea1..236a0e1d7 100644 --- a/src/zenserver/cache/structuredcachestore.cpp +++ b/src/zenserver/cache/structuredcachestore.cpp @@ -40,6 +40,7 @@ ZEN_THIRD_PARTY_INCLUDES_END # include <zencore/workthreadpool.h> # include <zenstore/cidstore.h> # include <random> +# include <unordered_map> #endif namespace zen { @@ -310,9 +311,9 @@ ZenCacheStore::LogWorker() { SetCurrentThreadName("ZenCacheStore::LogWorker"); - spdlog::logger& ZCacheLog(logging::Get("z$")); + LoggerRef ZCacheLog(logging::Get("z$")); - auto Log = [&ZCacheLog]() { return ZCacheLog; }; + auto Log = [&ZCacheLog]() -> LoggerRef { return ZCacheLog; }; std::vector<AccessLogItem> Items; while (true) diff --git a/src/zenserver/config.cpp b/src/zenserver/config.cpp index 43d517520..8fe4213cb 100644 --- a/src/zenserver/config.cpp +++ b/src/zenserver/config.cpp @@ -23,8 +23,12 @@ ZEN_THIRD_PARTY_INCLUDES_END # include <conio.h> #else # include <pwd.h> +# include <unistd.h> #endif +#include <unordered_map> +#include <unordered_set> + #if ZEN_PLATFORM_WINDOWS // Used for getting My Documents for default data directory @@ -1325,9 +1329,9 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) if (result.count("help")) { - zen::logging::ConsoleLog().info("{}", options.help()); + ZEN_CONSOLE("{}", options.help()); #if ZEN_PLATFORM_WINDOWS - zen::logging::ConsoleLog().info("Press any key to exit!"); + ZEN_CONSOLE("Press any key to exit!"); _getch(); #else // Assume the user's in a terminal on all other platforms and that @@ -1368,13 +1372,13 @@ ParseCliOptions(int argc, char* argv[], ZenServerOptions& ServerOptions) } catch (cxxopts::OptionParseException& e) { - zen::logging::ConsoleLog().error("Error parsing zenserver arguments: {}\n\n{}", e.what(), options.help()); + ZEN_CONSOLE_ERROR("Error parsing zenserver arguments: {}\n\n{}", e.what(), options.help()); throw; } catch (zen::OptionParseException& e) { - zen::logging::ConsoleLog().error("Error parsing zenserver arguments: {}\n\n{}", e.what(), options.help()); + ZEN_CONSOLE_ERROR("Error parsing zenserver arguments: {}\n\n{}", e.what(), options.help()); throw; } diff --git a/src/zenserver/diag/diagsvcs.cpp b/src/zenserver/diag/diagsvcs.cpp index 93c2eafc3..1d62b4d17 100644 --- a/src/zenserver/diag/diagsvcs.cpp +++ b/src/zenserver/diag/diagsvcs.cpp @@ -11,7 +11,10 @@ #include <fstream> #include <sstream> +ZEN_THIRD_PARTY_INCLUDES_START +#include <spdlog/logger.h> #include <json11.hpp> +ZEN_THIRD_PARTY_INCLUDES_END namespace zen { @@ -75,7 +78,7 @@ HttpHealthService::HttpHealthService() [this](HttpRouterRequest& RoutedReq) { HttpServerRequest& HttpReq = RoutedReq.ServerRequest(); - zen::Log().flush(); + zen::Log().SpdLogger->flush(); std::filesystem::path Path = [&] { RwLock::SharedLockScope _(m_InfoLock); diff --git a/src/zenserver/diag/logging.cpp b/src/zenserver/diag/logging.cpp index 6a27ea13e..e2d57b840 100644 --- a/src/zenserver/diag/logging.cpp +++ b/src/zenserver/diag/logging.cpp @@ -11,6 +11,10 @@ #include <zenutil/logging.h> #include <zenutil/logging/rotatingfilesink.h> +ZEN_THIRD_PARTY_INCLUDES_START +#include <spdlog/spdlog.h> +ZEN_THIRD_PARTY_INCLUDES_END + namespace zen { void diff --git a/src/zenserver/main.cpp b/src/zenserver/main.cpp index b83964319..69cc2bbf5 100644 --- a/src/zenserver/main.cpp +++ b/src/zenserver/main.cpp @@ -244,7 +244,7 @@ ZenEntryPoint::Run() } catch (std::exception& e) { - SPDLOG_CRITICAL("Caught exception in main: {}", e.what()); + ZEN_CRITICAL("Caught exception in main: {}", e.what()); if (!IsApplicationExitRequested()) { RequestApplicationExit(1); @@ -297,7 +297,7 @@ test_main(int argc, char** argv) zen::z$service_forcelink(); zen::logging::InitializeLogging(); - spdlog::set_level(spdlog::level::debug); + zen::logging::SetLogLevel(zen::logging::level::Debug); zen::MaximizeOpenFileCount(); diff --git a/src/zenserver/projectstore/httpprojectstore.h b/src/zenserver/projectstore/httpprojectstore.h index ac12d4d7c..9998ae83e 100644 --- a/src/zenserver/projectstore/httpprojectstore.h +++ b/src/zenserver/projectstore/httpprojectstore.h @@ -81,9 +81,9 @@ private: void HandleOplogDetailsRequest(HttpRouterRequest& Req); void HandleOplogOpDetailsRequest(HttpRouterRequest& Req); - inline spdlog::logger& Log() { return m_Log; } + inline LoggerRef Log() { return m_Log; } - spdlog::logger& m_Log; + LoggerRef m_Log; CidStore& m_CidStore; HttpRequestRouter m_Router; Ref<ProjectStore> m_ProjectStore; diff --git a/src/zenserver/projectstore/projectstore.cpp b/src/zenserver/projectstore/projectstore.cpp index ba30dcd7b..9fedd9165 100644 --- a/src/zenserver/projectstore/projectstore.cpp +++ b/src/zenserver/projectstore/projectstore.cpp @@ -454,7 +454,7 @@ struct ProjectStore::OplogStorage : public RefCounted uint32_t GetMaxLsn() const { return m_MaxLsn.load(); } - spdlog::logger& Log() { return m_OwnerOplog->Log(); } + LoggerRef Log() { return m_OwnerOplog->Log(); } private: ProjectStore::Oplog* m_OwnerOplog; @@ -1349,7 +1349,7 @@ ProjectStore::Project::WriteAccessTimes() } } -spdlog::logger& +LoggerRef ProjectStore::Project::Log() { return m_ProjectStore->Log(); diff --git a/src/zenserver/projectstore/projectstore.h b/src/zenserver/projectstore/projectstore.h index fbff1444d..fe1068485 100644 --- a/src/zenserver/projectstore/projectstore.h +++ b/src/zenserver/projectstore/projectstore.h @@ -13,6 +13,7 @@ ZEN_THIRD_PARTY_INCLUDES_START ZEN_THIRD_PARTY_INCLUDES_END #include <map> +#include <unordered_map> namespace zen { @@ -123,7 +124,7 @@ public: const std::filesystem::path& TempPath() const { return m_TempPath; } const std::filesystem::path& MarkerPath() const { return m_MarkerPath; } - spdlog::logger& Log() { return m_OuterProject->Log(); } + LoggerRef Log() { return m_OuterProject->Log(); } void Flush(); void ScrubStorage(ScrubContext& Ctx) const; void GatherReferences(GcContext& GcCtx); @@ -242,7 +243,7 @@ public: [[nodiscard]] static bool Exists(const std::filesystem::path& BasePath); void Flush(); void ScrubStorage(ScrubContext& Ctx); - spdlog::logger& Log(); + LoggerRef Log(); void GatherReferences(GcContext& GcCtx); uint64_t TotalSize() const; bool PrepareForDelete(std::filesystem::path& OutDeletePath); @@ -285,7 +286,7 @@ public: void DiscoverProjects(); void IterateProjects(std::function<void(Project& Prj)>&& Fn); - spdlog::logger& Log() { return m_Log; } + LoggerRef Log() { return m_Log; } const std::filesystem::path& BasePath() const { return m_ProjectBasePath; } virtual void GatherReferences(GcContext& GcCtx) override; @@ -368,7 +369,7 @@ public: bool AreDiskWritesAllowed() const; private: - spdlog::logger& m_Log; + LoggerRef m_Log; GcManager& m_Gc; CidStore& m_CidStore; JobQueue& m_JobQueue; diff --git a/src/zenserver/sentryintegration.cpp b/src/zenserver/sentryintegration.cpp index 10a05c3c8..755fe97db 100644 --- a/src/zenserver/sentryintegration.cpp +++ b/src/zenserver/sentryintegration.cpp @@ -16,6 +16,10 @@ # include <pwd.h> #endif +ZEN_THIRD_PARTY_INCLUDES_START +#include <spdlog/spdlog.h> +ZEN_THIRD_PARTY_INCLUDES_END + #if ZEN_USE_SENTRY # define SENTRY_BUILD_STATIC 1 ZEN_THIRD_PARTY_INCLUDES_START @@ -162,23 +166,23 @@ SentryLogFunction(sentry_level_t Level, const char* Message, va_list Args, [[may switch (Level) { case SENTRY_LEVEL_DEBUG: - ConsoleLog().debug("sentry: {}", MessagePtr); + ZEN_CONSOLE_DEBUG("sentry: {}", MessagePtr); break; case SENTRY_LEVEL_INFO: - ConsoleLog().info("sentry: {}", MessagePtr); + ZEN_CONSOLE_INFO("sentry: {}", MessagePtr); break; case SENTRY_LEVEL_WARNING: - ConsoleLog().warn("sentry: {}", MessagePtr); + ZEN_CONSOLE_WARN("sentry: {}", MessagePtr); break; case SENTRY_LEVEL_ERROR: - ConsoleLog().error("sentry: {}", MessagePtr); + ZEN_CONSOLE_ERROR("sentry: {}", MessagePtr); break; case SENTRY_LEVEL_FATAL: - ConsoleLog().critical("sentry: {}", MessagePtr); + ZEN_CONSOLE_CRITICAL("sentry: {}", MessagePtr); break; } } @@ -192,7 +196,7 @@ SentryIntegration::~SentryIntegration() { if (m_IsInitialized && m_SentryErrorCode == 0) { - logging::SetErrorLog(std::shared_ptr<spdlog::logger>()); + logging::SetErrorLog(""); m_SentryAssert.reset(); sentry_close(); } @@ -250,8 +254,8 @@ SentryIntegration::Initialize(std::string SentryDatabasePath, std::string Sentry sentry_set_user(SentryUserObject); } - auto SentrySink = spdlog::create<sentry::sentry_sink>("sentry"); - logging::SetErrorLog(std::move(SentrySink)); + m_SentryLogger = spdlog::create<sentry::sentry_sink>("sentry"); + logging::SetErrorLog("sentry"); m_SentryAssert = std::make_unique<sentry::SentryAssertImpl>(); } diff --git a/src/zenserver/sentryintegration.h b/src/zenserver/sentryintegration.h index f25cf5dce..fddba8882 100644 --- a/src/zenserver/sentryintegration.h +++ b/src/zenserver/sentryintegration.h @@ -18,6 +18,10 @@ # include <memory> +ZEN_THIRD_PARTY_INCLUDES_START +# include <spdlog/logger.h> +ZEN_THIRD_PARTY_INCLUDES_END + namespace sentry { struct SentryAssertImpl; @@ -42,6 +46,7 @@ private: bool m_AllowPII = false; std::unique_ptr<sentry::SentryAssertImpl> m_SentryAssert; std::string m_SentryUserName; + std::shared_ptr<spdlog::logger> m_SentryLogger; }; } // namespace zen diff --git a/src/zenserver/upstream/jupiter.h b/src/zenserver/upstream/jupiter.h index ca84905d2..467f28501 100644 --- a/src/zenserver/upstream/jupiter.h +++ b/src/zenserver/upstream/jupiter.h @@ -131,15 +131,15 @@ public: CloudCacheClient& Client() { return *m_CacheClient; }; private: - inline spdlog::logger& Log() { return m_Log; } - cpr::Session& GetSession(); - CloudCacheAccessToken GetAccessToken(bool RefreshToken = false); + inline LoggerRef Log() { return m_Log; } + cpr::Session& GetSession(); + CloudCacheAccessToken GetAccessToken(bool RefreshToken = false); CloudCacheResult CacheTypeExists(std::string_view Namespace, std::string_view TypeId, const IoHash& Key); CloudCacheExistsResult CacheTypeExists(std::string_view Namespace, std::string_view TypeId, const std::set<IoHash>& Keys); - spdlog::logger& m_Log; + LoggerRef m_Log; RefPtr<CloudCacheClient> m_CacheClient; detail::CloudCacheSessionState* m_SessionState; }; @@ -195,10 +195,10 @@ public: std::string_view ComputeCluster() const { return m_ComputeCluster; } std::string_view ServiceUrl() const { return m_ServiceUrl; } - spdlog::logger& Logger() { return m_Log; } + LoggerRef Logger() { return m_Log; } private: - spdlog::logger& m_Log; + LoggerRef m_Log; std::string m_ServiceUrl; std::string m_DefaultDdcNamespace; std::string m_DefaultBlobStoreNamespace; diff --git a/src/zenserver/upstream/upstreamcache.cpp b/src/zenserver/upstream/upstreamcache.cpp index 5f711d0bc..8e00f86b0 100644 --- a/src/zenserver/upstream/upstreamcache.cpp +++ b/src/zenserver/upstream/upstreamcache.cpp @@ -29,7 +29,6 @@ #include <atomic> #include <shared_mutex> #include <thread> -#include <unordered_map> namespace zen { @@ -753,10 +752,10 @@ namespace detail { return {.Bytes = TotalBytes, .ElapsedSeconds = TotalElapsedSeconds, .Success = true}; } - spdlog::logger& Log() { return m_Log; } + LoggerRef Log() { return m_Log; } AuthMgr& m_AuthMgr; - spdlog::logger& m_Log; + LoggerRef m_Log; UpstreamEndpointInfo m_Info; UpstreamStatus m_Status; UpstreamEndpointStats m_Stats; @@ -1460,9 +1459,9 @@ namespace detail { return m_Endpoints.front(); } - spdlog::logger& Log() { return m_Log; } + LoggerRef Log() { return m_Log; } - spdlog::logger& m_Log; + LoggerRef m_Log; UpstreamEndpointInfo m_Info; UpstreamStatus m_Status; UpstreamEndpointStats m_Stats; @@ -2078,7 +2077,7 @@ private: } } - spdlog::logger& Log() { return m_Log; } + LoggerRef Log() { return m_Log; } using UpstreamQueue = BlockingQueue<UpstreamCacheRecord>; @@ -2103,7 +2102,7 @@ private: } }; - spdlog::logger& m_Log; + LoggerRef m_Log; UpstreamCacheOptions m_Options; ZenCacheStore& m_CacheStore; CidStore& m_CidStore; diff --git a/src/zenserver/upstream/zen.h b/src/zenserver/upstream/zen.h index bfba8fa98..c1e4fbd0f 100644 --- a/src/zenserver/upstream/zen.h +++ b/src/zenserver/upstream/zen.h @@ -20,10 +20,6 @@ ZEN_THIRD_PARTY_INCLUDES_END struct ZenCacheValue; -namespace spdlog { -class logger; -} - namespace zen { class CbObjectWriter; @@ -85,9 +81,9 @@ public: ZenCacheResult InvokeRpc(const CbPackage& Package); private: - inline spdlog::logger& Log() { return m_Log; } + inline LoggerRef Log() { return m_Log; } - spdlog::logger& m_Log; + LoggerRef m_Log; Ref<ZenStructuredCacheClient> m_Client; detail::ZenCacheSessionState* m_SessionState; }; @@ -105,10 +101,10 @@ public: std::string_view ServiceUrl() const { return m_ServiceUrl; } - inline spdlog::logger& Log() { return m_Log; } + inline LoggerRef Log() { return m_Log; } private: - spdlog::logger& m_Log; + LoggerRef m_Log; std::string m_ServiceUrl; std::chrono::milliseconds m_ConnectTimeout; std::chrono::milliseconds m_Timeout; diff --git a/src/zenserver/zenserver.h b/src/zenserver/zenserver.h index de069be69..7da536708 100644 --- a/src/zenserver/zenserver.h +++ b/src/zenserver/zenserver.h @@ -10,9 +10,7 @@ #include <string_view> ZEN_THIRD_PARTY_INCLUDES_START -#include <fmt/format.h> #include <asio.hpp> -#include <lua.hpp> ZEN_THIRD_PARTY_INCLUDES_END ////////////////////////////////////////////////////////////////////////// |