]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/logger.cpp
Threadengine stuff
[user/henk/code/inspircd.git] / src / logger.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core: libIRCDlogger */
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(ServerInstance, stdout);
52                 noforkstream = new FileLogStream(ServerInstance, ServerInstance->Config->forcedebug ? DEBUG : ServerInstance->Config->LogLevel, fw);
53         }
54         else
55         {
56                 noforkstream->ChangeLevel(ServerInstance->Config->forcedebug ? DEBUG : ServerInstance->Config->LogLevel);
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(ServerInstance);
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(ServerInstance, f);
113                         logmap.insert(std::make_pair(target, fw));
114                 }
115                 else
116                 {
117                         fw = i->second;
118                 }
119                 FileLogStream* fls = new FileLogStream(ServerInstance, 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(InspIRCd* Instance, FILE* logfile)
334 : ServerInstance(Instance), log(logfile), writeops(0)
335 {
336         if (log)
337         {
338                 Instance->SE->NonBlocking(fileno(log));
339                 SetFd(fileno(log));
340                 buffer.clear();
341         }
342 }
343
344 bool FileWriter::Readable()
345 {
346         return false;
347 }
348
349 void FileWriter::HandleEvent(EventType, int)
350 {
351         WriteLogLine("");
352         if (log)
353         {
354                 ServerInstance->SE->DelFd(this);
355         }
356 }
357
358 void FileWriter::WriteLogLine(const std::string &line)
359 {
360         if (line.length())
361         {
362                 buffer.append(line);
363         }
364
365         if (log)
366         {
367                 int written = fprintf(log,"%s",buffer.c_str());
368 #ifdef WINDOWS
369                 buffer.clear();
370 #else
371                 if ((written >= 0) && (written < (int)buffer.length()))
372                 {
373                         buffer.erase(0, buffer.length());
374                         ServerInstance->SE->AddFd(this);
375                 }
376                 else if (written == -1)
377                 {
378                         if (errno == EAGAIN)
379                                 ServerInstance->SE->AddFd(this);
380                 }
381                 else
382                 {
383                         /* Wrote the whole buffer, and no need for write callback */
384                         buffer.clear();
385                 }
386 #endif
387                 if (writeops++ % 20)
388                 {
389                         fflush(log);
390                 }
391         }
392 }
393
394 void FileWriter::Close()
395 {
396         if (log)
397         {
398                 ServerInstance->SE->Blocking(fileno(log));
399
400                 if (buffer.size())
401                 {
402                         fprintf(log,"%s",buffer.c_str());
403                 }
404
405 #ifndef WINDOWS
406                 ServerInstance->SE->DelFd(this);
407 #endif
408
409                 fflush(log);
410                 fclose(log);
411         }
412
413         buffer.clear();
414 }
415
416 FileWriter::~FileWriter()
417 {
418         this->Close();
419 }