aboutsummaryrefslogtreecommitdiff
path: root/src/modules/m_log_sql.cpp
blob: d77b235cda14e478a4fdd8168f446bd5d254aa24 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * 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 "modules/sql.h"

namespace
{
	Module* thismod;
}

class SQLQuery final
	: public SQL::Query
{
public:
	SQLQuery(Module* mod)
		: SQL::Query(mod)
	{
	}

	void OnResult(SQL::Result& res) override
	{
		// Nothing to do here.
	}

	void OnError(SQL::Error& error) override
	{
		ServerInstance->SNO.WriteGlobalSno('a', "Unable to write to SQL log (query error: %s).", error.ToString());
	}
};

class SQLMethod final
	: public Log::Method
{
private:
	std::string query;
	dynamic_reference<SQL::Provider> sql;

public:
	SQLMethod(const dynamic_reference<SQL::Provider>& s, const std::string& q)
		: query(q)
		, sql(s)
	{
	}

	void OnLog(Log::Level level, const std::string& type, const std::string& message) override
	{
		if (!sql)
		{
			ServerInstance->SNO.WriteGlobalSno('a', "Unable to write to SQL log (database %s not available).", sql->GetId().c_str());
			return;
		}

		SQL::ParamMap params = {
			{ "level",    ConvToStr(static_cast<uint8_t>(level)) },
			{ "levelstr", Log::LevelToString(level)              },
			{ "message",  message                                },
			{ "time",     ConvToStr(ServerInstance->Time())      },
			{ "type",     type                                   },
		};
		sql->Submit(new SQLQuery(thismod), query, params);
	}
};

class SQLEngine final
	: public Log::Engine
{
public:
	SQLEngine(Module* Creator)
		: Log::Engine(Creator, "sql")
	{
	}

public:
	Log::MethodPtr Create(std::shared_ptr<ConfigTag> tag) override
	{
		dynamic_reference<SQL::Provider> sql(creator, "SQL");
		const std::string dbid = tag->getString("dbid");
		if (!dbid.empty())
			sql.SetProvider("SQL/" + dbid);

		const std::string query = tag->getString("query", "INSERT INTO ircd_log (time, type, message) VALUES (FROM_UNIXTIME($time), '$type', '$message');", 1);
		return std::make_shared<SQLMethod>(sql, query);
	}
};

class ModuleLogSQL final
	: public Module
{
private:
	SQLEngine engine;

public:
	ModuleLogSQL()
		: Module(VF_VENDOR, "Provides the ability to write logs to an SQL database.")
		, engine(this)
	{
		thismod = this;
	}

};

MODULE_INIT(ModuleLogSQL)