]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/logger.cpp
Add stdalgo::isin() and use it to simplify code
[user/henk/code/inspircd.git] / src / logger.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24
25 /*
26  * Suggested implementation...
27  *      class LogManager
28  *              bool AddLogType(const std::string &type, enum loglevel, LogStream *)
29  *              bool DelLogType(const std::string &type, LogStream *)
30  *              Log(const std::string &type, enum loglevel, const std::string &msg)
31  *              std::map<std::string, std::vector<LogStream *> > logstreams (holds a 'chain' of logstreams for each type that are all notified when a log happens)
32  *
33  *  class LogStream
34  *              std::string type
35  *      virtual void OnLog(enum loglevel, const std::string &msg)
36  *
37  * How it works:
38  *  Modules create their own logstream types (core will create one for 'file logging' for example) and create instances of these logstream types
39  *  and register interest in a certain logtype. Globbing is not here, with the exception of * - for all events.. loglevel is used to drop
40  *  events that are of no interest to a logstream.
41  *
42  *  When Log is called, the vector of logstreams for that type is iterated (along with the special vector for "*"), and all registered logstreams
43  *  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.
44  *  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.
45  *
46  * NOTE: Somehow we have to let LogManager manage the non-blocking file streams and provide an interface to share them with various LogStreams,
47  *       as, for example, a user may want to let 'KILL' and 'XLINE' snotices go to /home/ircd/inspircd/logs/operactions.log, or whatever. How
48  *       can we accomplish this easily? I guess with a map of pre-loved logpaths, and a pointer of FILE *..
49  *
50  */
51
52 const char LogStream::LogHeader[] =
53         "Log started for " INSPIRCD_VERSION " (" INSPIRCD_REVISION ", " MODULE_INIT_STR ")"
54         " - compiled on " INSPIRCD_SYSTEM;
55
56 LogManager::LogManager()
57         : Logging(false)
58 {
59 }
60
61 LogManager::~LogManager()
62 {
63 }
64
65 void LogManager::OpenFileLogs()
66 {
67         if (ServerInstance->Config->cmdline.forcedebug)
68         {
69                 ServerInstance->Config->RawLog = true;
70                 return;
71         }
72         /* Skip rest of logfile opening if we are running -nolog. */
73         if (!ServerInstance->Config->cmdline.writelog)
74                 return;
75         std::map<std::string, FileWriter*> logmap;
76         ConfigTagList tags = ServerInstance->Config->ConfTags("log");
77         for(ConfigIter i = tags.first; i != tags.second; ++i)
78         {
79                 ConfigTag* tag = i->second;
80                 std::string method = tag->getString("method");
81                 if (method != "file")
82                 {
83                         continue;
84                 }
85                 std::string type = tag->getString("type");
86                 std::string level = tag->getString("level");
87                 LogLevel loglevel = LOG_DEFAULT;
88                 if (level == "rawio")
89                 {
90                         loglevel = LOG_RAWIO;
91                         ServerInstance->Config->RawLog = true;
92                 }
93                 else if (level == "debug")
94                 {
95                         loglevel = LOG_DEBUG;
96                 }
97                 else if (level == "verbose")
98                 {
99                         loglevel = LOG_VERBOSE;
100                 }
101                 else if (level == "default")
102                 {
103                         loglevel = LOG_DEFAULT;
104                 }
105                 else if (level == "sparse")
106                 {
107                         loglevel = LOG_SPARSE;
108                 }
109                 else if (level == "none")
110                 {
111                         loglevel = LOG_NONE;
112                 }
113                 FileWriter* fw;
114                 std::string target = ServerInstance->Config->Paths.PrependLog(tag->getString("target"));
115                 std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
116                 if (fwi == logmap.end())
117                 {
118                         char realtarget[256];
119                         time_t time = ServerInstance->Time();
120                         struct tm *mytime = gmtime(&time);
121                         strftime(realtarget, sizeof(realtarget), target.c_str(), mytime);
122                         FILE* f = fopen(realtarget, "a");
123                         fw = new FileWriter(f);
124                         logmap.insert(std::make_pair(target, fw));
125                 }
126                 else
127                 {
128                         fw = fwi->second;
129                 }
130                 FileLogStream* fls = new FileLogStream(loglevel, fw);
131                 fls->OnLog(LOG_SPARSE, "HEADER", LogStream::LogHeader);
132                 AddLogTypes(type, fls, true);
133         }
134 }
135
136 void LogManager::CloseLogs()
137 {
138         if (ServerInstance->Config && ServerInstance->Config->cmdline.forcedebug)
139                 return;
140
141         LogStreams.clear();
142         GlobalLogStreams.clear();
143
144         for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
145         {
146                 delete i->first;
147         }
148
149         AllLogStreams.clear();
150 }
151
152 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
153 {
154         irc::spacesepstream css(types);
155         std::string tok;
156         std::vector<std::string> excludes;
157         while (css.GetToken(tok))
158         {
159                 if (tok.empty())
160                 {
161                         continue;
162                 }
163                 if (tok.at(0) == '-')
164                 {
165                         /* Exclude! */
166                         excludes.push_back(tok.substr(1));
167                 }
168                 else
169                 {
170                         AddLogType(tok, l, autoclose);
171                 }
172         }
173         // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
174         // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
175         for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
176         {
177                 if (*i == "*")
178                 {
179                         /* -* == Exclude all. Why someone would do this, I dunno. */
180                         DelLogStream(l);
181                         return;
182                 }
183                 DelLogType(*i, l);
184         }
185         // Now if it's registered as a global, add the exclusions there too.
186         std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
187         if (gi != GlobalLogStreams.end())
188         {
189                 gi->second.swap(excludes); // Swap with the vector in the hash.
190         }
191 }
192
193 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
194 {
195         LogStreams[type].push_back(l);
196
197         if (type == "*")
198                 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
199
200         if (autoclose)
201                 AllLogStreams[l]++;
202
203         return true;
204 }
205
206 void LogManager::DelLogStream(LogStream* l)
207 {
208         for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
209         {
210                 while (stdalgo::erase(i->second, l))
211                 {
212                         // Keep erasing while it exists
213                 }
214         }
215
216         GlobalLogStreams.erase(l);
217
218         std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
219         if (ai == AllLogStreams.end())
220         {
221                 return; /* Done. */
222         }
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                 GlobalLogStreams.erase(l);
234         }
235
236         if (i != LogStreams.end())
237         {
238                 if (stdalgo::erase(i->second, l))
239                 {
240                         if (i->second.size() == 0)
241                         {
242                                 LogStreams.erase(i);
243                         }
244                 }
245                 else
246                 {
247                         return false;
248                 }
249         }
250         else
251         {
252                 return false;
253         }
254
255         std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
256         if (ai == AllLogStreams.end())
257         {
258                 return true;
259         }
260
261         if ((--ai->second) < 1)
262         {
263                 AllLogStreams.erase(ai);
264                 delete l;
265         }
266
267         return true;
268 }
269
270 void LogManager::Log(const std::string &type, LogLevel loglevel, const char *fmt, ...)
271 {
272         if (Logging)
273                 return;
274
275         std::string buf;
276         VAFORMAT(buf, fmt, fmt);
277         this->Log(type, loglevel, buf);
278 }
279
280 void LogManager::Log(const std::string &type, LogLevel loglevel, const std::string &msg)
281 {
282         if (Logging)
283         {
284                 return;
285         }
286
287         Logging = true;
288
289         for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
290         {
291                 if (stdalgo::isin(gi->second, type))
292                 {
293                         continue;
294                 }
295                 gi->first->OnLog(loglevel, type, msg);
296         }
297
298         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
299
300         if (i != LogStreams.end())
301         {
302                 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
303                 {
304                         (*it)->OnLog(loglevel, type, msg);
305                 }
306         }
307
308         Logging = false;
309 }
310
311
312 FileWriter::FileWriter(FILE* logfile)
313 : log(logfile), writeops(0)
314 {
315 }
316
317 void FileWriter::WriteLogLine(const std::string &line)
318 {
319         if (log == NULL)
320                 return;
321 // 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
322 //              throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
323
324         fputs(line.c_str(), log);
325         if (++writeops % 20 == 0)
326         {
327                 fflush(log);
328         }
329 }
330
331 FileWriter::~FileWriter()
332 {
333         if (log)
334         {
335                 fflush(log);
336                 fclose(log);
337                 log = NULL;
338         }
339 }