aboutsummaryrefslogtreecommitdiff
path: root/include/logging.h
blob: 79365f097bd1ca07c1e51b2bbd3ce5a0212cac65 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2022-2024 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/>.
 */


#pragma once

namespace Log
{
	class Method;
	class FileMethod;

	class Engine;
	class FileEngine;
	class StreamEngine;

	class Manager;

	/** A shared pointer to a log method. */
	typedef std::shared_ptr<Method> MethodPtr;

	/** Levels at which messages can be logged. */
	enum class Level
		: uint8_t
	{
		/** A critical message which must be investigated. */
		CRITICAL = 0,

		/** An important message which should be investigated. */
		WARNING = 1,

		/** A general message which is useful to have on record. */
		NORMAL = 2,

		/** A debug message that we might want to store when testing. */
		DEBUG = 3,

		/** A sensitive message that we should not store lightly. */
		RAWIO = 4,
	};

	/** Converts a log level to a string.
	 * @param level The log level to convert.
	 */
	CoreExport const char* LevelToString(Level level);

	/** Notify a user that raw I/O logging is enabled.
	 * @param user The user to notify.
	 * @param type The type of message to send.
	 */
	CoreExport void NotifyRawIO(LocalUser* user, MessageType type);
}

/** Base class for logging methods. */
class CoreExport Log::Method
{
protected:
	Method() = default;

public:
	virtual ~Method() = default;

	/** Determines whether this logging method accepts cached messages. */
	virtual bool AcceptsCachedMessages() const { return true; }

	/** Writes a message to the logger.
	 * @param time The time at which the message was logged.
	 * @param level The level at which the log message was written.
	 * @param type The component which wrote the log message.
	 * @param message The message which was written to the log.
	 */
	virtual void OnLog(time_t time, Level level, const std::string& type, const std::string& message) = 0;
};

/** A logger that writes to a file stream. */
class CoreExport Log::FileMethod final
	: public Method
	, public Timer
{
private:
	/** Whether to autoclose the file on exit. */
	bool autoclose;

	/** The file to which the log is written. */
	FILE* file;

	/** How often the file stream should be flushed. */
	const unsigned long flush;

	/** The number of lines which have been written since the file stream was created. */
	unsigned long lines = 0;

	/** The name the underlying file. */
	const std::string name;

public:
	FileMethod(const std::string& n, FILE* fh, unsigned long fl, bool ac);
	~FileMethod() override;

	/** @copydoc Log::Method::AcceptsCachedMessages */
	bool AcceptsCachedMessages() const override { return false; }

	/** @copydoc Timer::Tick */
	bool Tick() override;

	/** @copydoc Log::Method::OnLog */
	void OnLog(time_t time, Level level, const std::string& type, const std::string& message) override;
};

/** Base class for logging engines. */
class CoreExport Log::Engine
	: public DataProvider
{
protected:
	Engine(Module* Creator, const std::string& Name);

public:
	virtual ~Engine() override;

	/** Creates a new logger from the specified config.
	 * @param tag The config tag to configure the logger with.
	 */
	virtual MethodPtr Create(const std::shared_ptr<ConfigTag>& tag) = 0;

};

/** A logger which writes to a file. */
class CoreExport Log::FileEngine final
	: public Engine
{
public:
	FileEngine(Module* Creator);

	/** @copydoc Log::Engine::Create */
	MethodPtr Create(const std::shared_ptr<ConfigTag>& tag) override;
};

/** A logger which writes to a stream. */
class CoreExport Log::StreamEngine final
	: public Engine
{
private:
	FILE* file;

public:
	StreamEngine(Module* Creator, const std::string& Name, FILE* fh);

	/** @copydoc Log::Engine::Create */
	MethodPtr Create(const std::shared_ptr<ConfigTag>& tag) override;
};

/** Manager for the logging system. */
class CoreExport Log::Manager final
{
private:
	/** A log message which has been cached until modules load. */
	struct CachedMessage final
	{
		/** The time the message was logged at. */
		time_t time;

		/** The level the message was logged at. */
		Level level;

		/** The type of the message that was logged. */
		std::string type;

		/** The message that was logged. */
		std::string message;

		CachedMessage(time_t ts, Level l, const std::string& t, const std::string& m);
	};

	/** Encapsulates information about a logger. */
	struct Info final
	{
		/** Whether the logger was read from the server config. */
		bool config;

		/** Whether this logger is dead and is awaiting removal. */
		bool dead = false;

		/** The minimum log level that this logger accepts. */
		Level level;

		/** The types of log message that this logger accepts. */
		TokenList types;

		/** The handler for this logger type. */
		MethodPtr method;

		/** The engine which created this logger. */
		const Engine* engine;

		Info(Level l, TokenList t, MethodPtr m, bool c, const Engine* e) ATTR_NOT_NULL(6);

