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