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/>.
26 * Suggested implementation...
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)
35 * virtual void OnLog(enum loglevel, const std::string &msg)
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.
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.
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 *..
52 const char LogStream::LogHeader[] =
53 "Log started for " INSPIRCD_VERSION " (" INSPIRCD_REVISION ", " MODULE_INIT_STR ")"
54 " - compiled on " INSPIRCD_SYSTEM;
56 LogManager::LogManager()
61 LogManager::~LogManager()
65 void LogManager::OpenFileLogs()
67 if (ServerInstance->Config->cmdline.forcedebug)
69 ServerInstance->Config->RawLog = true;
72 /* Skip rest of logfile opening if we are running -nolog. */
73 if (!ServerInstance->Config->cmdline.writelog)
75 std::map<std::string, FileWriter*> logmap;
76 ConfigTagList tags = ServerInstance->Config->ConfTags("log");
77 for(ConfigIter i = tags.first; i != tags.second; ++i)
79 ConfigTag* tag = i->second;
80 std::string method = tag->getString("method");
85 std::string type = tag->getString("type");
86 std::string level = tag->getString("level");
87 LogLevel loglevel = LOG_DEFAULT;
91 ServerInstance->Config->RawLog = true;
93 else if (level == "debug")
97 else if (level == "verbose")
99 loglevel = LOG_VERBOSE;
101 else if (level == "default")
103 loglevel = LOG_DEFAULT;
105 else if (level == "sparse")
107 loglevel = LOG_SPARSE;
109 else if (level == "none")
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())
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));
130 FileLogStream* fls = new FileLogStream(loglevel, fw);
131 fls->OnLog(LOG_SPARSE, "HEADER", LogStream::LogHeader);
132 AddLogTypes(type, fls, true);
136 void LogManager::CloseLogs()
138 if (ServerInstance->Config && ServerInstance->Config->cmdline.forcedebug)
142 GlobalLogStreams.clear();
144 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
149 AllLogStreams.clear();
152 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
154 irc::spacesepstream css(types);
156 std::vector<std::string> excludes;
157 while (css.GetToken(tok))
163 if (tok.at(0) == '-')
166 excludes.push_back(tok.substr(1));
170 AddLogType(tok, l, autoclose);
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)
179 /* -* == Exclude all. Why someone would do this, I dunno. */
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())
189 gi->second.swap(excludes); // Swap with the vector in the hash.
193 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
195 LogStreams[type].push_back(l);
198 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
206 void LogManager::DelLogStream(LogStream* l)
208 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
210 std::vector<LogStream*>::iterator it;
211 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
217 GlobalLogStreams.erase(l);
219 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
220 if (ai == AllLogStreams.end())
226 AllLogStreams.erase(ai);
229 bool LogManager::DelLogType(const std::string &type, LogStream *l)
231 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
234 GlobalLogStreams.erase(l);
237 if (i != LogStreams.end())
239 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
241 if (it != i->second.end())
244 if (i->second.size() == 0)
259 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
260 if (ai == AllLogStreams.end())
265 if ((--ai->second) < 1)
267 AllLogStreams.erase(ai);
274 void LogManager::Log(const std::string &type, LogLevel loglevel, const char *fmt, ...)
280 VAFORMAT(buf, fmt, fmt);
281 this->Log(type, loglevel, buf);
284 void LogManager::Log(const std::string &type, LogLevel loglevel, const std::string &msg)
293 for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
295 if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
299 gi->first->OnLog(loglevel, type, msg);
302 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
304 if (i != LogStreams.end())
306 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
308 (*it)->OnLog(loglevel, type, msg);
316 FileWriter::FileWriter(FILE* logfile)
317 : log(logfile), writeops(0)
321 void FileWriter::WriteLogLine(const std::string &line)
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");
328 fputs(line.c_str(), log);
329 if (++writeops % 20 == 0)
335 FileWriter::~FileWriter()