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