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