diff options
Diffstat (limited to 'src/zen')
| -rw-r--r-- | src/zen/cmds/admin_cmd.cpp | 102 | ||||
| -rw-r--r-- | src/zen/cmds/admin_cmd.h | 19 | ||||
| -rw-r--r-- | src/zen/zen.cpp | 2 |
3 files changed, 123 insertions, 0 deletions
diff --git a/src/zen/cmds/admin_cmd.cpp b/src/zen/cmds/admin_cmd.cpp index 0aef968a9..b48207bec 100644 --- a/src/zen/cmds/admin_cmd.cpp +++ b/src/zen/cmds/admin_cmd.cpp @@ -267,4 +267,106 @@ JobCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) return 0; } +//////////////////////////////////////////// + +LoggingCommand::LoggingCommand() +{ + m_Options.add_options()("h,help", "Print help"); + m_Options.add_option("", "u", "hosturl", "Host URL", cxxopts::value(m_HostName)->default_value(""), "<hosturl>"); + m_Options.add_option("", "", "cache-write-log", "Enable cache write logging", cxxopts::value(m_CacheWriteLog), "<enable/disable>"); + m_Options.add_option("", "", "cache-access-log", "Enable cache access logging", cxxopts::value(m_CacheAccessLog), "<enable/disable>"); + m_Options + .add_option("", "", "set-log-level", "Set zenserver log level", cxxopts::value(m_SetLogLevel), "<trace/debug/info/warning/error>"); +} + +LoggingCommand::~LoggingCommand() = default; + +int +LoggingCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) +{ + ZEN_UNUSED(GlobalOptions); + + using namespace std::literals; + + if (!ParseOptions(argc, argv)) + { + return 0; + } + + m_HostName = ResolveTargetHostSpec(m_HostName); + + if (m_HostName.empty()) + { + throw OptionParseException("unable to resolve server specification"); + } + + HttpClient Http(m_HostName); + + HttpClient::KeyValueMap Parameters; + + if (!m_CacheWriteLog.empty()) + { + if (m_CacheWriteLog == "enable") + { + (*Parameters)["cacheenablewritelog"] = "true"; + } + else if (m_CacheWriteLog == "disable") + { + (*Parameters)["cacheenablewritelog"] = "false"; + } + else + { + ZEN_ERROR("Invalid value for parameter 'cache-write-log'. Use 'enable' or 'disable'"); + return 1; + } + } + + if (!m_CacheAccessLog.empty()) + { + if (m_CacheAccessLog == "enable") + { + (*Parameters)["cacheenableaccesslog"] = "true"; + } + else if (m_CacheAccessLog == "disable") + { + (*Parameters)["cacheenableaccesslog"] = "false"; + } + else + { + ZEN_ERROR("Invalid value for parameter 'cache-access-log'. Use 'enable' or 'disable'"); + return 1; + } + } + + if (!m_SetLogLevel.empty()) + { + (*Parameters)["loglevel"] = m_SetLogLevel; + } + + if ((*Parameters).empty()) + { + if (HttpClient::Response Result = Http.Get("/admin/logs", HttpClient::Accept(ZenContentType::kJSON))) + { + ZEN_CONSOLE("{}", Result.AsText()); + } + else + { + Result.ThrowError("failed fetching log info"sv); + return 1; + } + return 0; + } + if (HttpClient::Response Result = Http.Post("/admin/logs", HttpClient::KeyValueMap{}, Parameters)) + { + ZEN_CONSOLE("{}", Result.AsText()); + } + else + { + Result.ThrowError("failed setting log info"sv); + return 1; + } + + return 0; +} + } // namespace zen diff --git a/src/zen/cmds/admin_cmd.h b/src/zen/cmds/admin_cmd.h index 6caab7138..873c230d9 100644 --- a/src/zen/cmds/admin_cmd.h +++ b/src/zen/cmds/admin_cmd.h @@ -73,4 +73,23 @@ private: bool m_Cancel = 0; }; +//////////////////////////////////////////// + +class LoggingCommand : public ZenCmdBase +{ +public: + LoggingCommand(); + ~LoggingCommand(); + + virtual int Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) override; + virtual cxxopts::Options& Options() override { return m_Options; } + +private: + cxxopts::Options m_Options{"logs", "Show/control zen logging"}; + std::string m_HostName; + std::string m_CacheWriteLog; + std::string m_CacheAccessLog; + std::string m_SetLogLevel; +}; + } // namespace zen diff --git a/src/zen/zen.cpp b/src/zen/zen.cpp index 23dce850f..9c664e0dc 100644 --- a/src/zen/zen.cpp +++ b/src/zen/zen.cpp @@ -226,6 +226,7 @@ main(int argc, char** argv) ServeCommand ServeCmd; SnapshotOplogCommand SnapshotOplogCmd; StatusCommand StatusCmd; + LoggingCommand LoggingCmd; JobCommand JobCmd; TopCommand TopCmd; TraceCommand TraceCmd; @@ -277,6 +278,7 @@ main(int argc, char** argv) {"scrub", &ScrubCmd, "Scrub zen storage (verify data integrity)"}, {"serve", &ServeCmd, "Serve files from a directory"}, {"status", &StatusCmd, "Show zen status"}, + {"logs", &LoggingCmd, "Show/control zen logging"}, {"jobs", &JobCmd, "Show/cancel zen background jobs"}, {"top", &TopCmd, "Monitor zen server activity"}, {"trace", &TraceCmd, "Control zen realtime tracing"}, |