]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/logger.cpp
Move typedef ClassVector to ServerConfig::ClassVector
[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                 std::vector<LogStream*>::iterator it;
211                 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
212                 {
213                         i->second.erase(it);
214                 }
215         }
216
217         GlobalLogStreams.erase(l);
218
219         std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
220         if (ai == AllLogStreams.end())
221         {
222                 return; /* Done. */
223         }
224
225         delete ai->first;
226         AllLogStreams.erase(ai);
227 }
228
229 bool LogManager::DelLogType(const std::string &type, LogStream *l)
230 {
231         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
232         if (type == "*")
233         {
234                 GlobalLogStreams.erase(l);
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, LogLevel loglevel, const char *fmt, ...)
275 {
276         if (Logging)
277                 return;
278
279         std::string buf;
280         VAFORMAT(buf, fmt, fmt);
281         this->Log(type, loglevel, buf);
282 }
283
284 void LogManager::Log(const std::string &type, LogLevel loglevel, const std::string &msg)
285 {
286         if (Logging)
287         {
288                 return;
289         }
290
291         Logging = true;
292
293         for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
294         {
295                 if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
296                 {
297                         continue;
298                 }
299                 gi->first->OnLog(loglevel, type, msg);
300         }
301
302         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
303
304         if (i != LogStreams.end())
305         {
306                 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
307                 {
308                         (*it)->OnLog(loglevel, type, msg);
309                 }
310         }
311
312         Logging = false;
313 }
314
315
316 FileWriter::FileWriter(FILE* logfile)
317 : log(logfile), writeops(0)
318 {
319 }
320
321 void FileWriter::WriteLogLine(const std::string &line)
322 {
323         if (log == NULL)
324                 return;
325 // 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
326 //              throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
327
328         fputs(line.c_str(), log);
329         if (++writeops % 20 == 0)
330         {
331                 fflush(log);
332         }
333 }
334
335 FileWriter::~FileWriter()
336 {
337         if (log)
338         {
339                 fflush(log);
340                 fclose(log);
341                 log = NULL;
342         }
343 }