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
|
/*
* 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/>.
*/
#include "inspircd.h"
#include "clientprotocolmsg.h"
#include "timeutils.h"
#include <fmt/color.h>
const char* Log::LevelToString(Log::Level level)
{
switch (level)
{
case Log::Level::CRITICAL:
return "critical";
case Log::Level::WARNING:
return "warning";
case Log::Level::NORMAL:
return "normal";
case Log::Level::DEBUG:
return "debug";
case Log::Level::RAWIO:
return "rawio";
}
// Should never happen.
return "unknown";
}
void Log::NotifyRawIO(LocalUser* user, MessageType type)
{
ClientProtocol::Messages::Privmsg msg(ServerInstance->FakeClient, user, "*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.", type);
user->Send(ServerInstance->GetRFCEvents().privmsg, msg);
}
class DebugLogMethod final
: public Log::Method
{
public:
void OnLog(time_t time, Log::Level level, const std::string& type, const std::string& message) override
{
fmt::println("{} {}: {}",
fmt::styled(Time::ToString(time, "%d %b %H:%M:%S"), fmt::fg(fmt::terminal_color::yellow)),
fmt::styled(type, fmt::fg(fmt::terminal_color::green)),
message
);
}
};
Log::FileMethod::FileMethod(const std::string& n, FILE* fh, unsigned long fl, bool ac)
: Timer(15*60, true)
, autoclose(ac)
, file(fh)
, flush(fl)
, name(n)
{
if (flush > 1)
ServerInstance->Timers.AddTimer(this);
}
Log::FileMethod::~FileMethod()
{
if (autoclose)
fclose(file);
}
void Log::FileMethod::OnLog(time_t time, Level level, const std::string& type, const std::string& message)
{
static time_t prevtime = 0;
static std::string timestr;
if (prevtime != time)
{
prevtime = time;
timestr = Time::ToString(prevtime);
}
fputs(timestr.c_str(), file);
fputs(" ", file);
fputs(type.c_str(), file);
fputs(": ", file);
fputs(message.c_str(), file);
#if defined _WIN32
fputs("\r\n", file);
#else
fputs("\n", file);
#endif
if (!(++lines % flush))
fflush(file);
if (ferror(file))
throw CoreException(INSP_FORMAT("Unable to write to {}: {}", name, strerror(errno)));
}
bool Log::FileMethod::Tick()
{
fflush(file);
return true;
}
Log::Engine::Engine(Module* Creator, const std::string& Name)
: DataProvider(Creator, "log/" + Name)
{
}
Log::Engine::~Engine()
{
if (creator)
ServerInstance->Logs.UnloadEngine(this);
}
Log::FileEngine::FileEngine(Module* Creator)
: Engine(Creator, "file")
{
}
Log::MethodPtr Log::FileEngine::Create(const std::shared_ptr<ConfigTag>& tag)
{
const std::string target = tag->getString("target");
if (target.empty())
throw CoreException("<log:target> must be specified for file logger at " + tag->source.str());
const std::string fulltarget = ServerInstance->Config->Paths.PrependLog(Time::ToString(ServerInstance->Time(), target.c_str()));
auto* fh = fopen(fulltarget.c_str(), "a");
if (!fh)
{
throw CoreException(INSP_FORMAT("Unable to open {} for file logger at {}: {}", fulltarget,
tag->source.str(), strerror(errno)));
}
const unsigned long flush = tag->getNum<unsigned long>("flush", 20, 1);
return std::make_shared<FileMethod>(fulltarget, fh, flush, true);
}
Log::StreamEngine::StreamEngine(Module* Creator, const std::string& Name, FILE* fh)
: Engine(Creator, Name)
, file(fh)
{
}
Log::MethodPtr Log::StreamEngine::Create(const std::shared_ptr<ConfigTag>& tag)
{
return std::make_shared<FileMethod>(name, file, 1, false);
}
Log::Manager::CachedMessage::CachedMessage(time_t ts, Level l, const std::string& t, const std::string& m)
: time(ts)
, level(l)
, type(t)
, message(m)
{
}
Log::Manager::Info::Info(Level l, TokenList t, MethodPtr m, bool c, const Engine* e)
: config(c)
, level(l)
, types(std::move(t))
, method(std::move(m))
, engine(e)
{
}
bool Log::Manager::Info::Suitable(Level l, const std::string& t) const
{
return level >= l && types.Contains(t) && !dead;
}
Log::Manager::Manager()
: filelog(nullptr)
, stderrlog(nullptr, "stderr", stderr)
, stdoutlog(nullptr, "stdout", stdout)
{
}
void Log::Manager::CloseLogs()
{
logging = true; // Prevent writing to dying loggers.
loggers.erase(std::remove_if(loggers.begin(), loggers.end(), [](const Info& info) { return info.config; }), loggers.end());
logging = false;
}
void Log::Manager::EnableDebugMode()
{
TokenList types = std::string("*");
MethodPtr method = std::make_shared<DebugLogMethod>();
if (ServerInstance->Config->CommandLine.forceprotodebug)
{
// If we are doing a protocol debug we need to warn users.
loggers.emplace_back(Level::RAWIO, std::move(types), std::move(method), false, &stdoutlog);
ServerInstance->Config->RawLog = true;
}
else
{
loggers.emplace_back(Level::DEBUG, std::move(types), std::move(method), false, &stdoutlog);
}
}
void Log::Manager::OpenLogs(bool requiremethods)
{
// If the server is started in debug mode we don't write logs.
if (ServerInstance->Config->CommandLine.forcedebug)
{
const auto* option = ServerInstance->Config->CommandLine.forceprotodebug ? "--protocoldebug" : "--debug";
Normal("LOG", "Not opening loggers because we were started with {}", option);
CheckRawLog();
return;
}
// If the server is started with logging disabled we don't write logs.
if (!ServerInstance->Config->CommandLine.writelog)
{
Normal("LOG", "Not opening loggers because we were started with --nolog");
CheckRawLog();
return;
}
for (const auto& [_, tag] : ServerInstance->Config->ConfTags("log"))
{
const std::string methodstr = tag->getString("method", "file", 1);
Log::Engine* engine = ServerInstance->Modules.FindDataService<Log::Engine>("log/" + methodstr);
if (!engine)
{
if (!requiremethods)
continue; // We will open this later.
throw CoreException(methodstr + " is not a valid logging method at " + tag->source.str());
}
const Level level = tag->getEnum("level", Level::NORMAL, {
{ "critical", Level::CRITICAL },
{ "warning", Level::WARNING },
{ "normal", Level::NORMAL },
{ "debug", Level::DEBUG },
{ "rawio", Level::RAWIO },
// Deprecated v3 names.
{ "sparse", Level::CRITICAL },
{ "verbose", Level::WARNING },
{ "default", Level::NORMAL },
});
TokenList types = tag->getString("type", "*", 1);
MethodPtr method = engine->Create(tag);
loggers.emplace_back(level, std::move(types), method, true, engine);
}
if (requiremethods && caching)
{
// The server has finished starting up so we can write out any cached log messages.
for (auto& logger : loggers)
{
if (logger.dead || !logger.method->AcceptsCachedMessages())
continue; // Does not support logging.
for (const auto& message : cache)
{
if (!logger.Suitable(message.level, message.type))
continue;
try
{
logger.method->OnLog(message.time, message.level, message.type, message.message);
}
catch (const CoreException& err)
{
logger.dead = true;
logger.method.reset();
ServerInstance->SNO.WriteGlobalSno('a', "A logger threw an exception: {}", err.GetReason());
break;
}
}
}
cache.clear();
cache.shrink_to_fit();
caching = false;
}
CheckRawLog();
}
void Log::Manager::RegisterServices()
{
ServiceProvider* coreloggers[] = { &filelog, &stderrlog, &stdoutlog };
ServerInstance->Modules.AddServices(coreloggers, sizeof(coreloggers)/sizeof(ServiceProvider*));
}
void Log::Manager::UnloadEngine(const Engine* engine)
{
logging = true; // Prevent writing to dying loggers.
size_t logger_count = loggers.size();
loggers.erase(std::remove_if(loggers.begin(), loggers.end(), [&engine](const Info& info) { return info.engine == engine; }), loggers.end());
logging = false;
Normal("LOG", "The {} log engine is unloading; removed {}/{} loggers.", engine->name.c_str(), logger_count - loggers.size(), logger_count);
}
void Log::Manager::CheckRawLog()
{
// There might be a logger not from the config so we need to check this outside of the creation loop.
ServerInstance->Config->RawLog = std::any_of(loggers.begin(), loggers.end(), [](const auto& logger) {
return logger.level >= Level::RAWIO;
});
}
void Log::Manager::Write(Level level, const std::string& type, const std::string& message)
{
if (logging)
return; // Avoid log loops.
logging = true;
time_t time = ServerInstance->Time();
for (auto& logger : loggers)
{
if (!logger.Suitable(level, type))
continue;
try
{
logger.method->OnLog(time, level, type, message);
}
catch (const CoreException& err)
{
logger.dead = true;
logger.method.reset();
ServerInstance->SNO.WriteGlobalSno('a', "A logger threw an exception: {}", err.GetReason());
break;
}
}
if (caching)
cache.emplace_back(time, level, type, message);
logging = false;
}
|