aboutsummaryrefslogtreecommitdiff
path: root/src/modules/extra/m_log_syslog.cpp
blob: 5feba19579eafeb34304d0a428d1c166a8ff45fc (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2022 Sadie Powell <sadie@witchery.services>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */


#include "inspircd.h"

#include <syslog.h>

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::ERROR:
				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<ConfigTag>& tag) override
	{
		return std::make_shared<SyslogMethod>();
	}
};

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)