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