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
|
/*
* 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"
const char* Log::LevelToString(Log::Level level)
{
switch (level)
{
case Log::Level::ERROR:
return "error";
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";
}
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 = InspIRCd::TimeString(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(InspIRCd::Format("Unable to write to %s: %s", name.c_str(), 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(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(InspIRCd::TimeString(ServerInstance->Time(), target.c_str()));
auto fh = fopen(fulltarget.c_str(), "a");
if (!fh)
{
throw CoreException(InspIRCd::Format("Unable to open %s for file logger at %s: %s",
fulltarget.c_str(), tag->source.str().c_str(), strerror(errno)));
}
const unsigned long flush = tag->getUInt("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(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)
{
}
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 = stdoutlog.Create(ServerInstance->Config->EmptyTag);
loggers.emplace_back(Level::RAWIO, 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->cmdline.forcedebug)
{
Normal("LOG", "Not opening loggers because we were started with --debug");
ServerInstance->Config->RawLog = true;
return;
}
// If the server is started with logging disabled we don't write logs.
if (!ServerInstance->Config->cmdline.writelog)
{
Normal("LOG", "Not opening loggers because we were started with --nolog");
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, {
{ "error", Level::ERROR },
{ "warning", Level::WARNING },
{ "normal", Level::NORMAL },
{ "debug", Level::DEBUG },
{ "rawio", Level::RAWIO },
// Deprecated v3 names.
{ "sparse", Level::ERROR },
{ "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 (const auto& logger : loggers)
{
if (!logger.method->AcceptsCachedMessages())
continue; // Does not support logging.
for (const auto& message : cache)
{
if (logger.level >= message.level && logger.types.Contains(message.type))
logger.method->OnLog(message.time, message.level, message.type, message.message);
}
}
cache.clear();
cache.shrink_to_fit();
caching = false;
}
}
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 %s log engine is unloading; removed %zu/%zu loggers.", engine->name.c_str(), logger_count - loggers.size(), logger_count);
}
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 (const auto& logger : loggers)
{
if (logger.level >= level && logger.types.Contains(type))
logger.method->OnLog(time, level, type, message);
}
if (caching)
cache.emplace_back(time, level, type, message);
logging = false;
}
void Log::Manager::Write(Level level, const std::string& type, const char* format, va_list& args)
{
Write(level, type, InspIRCd::Format(args, format));
}
|