2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
5 * Copyright (C) 2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
7 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
9 * Copyright (C) 2010 Adam <Adam@anope.org>
10 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
11 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
12 * Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
13 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
15 * This file is part of InspIRCd. InspIRCd is free software: you can
16 * redistribute it and/or modify it under the terms of the GNU General Public
17 * License as published by the Free Software Foundation, version 2.
19 * This program is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * Suggested implementation...
34 * bool AddLogType(const std::string &type, enum loglevel, LogStream *)
35 * bool DelLogType(const std::string &type, LogStream *)
36 * Log(const std::string &type, enum loglevel, const std::string &msg)
37 * std::map<std::string, std::vector<LogStream *> > logstreams (holds a 'chain' of logstreams for each type that are all notified when a log happens)
41 * virtual void OnLog(enum loglevel, const std::string &msg)
44 * Modules create their own logstream types (core will create one for 'file logging' for example) and create instances of these logstream types
45 * and register interest in a certain logtype. Globbing is not here, with the exception of * - for all events.. loglevel is used to drop
46 * events that are of no interest to a logstream.
48 * When Log is called, the vector of logstreams for that type is iterated (along with the special vector for "*"), and all registered logstreams
49 * 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.
50 * 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.
52 * NOTE: Somehow we have to let LogManager manage the non-blocking file streams and provide an interface to share them with various LogStreams,
53 * as, for example, a user may want to let 'KILL' and 'XLINE' snotices go to /home/ircd/inspircd/logs/operactions.log, or whatever. How
54 * can we accomplish this easily? I guess with a map of pre-loved logpaths, and a pointer of FILE *..
58 const char LogStream::LogHeader[] =
59 "Log started for " INSPIRCD_VERSION;
61 LogManager::LogManager()
66 LogManager::~LogManager()
70 void LogManager::OpenFileLogs()
72 if (ServerInstance->Config->cmdline.forcedebug)
74 ServerInstance->Config->RawLog = true;
77 /* Skip rest of logfile opening if we are running -nolog. */
78 if (!ServerInstance->Config->cmdline.writelog)
80 std::map<std::string, FileWriter*> logmap;
81 ConfigTagList tags = ServerInstance->Config->ConfTags("log");
82 for(ConfigIter i = tags.first; i != tags.second; ++i)
84 ConfigTag* tag = i->second;
85 std::string method = tag->getString("method");
86 if (!stdalgo::string::equalsci(method, "file"))
90 std::string type = tag->getString("type");
91 std::string level = tag->getString("level");
92 LogLevel loglevel = LOG_DEFAULT;
93 if (stdalgo::string::equalsci(level, "rawio"))
96 ServerInstance->Config->RawLog = true;
98 else if (stdalgo::string::equalsci(level, "debug"))
100 loglevel = LOG_DEBUG;
102 else if (stdalgo::string::equalsci(level, "verbose"))
104 loglevel = LOG_VERBOSE;
106 else if (stdalgo::string::equalsci(level, "default"))
108 loglevel = LOG_DEFAULT;
110 else if (stdalgo::string::equalsci(level, "sparse"))
112 loglevel = LOG_SPARSE;
114 else if (stdalgo::string::equalsci(level, "none"))
119 std::string target = ServerInstance->Config->Paths.PrependLog(tag->getString("target"));
120 std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
121 if (fwi == logmap.end())
123 char realtarget[256];
124 time_t time = ServerInstance->Time();
125 struct tm *mytime = gmtime(&time);
126 strftime(realtarget, sizeof(realtarget), target.c_str(), mytime);
127 FILE* f = fopen(realtarget, "a");
128 fw = new FileWriter(f, tag->getUInt("flush", 20, 1, UINT_MAX));
129 logmap.insert(std::make_pair(target, fw));
135 FileLogStream* fls = new FileLogStream(loglevel, fw);
136 fls->OnLog(LOG_SPARSE, "HEADER", LogStream::LogHeader);
137 AddLogTypes(type, fls, true);
141 void LogManager::CloseLogs()
143 if (ServerInstance->Config && ServerInstance->Config->cmdline.forcedebug)
147 GlobalLogStreams.clear();
149 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
154 AllLogStreams.clear();
157 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
159 irc::spacesepstream css(types);
161 std::vector<std::string> excludes;
162 while (css.GetToken(tok))
168 if (tok.at(0) == '-')
171 excludes.push_back(tok.substr(1));
175 AddLogType(tok, l, autoclose);
178 // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
179 // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
180 for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
184 /* -* == Exclude all. Why someone would do this, I dunno. */
190 // Now if it's registered as a global, add the exclusions there too.
191 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
192 if (gi != GlobalLogStreams.end())
194 gi->second.swap(excludes); // Swap with the vector in the hash.
198 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
200 LogStreams[type].push_back(l);
203 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
211 void LogManager::DelLogStream(LogStream* l)
213 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
215 while (stdalgo::erase(i->second, l))
217 // Keep erasing while it exists
221 GlobalLogStreams.erase(l);
223 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
224 if (ai == AllLogStreams.end())
230 AllLogStreams.erase(ai);
233 bool LogManager::DelLogType(const std::string &type, LogStream *l)
235 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
238 GlobalLogStreams.erase(l);
241 if (i != LogStreams.end())
243 if (stdalgo::erase(i->second, l))
245 if (i->second.size() == 0)
260 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
261 if (ai == AllLogStreams.end())
266 if ((--ai->second) < 1)
268 AllLogStreams.erase(ai);
275 void LogManager::Log(const std::string &type, LogLevel loglevel, const char *fmt, ...)
281 VAFORMAT(buf, fmt, fmt);
282 this->Log(type, loglevel, buf);
285 void LogManager::Log(const std::string &type, LogLevel loglevel, const std::string &msg)
294 for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
296 if (stdalgo::isin(gi->second, type))
300 gi->first->OnLog(loglevel, type, msg);
303 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
305 if (i != LogStreams.end())
307 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
309 (*it)->OnLog(loglevel, type, msg);
317 FileWriter::FileWriter(FILE* logfile, unsigned int flushcount)
324 void FileWriter::WriteLogLine(const std::string &line)
328 // 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
329 // throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
331 fputs(line.c_str(), log);
332 if (++writeops % flush == 0)
338 FileWriter::~FileWriter()