aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver
diff options
context:
space:
mode:
Diffstat (limited to 'src/zenserver')
-rw-r--r--src/zenserver/admin/admin.cpp83
-rw-r--r--src/zenserver/admin/admin.h11
-rw-r--r--src/zenserver/cache/httpstructuredcache.cpp8
-rw-r--r--src/zenserver/cache/structuredcachestore.cpp41
-rw-r--r--src/zenserver/cache/structuredcachestore.h11
-rw-r--r--src/zenserver/zenserver.cpp23
6 files changed, 151 insertions, 26 deletions
diff --git a/src/zenserver/admin/admin.cpp b/src/zenserver/admin/admin.cpp
index 05ae0a24a..083086f50 100644
--- a/src/zenserver/admin/admin.cpp
+++ b/src/zenserver/admin/admin.cpp
@@ -10,14 +10,24 @@
#endif // ZEN_WITH_TRACE
#include <zenstore/gc.h>
+#include "cache/structuredcachestore.h"
#include <chrono>
+ZEN_THIRD_PARTY_INCLUDES_START
+#include <spdlog/spdlog.h>
+ZEN_THIRD_PARTY_INCLUDES_END
+
namespace zen {
-HttpAdminService::HttpAdminService(GcScheduler& Scheduler, JobQueue& BackgroundJobQueue)
+HttpAdminService::HttpAdminService(GcScheduler& Scheduler,
+ JobQueue& BackgroundJobQueue,
+ ZenCacheStore& CacheStore,
+ const LogPaths& LogPaths)
: m_GcScheduler(Scheduler)
, m_BackgroundJobQueue(BackgroundJobQueue)
+, m_CacheStore(CacheStore)
+, m_LogPaths(LogPaths)
{
using namespace std::literals;
@@ -315,6 +325,77 @@ HttpAdminService::HttpAdminService(GcScheduler& Scheduler, JobQueue& BackgroundJ
},
HttpVerb::kPost);
#endif // ZEN_WITH_TRACE
+
+ m_Router.RegisterRoute(
+ "logs",
+ [this](HttpRouterRequest& Req) {
+ CbObjectWriter Obj;
+ spdlog::string_view_t LogLevel = spdlog::level::to_string_view(spdlog::get_level());
+ Obj.AddString("loglevel", std::string_view(LogLevel.data(), LogLevel.size()));
+ Obj.AddString("Logfile", PathToUtf8(m_LogPaths.AbsLogPath));
+ Obj.BeginObject("cache");
+ {
+ const ZenCacheStore::Configuration& CacheConfig = m_CacheStore.GetConfiguration();
+ Obj.AddString("Logfile", PathToUtf8(m_LogPaths.CacheLogPath));
+ Obj.AddBool("Write", CacheConfig.Logging.EnableWriteLog);
+ Obj.AddBool("Access", CacheConfig.Logging.EnableAccessLog);
+ }
+ Obj.EndObject();
+ Obj.BeginObject("http");
+ {
+ Obj.AddString("Logfile", PathToUtf8(m_LogPaths.HttpLogPath));
+ }
+ Obj.EndObject();
+ Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Obj.Save());
+ },
+ HttpVerb::kGet);
+
+ m_Router.RegisterRoute(
+ "logs",
+ [this](HttpRouterRequest& Req) {
+ HttpServerRequest& HttpReq = Req.ServerRequest();
+ const HttpServerRequest::QueryParams Params = HttpReq.GetQueryParams();
+ bool SetCacheLogConfig = false;
+ ExtendableStringBuilder<256> StringBuilder;
+ ZenCacheStore::Configuration::LogConfig LoggingConfig = m_CacheStore.GetConfiguration().Logging;
+ if (std::string Param(Params.GetValue("cacheenablewritelog")); Param.empty() == false)
+ {
+ LoggingConfig.EnableWriteLog = StrCaseCompare(Param.c_str(), "true") == 0;
+ SetCacheLogConfig = true;
+ }
+ if (std::string Param(Params.GetValue("cacheenableaccesslog")); Param.empty() == false)
+ {
+ LoggingConfig.EnableAccessLog = StrCaseCompare(Param.c_str(), "true") == 0;
+ SetCacheLogConfig = true;
+ }
+ if (SetCacheLogConfig)
+ {
+ m_CacheStore.SetLoggingConfig(LoggingConfig);
+ StringBuilder.Append(fmt::format("cache write log: {}, cache access log: {}",
+ LoggingConfig.EnableWriteLog ? "true" : "false",
+ LoggingConfig.EnableAccessLog ? "true" : "false"));
+ }
+ 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);
+ if (LogLevel != Param)
+ {
+ return Req.ServerRequest().WriteResponse(HttpResponseCode::BadRequest,
+ HttpContentType::kText,
+ fmt::format("Invalid log level '{}'", Param));
+ }
+ spdlog::set_level(NewLevel);
+ if (StringBuilder.Size() > 0)
+ {
+ StringBuilder.Append(", ");
+ }
+ StringBuilder.Append("loglevel: ");
+ StringBuilder.Append(Param);
+ }
+ return Req.ServerRequest().WriteResponse(HttpResponseCode::OK, HttpContentType::kText, StringBuilder.ToView());
+ },
+ HttpVerb::kPost);
}
HttpAdminService::~HttpAdminService()
diff --git a/src/zenserver/admin/admin.h b/src/zenserver/admin/admin.h
index 3152f87ab..12a47f29e 100644
--- a/src/zenserver/admin/admin.h
+++ b/src/zenserver/admin/admin.h
@@ -9,11 +9,18 @@ namespace zen {
class GcScheduler;
class JobQueue;
+class ZenCacheStore;
class HttpAdminService : public zen::HttpService
{
public:
- HttpAdminService(GcScheduler& Scheduler, JobQueue& BackgroundJobQueue);
+ struct LogPaths
+ {
+ std::filesystem::path AbsLogPath;
+ std::filesystem::path HttpLogPath;
+ std::filesystem::path CacheLogPath;
+ };
+ HttpAdminService(GcScheduler& Scheduler, JobQueue& BackgroundJobQueue, ZenCacheStore& CacheStore, const LogPaths& LogPaths);
~HttpAdminService();
virtual const char* BaseUri() const override;
@@ -23,6 +30,8 @@ private:
HttpRequestRouter m_Router;
GcScheduler& m_GcScheduler;
JobQueue& m_BackgroundJobQueue;
+ ZenCacheStore& m_CacheStore;
+ LogPaths m_LogPaths;
};
} // namespace zen
diff --git a/src/zenserver/cache/httpstructuredcache.cpp b/src/zenserver/cache/httpstructuredcache.cpp
index 32e8c1f98..11ac81dcb 100644
--- a/src/zenserver/cache/httpstructuredcache.cpp
+++ b/src/zenserver/cache/httpstructuredcache.cpp
@@ -717,8 +717,12 @@ HttpStructuredCacheService::HandleCacheRequest(HttpServerRequest& Request)
BasePathString << Info.Config.BasePath.u8string();
ResponseWriter.AddString("BasePath"sv, BasePathString.ToView());
ResponseWriter.AddBool("AllowAutomaticCreationOfNamespaces", Info.Config.AllowAutomaticCreationOfNamespaces);
- ResponseWriter.AddBool("EnableWriteLog", Info.Config.EnableWriteLog);
- ResponseWriter.AddBool("EnableAccessLog", Info.Config.EnableAccessLog);
+ ResponseWriter.BeginObject("Logging");
+ {
+ ResponseWriter.AddBool("EnableWriteLog", Info.Config.Logging.EnableWriteLog);
+ ResponseWriter.AddBool("EnableAccessLog", Info.Config.Logging.EnableAccessLog);
+ }
+ ResponseWriter.EndObject();
}
ResponseWriter.EndObject();
diff --git a/src/zenserver/cache/structuredcachestore.cpp b/src/zenserver/cache/structuredcachestore.cpp
index 4499b05f7..809df1a94 100644
--- a/src/zenserver/cache/structuredcachestore.cpp
+++ b/src/zenserver/cache/structuredcachestore.cpp
@@ -238,10 +238,7 @@ ZenCacheStore::ZenCacheStore(GcManager& Gc, const Configuration& Configuration,
, m_Configuration(Configuration)
, m_ExitLogging(false)
{
- if (m_Configuration.EnableAccessLog || m_Configuration.EnableWriteLog)
- {
- m_AsyncLoggingThread = std::thread(&ZenCacheStore::LogWorker, this);
- }
+ SetLoggingConfig(m_Configuration.Logging);
CreateDirectories(m_Configuration.BasePath);
ZEN_INFO("Initializing at '{}'", m_Configuration.BasePath);
@@ -281,12 +278,7 @@ ZenCacheStore::ZenCacheStore(GcManager& Gc, const Configuration& Configuration,
ZenCacheStore::~ZenCacheStore()
{
- m_ExitLogging.store(true);
- m_LogEvent.Set();
- if (m_AsyncLoggingThread.joinable())
- {
- m_AsyncLoggingThread.join();
- }
+ SetLoggingConfig({.EnableWriteLog = false, .EnableAccessLog = false});
m_Namespaces.clear();
}
@@ -381,7 +373,7 @@ ZenCacheStore::Get(const CacheRequestContext& Context,
{
bool Result = Store->Get(Bucket, HashKey, OutValue);
- if (m_Configuration.EnableAccessLog)
+ if (m_AccessLogEnabled)
{
ZEN_TRACE_CPU("Z$::Get::AccessLog");
bool Signal = false;
@@ -421,7 +413,7 @@ ZenCacheStore::Put(const CacheRequestContext& Context,
{
ZEN_TRACE_CPU("Z$::Put");
- if (m_Configuration.EnableWriteLog)
+ if (m_WriteLogEnabled)
{
ZEN_TRACE_CPU("Z$::Get::WriteLog");
bool Signal = false;
@@ -616,6 +608,31 @@ ZenCacheStore::StorageSize() const
return Size;
}
+void
+ZenCacheStore::SetLoggingConfig(const Configuration::LogConfig& Loggingconfig)
+{
+ if (!Loggingconfig.EnableAccessLog && !Loggingconfig.EnableWriteLog)
+ {
+ m_AccessLogEnabled.store(false);
+ m_WriteLogEnabled.store(false);
+ m_ExitLogging.store(true);
+ m_LogEvent.Set();
+ if (m_AsyncLoggingThread.joinable())
+ {
+ m_AsyncLoggingThread.join();
+ }
+ m_Configuration.Logging = Loggingconfig;
+ return;
+ }
+ if (!m_AccessLogEnabled.load() && !m_WriteLogEnabled.load())
+ {
+ m_AsyncLoggingThread = std::thread(&ZenCacheStore::LogWorker, this);
+ }
+ m_WriteLogEnabled.store(Loggingconfig.EnableWriteLog);
+ m_AccessLogEnabled.store(Loggingconfig.EnableAccessLog);
+ m_Configuration.Logging = Loggingconfig;
+}
+
ZenCacheStore::Info
ZenCacheStore::GetInfo() const
{
diff --git a/src/zenserver/cache/structuredcachestore.h b/src/zenserver/cache/structuredcachestore.h
index 239efe68f..e7b64babe 100644
--- a/src/zenserver/cache/structuredcachestore.h
+++ b/src/zenserver/cache/structuredcachestore.h
@@ -122,8 +122,11 @@ public:
{
std::filesystem::path BasePath;
bool AllowAutomaticCreationOfNamespaces = false;
- bool EnableWriteLog = true;
- bool EnableAccessLog = true;
+ struct LogConfig
+ {
+ bool EnableWriteLog = true;
+ bool EnableAccessLog = true;
+ } Logging;
};
struct Info
@@ -159,6 +162,8 @@ public:
GcStorageSize StorageSize() const;
+ Configuration GetConfiguration() const { return m_Configuration; }
+ void SetLoggingConfig(const Configuration::LogConfig& Loggingconfig);
Info GetInfo() const;
std::optional<ZenCacheNamespace::Info> GetNamespaceInfo(std::string_view Namespace);
std::optional<ZenCacheNamespace::BucketInfo> GetBucketInfo(std::string_view Namespace, std::string_view Bucket);
@@ -201,6 +206,8 @@ private:
std::atomic_bool m_ExitLogging;
Event m_LogEvent;
std::thread m_AsyncLoggingThread;
+ std::atomic_bool m_WriteLogEnabled;
+ std::atomic_bool m_AccessLogEnabled;
};
void z$_forcelink();
diff --git a/src/zenserver/zenserver.cpp b/src/zenserver/zenserver.cpp
index 9c607f1d3..c36e20b30 100644
--- a/src/zenserver/zenserver.cpp
+++ b/src/zenserver/zenserver.cpp
@@ -433,7 +433,13 @@ public:
m_GcScheduler.Initialize(GcConfig);
// Create and register admin interface last to make sure all is properly initialized
- m_AdminService = std::make_unique<HttpAdminService>(m_GcScheduler, *m_JobQueue);
+ m_AdminService =
+ std::make_unique<HttpAdminService>(m_GcScheduler,
+ *m_JobQueue,
+ *m_CacheStore,
+ HttpAdminService::LogPaths{.AbsLogPath = ServerOptions.AbsLogFile,
+ .HttpLogPath = ServerOptions.DataDir / "logs" / "http.log",
+ .CacheLogPath = ServerOptions.DataDir / "logs" / "z$.log"});
m_Http->RegisterService(*m_AdminService);
return EffectiveBasePort;
@@ -905,12 +911,13 @@ ZenServer::InitializeStructuredCache(const ZenServerOptions& ServerOptions)
using namespace std::literals;
ZEN_INFO("instantiating structured cache service");
- m_CacheStore = new ZenCacheStore(m_GcManager,
- ZenCacheStore::Configuration{.BasePath = m_DataRoot / "cache",
- .AllowAutomaticCreationOfNamespaces = true,
- .EnableWriteLog = ServerOptions.StructuredCacheWriteLogEnabled,
- .EnableAccessLog = ServerOptions.StructuredCacheAccessLogEnabled},
- m_GcManager.GetDiskWriteBlocker());
+ m_CacheStore =
+ new ZenCacheStore(m_GcManager,
+ ZenCacheStore::Configuration{.BasePath = m_DataRoot / "cache",
+ .AllowAutomaticCreationOfNamespaces = true,
+ .Logging = {.EnableWriteLog = ServerOptions.StructuredCacheWriteLogEnabled,
+ .EnableAccessLog = ServerOptions.StructuredCacheAccessLogEnabled}},
+ m_GcManager.GetDiskWriteBlocker());
const ZenUpstreamCacheConfig& UpstreamConfig = ServerOptions.UpstreamCacheConfig;
@@ -1136,7 +1143,7 @@ ZenEntryPoint::Run()
sentry_options_set_dsn(SentryOptions, "https://[email protected]/5919284");
sentry_options_set_database_path(SentryOptions, SentryDatabasePath.c_str());
sentry_options_set_logger(SentryOptions, SentryLogFunction, this);
- std::string SentryAttachmentPath = m_ServerOptions.AbsLogFile.string();
+ std::string SentryAttachmentPath = PathToUtf8(m_ServerOptions.AbsLogFile);
if (SentryAttachmentPath.starts_with("\\\\?\\"))
{
SentryAttachmentPath = SentryAttachmentPath.substr(4);