From cf47e0f3f81870f94a10a09f5f338ba7fd4bcdf4 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Wed, 24 Aug 2022 00:13:14 +0100 Subject: [PATCH] Add support for logging JSON to stdout and stderr. --- docs/conf/modules.conf.example | 10 +++++++- src/modules/extra/m_log_json.cpp | 44 ++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/docs/conf/modules.conf.example b/docs/conf/modules.conf.example index f7e5d8120..cca4c9a7d 100644 --- a/docs/conf/modules.conf.example +++ b/docs/conf/modules.conf.example @@ -1367,7 +1367,7 @@ # not loaded the oper accounts are still protected by a password. # #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# -# JSON logging module: Allows writing messages to the system log. # +# JSON logging module: Allows writing messages to a JSON file. # # This module is in extras. Re-run configure with: # # ./configure --enable-extras log_json # @@ -1376,6 +1376,14 @@ # target="inspircd.json" # level="normal" # type="* -USERINPUT -USEROUTPUT"> +# +# +# +# #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# # SQL logging module: Allows writing messages to an SQL database.. # diff --git a/src/modules/extra/m_log_json.cpp b/src/modules/extra/m_log_json.cpp index afa6d58a3..93a74c181 100644 --- a/src/modules/extra/m_log_json.cpp +++ b/src/modules/extra/m_log_json.cpp @@ -29,6 +29,9 @@ class JSONMethod final , public Timer { private: + // Whether to autoclose the file on exit. + bool autoclose; + // The file to which the log is written. FILE* file; @@ -45,8 +48,9 @@ public: // RapidJSON API: The type of character that this writer accepts. typedef char Ch; - JSONMethod(const std::string& n, FILE* fh, unsigned long fl) ATTR_NOT_NULL(3) + JSONMethod(const std::string& n, FILE* fh, unsigned long fl, bool ac) ATTR_NOT_NULL(3) : Timer(15*60, true) + , autoclose(ac) , file(fh) , flush(fl) , name(n) @@ -57,7 +61,8 @@ public: ~JSONMethod() override { - fclose(file); + if (autoclose) + fclose(file); } // RapidJSON API: We implement our own flushing in OnLog. @@ -117,11 +122,11 @@ public: } }; -class JSONEngine final +class JSONFileEngine final : public Log::Engine { public: - JSONEngine(Module* Creator) ATTR_NOT_NULL(2) + JSONFileEngine(Module* Creator) ATTR_NOT_NULL(2) : Log::Engine(Creator, "json") { } @@ -141,7 +146,26 @@ public: } const unsigned long flush = tag->getUInt("flush", 20, 1); - return std::make_shared(fulltarget, fh, flush); + return std::make_shared(fulltarget, fh, flush, true); + } +}; + +class JSONStreamEngine final + : public Log::Engine +{ +private: + FILE* file; + +public: + JSONStreamEngine(Module* Creator, const std::string& Name, FILE* fh) ATTR_NOT_NULL(2, 4) + : Log::Engine(Creator, Name) + , file(fh) + { + } + + Log::MethodPtr Create(std::shared_ptr tag) override + { + return std::make_shared(name, file, 1, false); } }; @@ -149,12 +173,16 @@ class ModuleLogJSON final : public Module { private: - JSONEngine engine; + JSONFileEngine log; + JSONStreamEngine stderrlog; + JSONStreamEngine stdoutlog; public: ModuleLogJSON() - : Module(VF_VENDOR, "Provides the ability to write logs to syslog.") - , engine(this) + : Module(VF_VENDOR, "Provides the ability to write logs to a JSON file.") + , log(this) + , stderrlog(this, "json-stderr", stderr) + , stdoutlog(this, "json-stdout", stdout) { } };