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