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