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/>.
24 #include "filelogger.h"
27 * Suggested implementation...
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)
36 * virtual void OnLog(enum loglevel, const std::string &msg)
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.
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.
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 *..
53 const char LogStream::LogHeader[] =
54 "Log started for " VERSION " (" REVISION ", " MODULE_INIT_STR ")"
55 " - compiled on " SYSTEM;
57 LogManager::LogManager()
62 LogManager::~LogManager()
67 void LogManager::OpenFileLogs()
69 if (ServerInstance->Config->cmdline.forcedebug)
71 ServerInstance->Config->RawLog = true;
74 /* Skip rest of logfile opening if we are running -nolog. */
75 if (!ServerInstance->Config->cmdline.writelog)
77 std::map<std::string, FileWriter*> logmap;
78 ConfigTagList tags = ServerInstance->Config->ConfTags("log");
79 for(ConfigIter i = tags.first; i != tags.second; ++i)
81 ConfigTag* tag = i->second;
82 std::string method = tag->getString("method");
87 std::string type = tag->getString("type");
88 std::string level = tag->getString("level");
89 LogLevel loglevel = LOG_DEFAULT;
93 ServerInstance->Config->RawLog = true;
95 else if (level == "debug")
99 else if (level == "verbose")
101 loglevel = LOG_VERBOSE;
103 else if (level == "default")
105 loglevel = LOG_DEFAULT;
107 else if (level == "sparse")
109 loglevel = LOG_SPARSE;
111 else if (level == "none")
116 std::string target = tag->getString("target");
117 std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
118 if (fwi == logmap.end())
120 char realtarget[256];
121 time_t time = ServerInstance->Time();
122 struct tm *mytime = gmtime(&time);
123 strftime(realtarget, sizeof(realtarget), target.c_str(), mytime);
124 FILE* f = fopen(realtarget, "a");
125 fw = new FileWriter(f);
126 logmap.insert(std::make_pair(target, fw));
132 FileLogStream* fls = new FileLogStream(loglevel, fw);
133 fls->OnLog(LOG_SPARSE, "HEADER", LogStream::LogHeader);
134 AddLogTypes(type, fls, true);
138 void LogManager::CloseLogs()
140 if (ServerInstance->Config && ServerInstance->Config->cmdline.forcedebug)
144 GlobalLogStreams.clear();
146 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
151 AllLogStreams.clear();
154 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
156 irc::spacesepstream css(types);
158 std::vector<std::string> excludes;
159 while (css.GetToken(tok))
165 if (tok.at(0) == '-')
168 excludes.push_back(tok.substr(1));
172 AddLogType(tok, l, autoclose);
175 // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
176 // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
177 for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
181 /* -* == Exclude all. Why someone would do this, I dunno. */
187 // Now if it's registered as a global, add the exclusions there too.
188 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
189 if (gi != GlobalLogStreams.end())
191 gi->second.swap(excludes); // Swap with the vector in the hash.
195 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
197 LogStreams[type].push_back(l);
200 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
208 void LogManager::DelLogStream(LogStream* l)
210 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
212 std::vector<LogStream*>::iterator it;
213 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
219 GlobalLogStreams.erase(l);
221 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
222 if (ai == AllLogStreams.end())
228 AllLogStreams.erase(ai);
231 bool LogManager::DelLogType(const std::string &type, LogStream *l)
233 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
236 GlobalLogStreams.erase(l);
239 if (i != LogStreams.end())
241 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
243 if (it != i->second.end())
246 if (i->second.size() == 0)
261 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
262 if (ai == AllLogStreams.end())
267 if ((--ai->second) < 1)
269 AllLogStreams.erase(ai);
276 void LogManager::Log(const std::string &type, LogLevel loglevel, const char *fmt, ...)
282 VAFORMAT(buf, fmt, fmt);
283 this->Log(type, loglevel, buf);
286 void LogManager::Log(const std::string &type, LogLevel loglevel, const std::string &msg)
295 for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
297 if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
301 gi->first->OnLog(loglevel, type, msg);
304 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
306 if (i != LogStreams.end())
308 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
310 (*it)->OnLog(loglevel, type, msg);
318 FileWriter::FileWriter(FILE* logfile)
319 : log(logfile), writeops(0)
323 void FileWriter::WriteLogLine(const std::string &line)
327 // 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
328 // throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
330 fputs(line.c_str(), log);
331 if (++writeops % 20 == 0)
337 FileWriter::~FileWriter()