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