		/** Determines whether a message of the specified level and type should be written to this logger.
		 * @param l The level to check
		 * @param t The type to check.
		 */
		bool Suitable(Level l, const std::string& t) const;
	};

	/** The log messages we have cached for modules. */
	std::vector<CachedMessage> cache;

	/** Whether we have just started up and need to cache messages until modules are loaded. */
	bool caching = true;

	/** The logger engine that writes to a file. */
	Log::FileEngine filelog;

	/** A logger that writes to stderr. */
	Log::StreamEngine stderrlog;

	/** A logger that writes to stdout. */
	Log::StreamEngine stdoutlog;

	/** The currently registered loggers. */
	std::vector<Info> loggers;

	/** Whether we are currently logging to a file. */
	bool logging = false;

	/** Check whether raw logging is enabled. */
	void CheckRawLog();

	/** Writes a message to the server log.
	 * @param level The level to log at.
	 * @param type The type of message that is being logged.
	 * @param message The message to log.
	 */
	void Write(Level level, const std::string& type, const std::string& message);

public:
	Manager();

	/** Closes all loggers which were opened from the config. */
	void CloseLogs();

	/** Enables writing rawio logs to the standard output stream. */
	void EnableDebugMode();

	/** Opens loggers that are specified in the config. */
	void OpenLogs(bool requiremethods);

	/** Registers the core logging services with the event system. */
	void RegisterServices();

	/** Unloads all loggers that are provided by the specified engine.
	 * @param engine The engine to unload the loggers of.
	 */
	void UnloadEngine(const Engine* engine) ATTR_NOT_NULL(2);

	/** Writes an critical message to the server log.
	 * @param type The type of message that is being logged.
	 * @param message The message to log.
	 */
	inline void Critical(const std::string& type, const std::string& message)
	{
		Write(Level::CRITICAL, type, message);
	}

	/** Writes an critical message to the server log.
	 * @param type The type of message that is being logged.
	 * @param format A format string to format and then log.
	 * @param args One or more arguments to format the string with.
	 */
	template <typename... Args>
	void Critical(const std::string& type, const char* format, Args&&... args)
	{
		Write(Level::CRITICAL, type, fmt::vformat(format, fmt::make_format_args(args...)));
	}

	/** Writes a warning message to the server log.
	 * @param type The type of message that is being logged.
	 * @param message The message to log.
	 */
	inline void Warning(const std::string& type, const std::string& message)
	{
		Write(Level::WARNING, type, message);
	}

	/** Writes a warning message to the server log.
	 * @param type The type of message that is being logged.
	 * @param format A format string to format and then log.
	 * @param args One or more arguments to format the string with.
	 */
	template <typename... Args>
	void Warning(const std::string& type, const char* format, Args&&... args)
	{
		Write(Level::WARNING, type, fmt::vformat(format, fmt::make_format_args(args...)));
	}

	/** Writes a normal message to the server log.
	 * @param type The type of message that is being logged.
	 * @param message The message to log.
	 */
	inline void Normal(const std::string& type, const std::string& message)
	{
		Write(Level::NORMAL, type, message);
	}

	/** Writes a normal message to the server log.
	 * @param type The type of message that is being logged.
	 * @param format A format string to format and then log.
	 * @param args One or more arguments to format the string with.
	 */
	template <typename... Args>
	void Normal(const std::string& type, const char* format, Args&&... args)
	{
		Write(Level::NORMAL, type, fmt::vformat(format, fmt::make_format_args(args...)));
	}

	/** Writes a debug message to the server log.
	 * @param type The type of message that is being logged.
	 * @param message The message to log.
	 */
	inline void Debug(const std::string& type, const std::string& message)
	{
		Write(Level::DEBUG, type, message);
	}
	/** Writes a debug message to the server log.
	 * @param type The type of message that is being logged.
	 * @param format A format string to format and then log.
	 * @param args One or more arguments to format the string with.
	 */
	template <typename... Args>
	void Debug(const std::string& type, const char* format, Args&&... args)
	{
		Write(Level::DEBUG, type, fmt::vformat(format, fmt::make_format_args(args...)));
	}

	/** Writes a raw I/O message to the server log.
	 * @param type The type of message that is being logged.
	 * @param message The message to log.
	 */
	inline void RawIO(const std::string& type, const std::string& message)
	{
		Write(Level::RAWIO, type, message);
	}

	/** Writes a raw I/O message to the server log.
	 * @param type The type of message that is being logged.
	 * @param format A format string to format and then log.
	 * @param args One or more arguments to format the string with.
	 */
	template <typename... Args>
	void RawIO(const std::string& type, const char* format, Args&&... args)
	{
		Write(Level::RAWIO, type, fmt::vformat(format, fmt::make_format_args(args...)));
	}
};