From 889d521e05f2e922683d48c74eb00290e7a2f548 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Wed, 17 Jul 2024 00:06:50 +0100 Subject: Shuffle the modules about a bit. --- modules/extra/log_syslog.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 modules/extra/log_syslog.cpp (limited to 'modules/extra/log_syslog.cpp') diff --git a/modules/extra/log_syslog.cpp b/modules/extra/log_syslog.cpp new file mode 100644 index 000000000..9d7a1c749 --- /dev/null +++ b/modules/extra/log_syslog.cpp @@ -0,0 +1,93 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2022-2023 Sadie Powell + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "inspircd.h" + +#include + +class SyslogMethod final + : public Log::Method +{ +private: + // Converts an InspIRCd log level to syslog priority. + static int LevelToPriority(Log::Level level) + { + switch (level) + { + case Log::Level::CRITICAL: + return LOG_ERR; + + case Log::Level::WARNING: + return LOG_WARNING; + + case Log::Level::NORMAL: + return LOG_NOTICE; + + case Log::Level::DEBUG: + case Log::Level::RAWIO: + return LOG_DEBUG; + } + + // Should never happen. + return LOG_NOTICE; + } + +public: + void OnLog(time_t time, Log::Level level, const std::string& type, const std::string& message) override + { + syslog(LevelToPriority(level), "%s: %s", type.c_str(), message.c_str()); + } +}; + +class SyslogEngine final + : public Log::Engine +{ +public: + SyslogEngine(Module* Creator) ATTR_NOT_NULL(2) + : Log::Engine(Creator, "syslog") + { + } + + Log::MethodPtr Create(const std::shared_ptr& tag) override + { + return std::make_shared(); + } +}; + +class ModuleLogSyslog final + : public Module +{ +private: + SyslogEngine engine; + +public: + ModuleLogSyslog() + : Module(VF_VENDOR, "Provides the ability to log to syslog.") + , engine(this) + { + openlog("inspircd", LOG_NDELAY|LOG_PID, LOG_USER); + } + + ~ModuleLogSyslog() override + { + closelog(); + } +}; + +MODULE_INIT(ModuleLogSyslog) -- cgit v1.3.1-10-gc9f91