]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/logger.cpp
Add RAWIO log level which is more verbose than DEBUG
[user/henk/code/inspircd.git] / src / logger.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 LogManager::LogManager()
46 {
47         Logging = false;
48 }
49
50 LogManager::~LogManager()
51 {
52 }
53
54 void LogManager::OpenFileLogs()
55 {
56         /* Skip rest of logfile opening if we are running -nolog. */
57         if (!ServerInstance->Config->cmdline.writelog)
58                 return;
59         std::map<std::string, FileWriter*> logmap;
60         ConfigTagList tags = ServerInstance->Config->ConfTags("log");
61         for(ConfigIter i = tags.first; i != tags.second; ++i)
62         {
63                 ConfigTag* tag = i->second;
64                 std::string method = tag->getString("method");
65                 if (method != "file")
66                 {
67                         continue;
68                 }
69                 std::string type = tag->getString("type");
70                 std::string level = tag->getString("level");
71                 int loglevel = DEFAULT;
72                 if (level == "rawio")
73                 {
74                         loglevel = RAWIO;
75                         ServerInstance->Config->RawLog = true;
76                 }
77                 else if (level == "debug")
78                 {
79                         loglevel = DEBUG;
80                 }
81                 else if (level == "verbose")
82                 {
83                         loglevel = VERBOSE;
84                 }
85                 else if (level == "default")
86                 {
87                         loglevel = DEFAULT;
88                 }
89                 else if (level == "sparse")
90                 {
91                         loglevel = SPARSE;
92                 }
93                 else if (level == "none")
94                 {
95                         loglevel = NONE;
96                 }
97                 FileWriter* fw;
98                 std::string target = tag->getString("target");
99                 std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
100                 if (fwi == logmap.end())
101                 {
102                         char realtarget[MAXBUF];
103                         time_t time = ServerInstance->Time();
104                         struct tm *mytime = gmtime(&time);
105                         strftime(realtarget, MAXBUF, target.c_str(), mytime);
106                         FILE* f = fopen(realtarget, "a");
107                         fw = new FileWriter(f);
108                         logmap.insert(std::make_pair(target, fw));
109                 }
110                 else
111                 {
112                         fw = fwi->second;
113                 }
114                 FileLogStream* fls = new FileLogStream(loglevel, fw);
115                 AddLogTypes(type, fls, true);
116         }
117 }
118
119 void LogManager::CloseLogs()
120 {
121         std::map<std::string, std::vector<LogStream*> >().swap(LogStreams); /* Clear it */
122         std::map<LogStream*, std::vector<std::string> >().swap(GlobalLogStreams); /* Clear it */
123         for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
124         {
125                 delete i->first;
126         }
127         std::map<LogStream*, int>().swap(AllLogStreams); /* And clear it */
128 }
129
130 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
131 {
132         irc::spacesepstream css(types);
133         std::string tok;
134         std::vector<std::string> excludes;
135         while (css.GetToken(tok))
136         {
137                 if (tok.empty())
138                 {
139                         continue;
140                 }
141                 if (tok.at(0) == '-')
142                 {
143                         /* Exclude! */
144                         excludes.push_back(tok.substr(1));
145                 }
146                 else
147                 {
148                         AddLogType(tok, l, autoclose);
149                 }
150         }
151         // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
152         // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
153         for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
154         {
155                 if (*i == "*")
156                 {
157                         /* -* == Exclude all. Why someone would do this, I dunno. */
158                         DelLogStream(l);
159                         return;
160                 }
161                 DelLogType(*i, l);
162         }
163         // Now if it's registered as a global, add the exclusions there too.
164         std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
165         if (gi != GlobalLogStreams.end())
166         {
167                 gi->second.swap(excludes); // Swap with the vector in the hash.
168         }
169 }
170
171 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
172 {
173         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
174
175         if (i != LogStreams.end())
176         {
177                 i->second.push_back(l);
178         }
179         else
180         {
181                 std::vector<LogStream *> v;
182                 v.push_back(l);
183                 LogStreams[type] = v;
184         }
185
186         if (type == "*")
187         {
188                 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
189         }
190
191         if (autoclose)
192         {
193                 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
194                 if (ai == AllLogStreams.end())
195                 {
196                         AllLogStreams.insert(std::make_pair(l, 1));
197                 }
198                 else
199                 {
200                         ++ai->second;
201                 }
202         }
203
204         return true;
205 }
206
207 void LogManager::DelLogStream(LogStream* l)
208 {
209         for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
210         {
211                 std::vector<LogStream*>::iterator it;
212                 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
213                 {
214                         if (it == i->second.end())
215                                 continue;
216                         i->second.erase(it);
217                 }
218         }
219         std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
220         if (gi != GlobalLogStreams.end())
221         {
222                 GlobalLogStreams.erase(gi);
223         }
224         std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
225         if (ai == AllLogStreams.end())
226         {
227                 return; /* Done. */
228         }
229         delete ai->first;
230         AllLogStreams.erase(ai);
231 }
232
233 bool LogManager::DelLogType(const std::string &type, LogStream *l)
234 {
235         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
236         if (type == "*")
237         {
238                 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
239                 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
240         }
241
242         if (i != LogStreams.end())
243         {
244                 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
245
246                 if (it != i->second.end())
247                 {
248                         i->second.erase(it);
249                         if (i->second.size() == 0)
250                         {
251                                 LogStreams.erase(i);
252                         }
253                 }
254                 else
255                 {
256                         return false;
257                 }
258         }
259         else
260         {
261                 return false;
262         }
263
264         std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
265         if (ai == AllLogStreams.end())
266         {
267                 return true;
268         }
269
270         if ((--ai->second) < 1)
271         {
272                 AllLogStreams.erase(ai);
273                 delete l;
274         }
275
276         return true;
277 }
278
279 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
280 {
281         if (Logging)
282         {
283                 return;
284         }
285
286         va_list a;
287         static char buf[65536];
288
289         va_start(a, fmt);
290         vsnprintf(buf, 65536, fmt, a);
291         va_end(a);
292
293         this->Log(type, loglevel, std::string(buf));
294 }
295
296 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
297 {
298         if (Logging)
299         {
300                 return;
301         }
302
303         Logging = true;
304
305         for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
306         {
307                 if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
308                 {
309                         continue;
310                 }
311                 gi->first->OnLog(loglevel, type, msg);
312         }
313
314         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
315
316         if (i != LogStreams.end())
317         {
318                 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
319                 {
320                         (*it)->OnLog(loglevel, type, msg);
321                 }
322         }
323
324         Logging = false;
325 }
326
327
328 FileWriter::FileWriter(FILE* logfile)
329 : log(logfile), writeops(0)
330 {
331 }
332
333 void FileWriter::WriteLogLine(const std::string &line)
334 {
335         if (log == NULL)
336                 return;
337 // 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
338 //              throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
339
340         fprintf(log,"%s",line.c_str());
341         if (writeops++ % 20)
342         {
343                 fflush(log);
344         }
345 }
346
347 FileWriter::~FileWriter()
348 {
349         if (log)
350         {
351                 fflush(log);
352                 fclose(log);
353                 log = NULL;
354         }
355 }