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