1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $Core: libIRCDlogger */
18 #include "filelogger.h"
21 * Suggested implementation...
23 * bool AddLogType(const std::string &type, enum loglevel, LogStream *)
24 * bool DelLogType(const std::string &type, LogStream *)
25 * Log(const std::string &type, enum loglevel, const std::string &msg)
26 * std::map<std::string, std::vector<LogStream *> > logstreams (holds a 'chain' of logstreams for each type that are all notified when a log happens)
30 * virtual void OnLog(enum loglevel, const std::string &msg)
33 * Modules create their own logstream types (core will create one for 'file logging' for example) and create instances of these logstream types
34 * and register interest in a certain logtype. Globbing is not here, with the exception of * - for all events.. loglevel is used to drop
35 * events that are of no interest to a logstream.
37 * When Log is called, the vector of logstreams for that type is iterated (along with the special vector for "*"), and all registered logstreams
38 * 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.
39 * 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.
41 * NOTE: Somehow we have to let LogManager manage the non-blocking file streams and provide an interface to share them with various LogStreams,
42 * as, for example, a user may want to let 'KILL' and 'XLINE' snotices go to /home/ircd/inspircd/logs/operactions.log, or whatever. How
43 * can we accomplish this easily? I guess with a map of pre-loved logpaths, and a pointer of FILE *..
47 void LogManager::SetupNoFork()
51 FileWriter* fw = new FileWriter(ServerInstance, stdout);
52 noforkstream = new FileLogStream(ServerInstance, ServerInstance->Config->forcedebug ? DEBUG : ServerInstance->Config->LogLevel, fw);
56 noforkstream->ChangeLevel(ServerInstance->Config->forcedebug ? DEBUG : ServerInstance->Config->LogLevel);
58 AddLogType("*", noforkstream, false);
61 void LogManager::OpenFileLogs()
63 /* Re-register the nofork stream if necessary. */
64 if (ServerInstance->Config->nofork)
68 /* Skip rest of logfile opening if we are running -nolog. */
69 if (!ServerInstance->Config->writelog)
73 ConfigReader* Conf = new ConfigReader(ServerInstance);
74 std::map<std::string, FileWriter*> logmap;
75 std::map<std::string, FileWriter*>::iterator i;
76 for (int index = 0; index < Conf->Enumerate("log"); ++index)
78 std::string method = Conf->ReadValue("log", "method", index);
83 std::string type = Conf->ReadValue("log", "type", index);
84 std::string level = Conf->ReadValue("log", "level", index);
85 int loglevel = DEFAULT;
86 if (level == "debug" || ServerInstance->Config->forcedebug)
89 ServerInstance->Config->debugging = true;
91 else if (level == "verbose")
95 else if (level == "default")
99 else if (level == "sparse")
103 else if (level == "none")
108 std::string target = Conf->ReadValue("log", "target", index);
109 if ((i = logmap.find(target)) == logmap.end())
111 FILE* f = fopen(target.c_str(), "a");
112 fw = new FileWriter(ServerInstance, f);
113 logmap.insert(std::make_pair(target, fw));
119 FileLogStream* fls = new FileLogStream(ServerInstance, loglevel, fw);
120 AddLogTypes(type, fls, true);
124 void LogManager::CloseLogs()
126 std::map<std::string, std::vector<LogStream*> >().swap(LogStreams); /* Clear it */
127 std::map<LogStream*, std::vector<std::string> >().swap(GlobalLogStreams); /* Clear it */
128 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
132 std::map<LogStream*, int>().swap(AllLogStreams); /* And clear it */
135 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
137 irc::spacesepstream css(types);
139 std::vector<std::string> excludes;
140 while (css.GetToken(tok))
146 if (tok.at(0) == '-')
149 excludes.push_back(tok.substr(1));
153 AddLogType(tok, l, autoclose);
156 // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
157 // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
158 for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
162 /* -* == Exclude all. Why someone would do this, I dunno. */
168 // Now if it's registered as a global, add the exclusions there too.
169 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
170 if (gi != GlobalLogStreams.end())
172 gi->second.swap(excludes); // Swap with the vector in the hash.
176 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
178 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
180 if (i != LogStreams.end())
182 i->second.push_back(l);
186 std::vector<LogStream *> v;
188 LogStreams[type] = v;
193 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
198 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
199 if (ai == AllLogStreams.end())
201 AllLogStreams.insert(std::make_pair(l, 1));
212 void LogManager::DelLogStream(LogStream* l)
214 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
216 std::vector<LogStream*>::iterator it;
217 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
219 if (it == i->second.end())
224 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
225 if (gi != GlobalLogStreams.end())
227 GlobalLogStreams.erase(gi);
229 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
230 if (ai == AllLogStreams.end())
235 AllLogStreams.erase(ai);
238 bool LogManager::DelLogType(const std::string &type, LogStream *l)
240 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
243 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
244 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
247 if (i != LogStreams.end())
249 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
251 if (it != i->second.end())
254 if (i->second.size() == 0)
269 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
270 if (ai == AllLogStreams.end())
275 if ((--ai->second) < 1)
277 AllLogStreams.erase(ai);
284 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
292 static char buf[65536];
295 vsnprintf(buf, 65536, fmt, a);
298 this->Log(type, loglevel, std::string(buf));
301 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
310 for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
312 if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
316 gi->first->OnLog(loglevel, type, msg);
319 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
321 if (i != LogStreams.end())
323 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
325 (*it)->OnLog(loglevel, type, msg);
333 FileWriter::FileWriter(InspIRCd* Instance, FILE* logfile)
334 : ServerInstance(Instance), log(logfile), writeops(0)
338 Instance->SE->NonBlocking(fileno(log));
344 bool FileWriter::Readable()
349 void FileWriter::HandleEvent(EventType, int)
354 ServerInstance->SE->DelFd(this);
358 void FileWriter::WriteLogLine(const std::string &line)
367 int written = fprintf(log,"%s",buffer.c_str());
371 if ((written >= 0) && (written < (int)buffer.length()))
373 buffer.erase(0, buffer.length());
374 ServerInstance->SE->AddFd(this);
376 else if (written == -1)
379 ServerInstance->SE->AddFd(this);
383 /* Wrote the whole buffer, and no need for write callback */
394 void FileWriter::Close()
398 ServerInstance->SE->Blocking(fileno(log));
402 fprintf(log,"%s",buffer.c_str());
406 ServerInstance->SE->DelFd(this);
416 FileWriter::~FileWriter()