]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/logger.cpp
Default to file logging if a log type is not specified.
[user/henk/code/inspircd.git] / src / logger.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
7  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
9  *   Copyright (C) 2010 Adam <Adam@anope.org>
10  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
12  *   Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
13  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
14  *
15  * This file is part of InspIRCd.  InspIRCd is free software: you can
16  * redistribute it and/or modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation, version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26  */
27
28
29 #include "inspircd.h"
30
31 /*
32  * Suggested implementation...
33  *      class LogManager
34  *              bool AddLogType(const std::string &type, enum loglevel, LogStream *)
35  *              bool DelLogType(const std::string &type, LogStream *)
36  *              Log(const std::string &type, enum loglevel, const std::string &msg)
37  *              std::map<std::string, std::vector<LogStream *> > logstreams (holds a 'chain' of logstreams for each type that are all notified when a log happens)
38  *
39  *  class LogStream
40  *              std::string type
41  *      virtual void OnLog(enum loglevel, const std::string &msg)
42  *
43  * How it works:
44  *  Modules create their own logstream types (core will create one for 'file logging' for example) and create instances of these logstream types
45  *  and register interest in a certain logtype. Globbing is not here, with the exception of * - for all events.. loglevel is used to drop
46  *  events that are of no interest to a logstream.
47  *
48  *  When Log is called, the vector of logstreams for that type is iterated (along with the special vector for "*"), and all registered logstreams
49  *  are called back ("OnLog" or whatever) to do whatever they like with the message. In the case of the core, this will write to a file.
50  *  In the case of the module I plan to write (m_logtochannel or something), it will log to the channel(s) for that logstream, etc.
51  *
52  * NOTE: Somehow we have to let LogManager manage the non-blocking file streams and provide an interface to share them with various LogStreams,
53  *       as, for example, a user may want to let 'KILL' and 'XLINE' snotices go to /home/ircd/inspircd/logs/operactions.log, or whatever. How
54  *       can we accomplish this easily? I guess with a map of pre-loved logpaths, and a pointer of FILE *..
55  *
56  */
57
58 const char LogStream::LogHeader[] =
59         "Log started for " INSPIRCD_VERSION;
60
61 LogManager::LogManager()
62         : Logging(false)
63 {
64 }
65
66 LogManager::~LogManager()
67 {
68 }
69
70 void LogManager::OpenFileLogs()
71 {
72         if (ServerInstance->Config->cmdline.forcedebug)
73         {
74                 ServerInstance->Config->RawLog = true;
75                 return;
76         }
77         /* Skip rest of logfile opening if we are running -nolog. */
78         if (!ServerInstance->Config->cmdline.writelog)
79                 return;
80         std::map<std::string, FileWriter*> logmap;
81         ConfigTagList tags = ServerInstance->Config->ConfTags("log");
82         for(ConfigIter i = tags.first; i != tags.second; ++i)
83         {
84                 ConfigTag* tag = i->second;
85                 const std::string method = tag->getString("method", "file", 1);
86                 if (!stdalgo::string::equalsci(method, "file"))
87                 {
88                         continue;
89                 }
90                 std::string type = tag->getString("type");
91                 std::string level = tag->getString("level");
92                 LogLevel loglevel = LOG_DEFAULT;
93                 if (stdalgo::string::equalsci(level, "rawio"))
94                 {
95                         loglevel = LOG_RAWIO;
96                         ServerInstance->Config->RawLog = true;
97                 }
98                 else if (stdalgo::string::equalsci(level, "debug"))
99                 {
100                         loglevel = LOG_DEBUG;
101                 }
102                 else if (stdalgo::string::equalsci(level, "verbose"))
103                 {
104                         loglevel = LOG_VERBOSE;
105                 }
106                 else if (stdalgo::string::equalsci(level, "default"))
107                 {
108                         loglevel = LOG_DEFAULT;
109                 }
110                 else if (stdalgo::string::equalsci(level, "sparse"))
111                 {
112                         loglevel = LOG_SPARSE;
113                 }
114                 else if (stdalgo::string::equalsci(level, "none"))
115                 {
116                         loglevel = LOG_NONE;
117                 }
118                 FileWriter* fw;
119                 std::string target = ServerInstance->Config->Paths.PrependLog(tag->getString("target"));
120                 std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
121                 if (fwi == logmap.end())
122                 {
123                         char realtarget[256];
124                         time_t time = ServerInstance->Time();
125                         struct tm *mytime = gmtime(&time);
126                         strftime(realtarget, sizeof(realtarget), target.c_str(), mytime);
127                         FILE* f = fopen(realtarget, "a");
128                         fw = new FileWriter(f, tag->getUInt("flush", 20, 1, UINT_MAX));
129                         logmap.insert(std::make_pair(target, fw));
130                 }
131                 else
132                 {
133                         fw = fwi->second;
134                 }
135                 FileLogStream* fls = new FileLogStream(loglevel, fw);
136                 fls->OnLog(LOG_SPARSE, "HEADER", LogStream::LogHeader);
137                 AddLogTypes(type, fls, true);
138         }
139 }
140
141 void LogManager::CloseLogs()
142 {
143         if (ServerInstance->Config && ServerInstance->Config->cmdline.forcedebug)
144                 return;
145
146         LogStreams.clear();
147         GlobalLogStreams.clear();
148
149         for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
150         {
151                 delete i->first;
152         }
153
154         AllLogStreams.clear();
155 }
156
157 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
158 {
159         irc::spacesepstream css(types);
160         std::string tok;
161         std::vector<std::string> excludes;
162         while (css.GetToken(tok))
163         {
164                 if (tok.empty())
165                 {
166                         continue;
167                 }
168                 if (tok.at(0) == '-')
169                 {
170                         /* Exclude! */
171                         excludes.push_back(tok.substr(1));
172                 }
173                 else
174                 {
175                         AddLogType(tok, l, autoclose);
176                 }
177         }
178         // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
179         // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
180         for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
181         {
182                 if (*i == "*")
183                 {
184                         /* -* == Exclude all. Why someone would do this, I dunno. */
185                         DelLogStream(l);
186                         return;
187                 }
188                 DelLogType(*i, l);
189         }
190         // Now if it's registered as a global, add the exclusions there too.
191         std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
192         if (gi != GlobalLogStreams.end())
193         {
194                 gi->second.swap(excludes); // Swap with the vector in the hash.
195         }
196 }
197
198 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
199 {
200         LogStreams[type].push_back(l);
201
202         if (type == "*")
203                 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
204
205         if (autoclose)
206                 AllLogStreams[l]++;
207
208         return true;
209 }
210
211 void LogManager::DelLogStream(LogStream* l)
212 {
213         for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
214         {
215                 while (stdalgo::erase(i->second, l))
216                 {
217                         // Keep erasing while it exists
218                 }
219         }
220
221         GlobalLogStreams.erase(l);
222
223         std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
224         if (ai == AllLogStreams.end())
225         {
226                 return; /* Done. */
227         }
228
229         delete ai->first;
230         AllLogStreams.erase(ai);
231 }
232
233 bool LogManager::DelLogType(const std::string &type, LogStream *l)
234 {
235         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
236         if (type == "*")
237         {
238                 GlobalLogStreams.erase(l);
239         }
240
241         if (i != LogStreams.end())
242         {
243                 if (stdalgo::erase(i->second, l))
244                 {
245                         if (i->second.size() == 0)
246                         {
247                                 LogStreams.erase(i);
248                         }
249                 }
250                 else
251                 {
252                         return false;
253                 }
254         }
255         else
256         {
257                 return false;
258         }
259
260         std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
261         if (ai == AllLogStreams.end())
262         {
263                 return true;
264         }
265
266         if ((--ai->second) < 1)
267         {
268                 AllLogStreams.erase(ai);
269                 delete l;
270         }
271
272         return true;
273 }
274
275 void LogManager::Log(const std::string &type, LogLevel loglevel, const char *fmt, ...)
276 {
277         if (Logging)
278                 return;
279
280         std::string buf;
281         VAFORMAT(buf, fmt, fmt);
282         this->Log(type, loglevel, buf);
283 }
284
285 void LogManager::Log(const std::string &type, LogLevel loglevel, const std::string &msg)
286 {
287         if (Logging)
288         {
289                 return;
290         }
291
292         Logging = true;
293
294         for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
295         {
296                 if (stdalgo::isin(gi->second, type))
297                 {
298                         continue;
299                 }
300                 gi->first->OnLog(loglevel, type, msg);
301         }
302
303         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
304
305         if (i != LogStreams.end())
306         {
307                 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
308                 {
309                         (*it)->OnLog(loglevel, type, msg);
310                 }
311         }
312
313         Logging = false;
314 }
315
316
317 FileWriter::FileWriter(FILE* logfile, unsigned int flushcount)
318         : log(logfile)
319         , flush(flushcount)
320         , writeops(0)
321 {
322 }
323
324 void FileWriter::WriteLogLine(const std::string &line)
325 {
326         if (log == NULL)
327                 return;
328 // XXX: For now, just return. Don't throw an exception. It'd be nice to find out if this is happening, but I'm terrified of breaking so close to final release. -- w00t
329 //              throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
330
331         fputs(line.c_str(), log);
332         if (++writeops % flush == 0)
333         {
334                 fflush(log);
335         }
336 }
337
338 FileWriter::~FileWriter()
339 {
340         if (log)
341         {
342                 fflush(log);
343                 fclose(log);
344                 log = NULL;
345         }
346 }