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 " (" MODULE_INIT_STR ")";
55 LogManager::LogManager()
60 LogManager::~LogManager()
64 void LogManager::OpenFileLogs()
66 if (ServerInstance->Config->cmdline.forcedebug)
68 ServerInstance->Config->RawLog = true;
71 /* Skip rest of logfile opening if we are running -nolog. */
72 if (!ServerInstance->Config->cmdline.writelog)
74 std::map<std::string, FileWriter*> logmap;
75 ConfigTagList tags = ServerInstance->Config->ConfTags("log");
76 for(ConfigIter i = tags.first; i != tags.second; ++i)
78 ConfigTag* tag = i->second;
79 std::string method = tag->getString("method");
84 std::string type = tag->getString("type");
85 std::string level = tag->getString("level");
86 LogLevel loglevel = LOG_DEFAULT;
90 ServerInstance->Config->RawLog = true;
92 else if (level == "debug")
96 else if (level == "verbose")
98 loglevel = LOG_VERBOSE;
100 else if (level == "default")
102 loglevel = LOG_DEFAULT;
104 else if (level == "sparse")
106 loglevel = LOG_SPARSE;
108 else if (level == "none")
113 std::string target = ServerInstance->Config->Paths.PrependLog(tag->getString("target"));
114 std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
115 if (fwi == logmap.end())
117 char realtarget[256];
118 time_t time = ServerInstance->Time();
119 struct tm *mytime = gmtime(&time);
120 strftime(realtarget, sizeof(realtarget), target.c_str(), mytime);
121 FILE* f = fopen(realtarget, "a");
122 fw = new FileWriter(f, static_cast<unsigned int>(tag->getInt("flush", 20, 1, INT_MAX)));
123 logmap.insert(std::make_pair(target, fw));
129 FileLogStream* fls = new FileLogStream(loglevel, fw);
130 fls->OnLog(LOG_SPARSE, "HEADER", LogStream::LogHeader);
131 AddLogTypes(type, fls, true);
135 void LogManager::CloseLogs()
137 if (ServerInstance->Config && ServerInstance->Config->cmdline.forcedebug)
141 GlobalLogStreams.clear();
143 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
148 AllLogStreams.clear();
151 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
153 irc::spacesepstream css(types);
155 std::vector<std::string> excludes;
156 while (css.GetToken(tok))
162 if (tok.at(0) == '-')
165 excludes.push_back(tok.substr(1));
169 AddLogType(tok, l, autoclose);
172 // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
173 // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
174 for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
178 /* -* == Exclude all. Why someone would do this, I dunno. */
184 // Now if it's registered as a global, add the exclusions there too.
185 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
186 if (gi != GlobalLogStreams.end())
188 gi->second.swap(excludes); // Swap with the vector in the hash.
192 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
194 LogStreams[type].push_back(l);
197 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
205 void LogManager::DelLogStream(LogStream* l)
207 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
209 while (stdalgo::erase(i->second, l))
211 // Keep erasing while it exists
215 GlobalLogStreams.erase(l);
217 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
218 if (ai == AllLogStreams.end())
224 AllLogStreams.erase(ai);
227 bool LogManager::DelLogType(const std::string &type, LogStream *l)
229 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
232 GlobalLogStreams.erase(l);
235 if (i != LogStreams.end())
237 if (stdalgo::erase(i->second, l))
239 if (i->second.size() == 0)
254 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
255 if (ai == AllLogStreams.end())
260 if ((--ai->second) < 1)
262 AllLogStreams.erase(ai);
269 void LogManager::Log(const std::string &type, LogLevel loglevel, const char *fmt, ...)
275 VAFORMAT(buf, fmt, fmt);
276 this->Log(type, loglevel, buf);
279 void LogManager::Log(const std::string &type, LogLevel loglevel, const std::string &msg)
288 for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
290 if (stdalgo::isin(gi->second, type))
294 gi->first->OnLog(loglevel, type, msg);
297 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
299 if (i != LogStreams.end())
301 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
303 (*it)->OnLog(loglevel, type, msg);
311 FileWriter::FileWriter(FILE* logfile, unsigned int flushcount)
318 void FileWriter::WriteLogLine(const std::string &line)
322 // 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
323 // throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
325 fputs(line.c_str(), log);
326 if (++writeops % flush == 0)
332 FileWriter::~FileWriter()