2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
25 #include "filelogger.h"
28 * Suggested implementation...
30 * bool AddLogType(const std::string &type, enum loglevel, LogStream *)
31 * bool DelLogType(const std::string &type, LogStream *)
32 * Log(const std::string &type, enum loglevel, const std::string &msg)
33 * std::map<std::string, std::vector<LogStream *> > logstreams (holds a 'chain' of logstreams for each type that are all notified when a log happens)
37 * virtual void OnLog(enum loglevel, const std::string &msg)
40 * Modules create their own logstream types (core will create one for 'file logging' for example) and create instances of these logstream types
41 * and register interest in a certain logtype. Globbing is not here, with the exception of * - for all events.. loglevel is used to drop
42 * events that are of no interest to a logstream.
44 * When Log is called, the vector of logstreams for that type is iterated (along with the special vector for "*"), and all registered logstreams
45 * 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.
46 * 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.
48 * NOTE: Somehow we have to let LogManager manage the non-blocking file streams and provide an interface to share them with various LogStreams,
49 * as, for example, a user may want to let 'KILL' and 'XLINE' snotices go to /home/ircd/inspircd/logs/operactions.log, or whatever. How
50 * can we accomplish this easily? I guess with a map of pre-loved logpaths, and a pointer of FILE *..
54 LogManager::LogManager()
59 LogManager::~LogManager()
63 void LogManager::OpenFileLogs()
65 if (ServerInstance->Config->cmdline.forcedebug)
67 ServerInstance->Config->RawLog = true;
70 /* Skip rest of logfile opening if we are running -nolog. */
71 if (!ServerInstance->Config->cmdline.writelog)
73 std::map<std::string, FileWriter*> logmap;
74 ConfigTagList tags = ServerInstance->Config->ConfTags("log");
75 for(ConfigIter i = tags.first; i != tags.second; ++i)
77 ConfigTag* tag = i->second;
78 std::string method = tag->getString("method");
83 std::string type = tag->getString("type");
84 std::string level = tag->getString("level");
85 int loglevel = DEFAULT;
89 ServerInstance->Config->RawLog = true;
91 else if (level == "debug")
95 else if (level == "verbose")
99 else if (level == "default")
103 else if (level == "sparse")
107 else if (level == "none")
112 std::string target = tag->getString("target");
113 std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
114 if (fwi == logmap.end())
116 char realtarget[MAXBUF];
117 time_t time = ServerInstance->Time();
118 struct tm *mytime = gmtime(&time);
119 strftime(realtarget, MAXBUF, target.c_str(), mytime);
120 FILE* f = fopen(realtarget, "a");
121 fw = new FileWriter(f);
122 logmap.insert(std::make_pair(target, fw));
128 FileLogStream* fls = new FileLogStream(loglevel, fw);
129 fls->OnLog(SPARSE, "HEADER", InspIRCd::LogHeader);
130 AddLogTypes(type, fls, true);
134 void LogManager::CloseLogs()
136 if (ServerInstance->Config && ServerInstance->Config->cmdline.forcedebug)
138 std::map<std::string, std::vector<LogStream*> >().swap(LogStreams); /* Clear it */
139 std::map<LogStream*, std::vector<std::string> >().swap(GlobalLogStreams); /* Clear it */
140 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
144 std::map<LogStream*, int>().swap(AllLogStreams); /* And clear it */
147 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
149 irc::spacesepstream css(types);
151 std::vector<std::string> excludes;
152 while (css.GetToken(tok))
158 if (tok.at(0) == '-')
161 excludes.push_back(tok.substr(1));
165 AddLogType(tok, l, autoclose);
168 // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
169 // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
170 for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
174 /* -* == Exclude all. Why someone would do this, I dunno. */
180 // Now if it's registered as a global, add the exclusions there too.
181 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
182 if (gi != GlobalLogStreams.end())
184 gi->second.swap(excludes); // Swap with the vector in the hash.
188 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
190 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
192 if (i != LogStreams.end())
194 i->second.push_back(l);
198 std::vector<LogStream *> v;
200 LogStreams[type] = v;
205 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
210 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
211 if (ai == AllLogStreams.end())
213 AllLogStreams.insert(std::make_pair(l, 1));
224 void LogManager::DelLogStream(LogStream* l)
226 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
228 std::vector<LogStream*>::iterator it;
229 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
231 if (it == i->second.end())
236 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
237 if (gi != GlobalLogStreams.end())
239 GlobalLogStreams.erase(gi);
241 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
242 if (ai == AllLogStreams.end())
247 AllLogStreams.erase(ai);
250 bool LogManager::DelLogType(const std::string &type, LogStream *l)
252 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
255 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
256 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
259 if (i != LogStreams.end())
261 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
263 if (it != i->second.end())
266 if (i->second.size() == 0)
281 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
282 if (ai == AllLogStreams.end())
287 if ((--ai->second) < 1)
289 AllLogStreams.erase(ai);
296 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
304 static char buf[65536];
307 vsnprintf(buf, 65536, fmt, a);
310 this->Log(type, loglevel, std::string(buf));
313 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
322 for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
324 if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
328 gi->first->OnLog(loglevel, type, msg);
331 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
333 if (i != LogStreams.end())
335 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
337 (*it)->OnLog(loglevel, type, msg);
345 FileWriter::FileWriter(FILE* logfile)
346 : log(logfile), writeops(0)
350 void FileWriter::WriteLogLine(const std::string &line)
354 // 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
355 // throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
357 fprintf(log,"%s",line.c_str());
364 FileWriter::~FileWriter()