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 if (ServerInstance->Config->nofork) SetupNoFork(); // Call this to reregister the nofork stream.
64 if (!ServerInstance->Config->writelog) return; // Skip rest of logfile opening if we are running -nolog.
65 ConfigReader* Conf = new ConfigReader(ServerInstance);
66 std::map<std::string, FileWriter*> logmap;
67 std::map<std::string, FileWriter*>::iterator i;
68 for (int index = 0; index < Conf->Enumerate("log"); ++index)
70 std::string method = Conf->ReadValue("log", "method", index);
71 if (method != "file") continue;
72 std::string type = Conf->ReadValue("log", "type", index);
73 std::string level = Conf->ReadValue("log", "level", index);
74 int loglevel = DEFAULT;
75 if (level == "debug" || ServerInstance->Config->forcedebug)
78 ServerInstance->Config->debugging = true;
80 else if (level == "verbose")
84 else if (level == "default")
88 else if (level == "sparse")
92 else if (level == "none")
97 std::string target = Conf->ReadValue("log", "target", index);
98 if ((i = logmap.find(target)) == logmap.end())
100 FILE* f = fopen(target.c_str(), "a");
101 fw = new FileWriter(ServerInstance, f);
102 logmap.insert(std::make_pair(target, fw));
108 FileLogStream* fls = new FileLogStream(ServerInstance, loglevel, fw);
109 irc::spacesepstream css(type);
111 while (css.GetToken(tok))
113 AddLogType(tok, fls, true);
118 void LogManager::CloseLogs()
120 std::map<std::string, std::vector<LogStream*> >().swap(LogStreams); /* Clear it */
121 std::vector<LogStream*>().swap(GlobalLogStreams); /* Clear it */
122 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
126 std::map<LogStream*, int>().swap(AllLogStreams); /* And clear it */
129 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
131 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
133 if (i != LogStreams.end())
134 i->second.push_back(l);
137 std::vector<LogStream *> v;
139 LogStreams[type] = v;
143 GlobalLogStreams.push_back(l);
147 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
148 if (ai == AllLogStreams.end())
150 AllLogStreams.insert(std::make_pair(l, 1));
161 void LogManager::DelLogStream(LogStream* l)
163 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
165 std::vector<LogStream*>::iterator it;
166 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
168 if (it == i->second.end()) continue;
172 std::vector<LogStream *>::iterator gi = std::find(GlobalLogStreams.begin(), GlobalLogStreams.end(), l);
173 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
174 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
175 if (ai == AllLogStreams.end()) return; /* Done. */
177 AllLogStreams.erase(ai);
180 bool LogManager::DelLogType(const std::string &type, LogStream *l)
182 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
185 std::vector<LogStream *>::iterator gi = std::find(GlobalLogStreams.begin(), GlobalLogStreams.end(), l);
186 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
189 if (i != LogStreams.end())
191 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
193 if (it != i->second.end())
196 if (i->second.size() == 0)
211 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
212 if (ai == AllLogStreams.end()) return true;
214 if ((--ai->second) < 1)
216 AllLogStreams.erase(ai);
223 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
229 static char buf[65536];
232 vsnprintf(buf, 65536, fmt, a);
235 this->Log(type, loglevel, std::string(buf));
238 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
245 std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
247 while (gi != GlobalLogStreams.end())
249 (*gi)->OnLog(loglevel, type, msg);
253 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
255 if (i != LogStreams.end())
257 std::vector<LogStream *>::iterator it = i->second.begin();
259 while (it != i->second.end())
261 (*it)->OnLog(loglevel, type, msg);
270 FileWriter::FileWriter(InspIRCd* Instance, FILE* logfile)
271 : ServerInstance(Instance), log(logfile), writeops(0)
275 Instance->SE->NonBlocking(fileno(log));
281 bool FileWriter::Readable()
286 void FileWriter::HandleEvent(EventType, int)
290 ServerInstance->SE->DelFd(this);
293 void FileWriter::WriteLogLine(const std::string &line)
300 int written = fprintf(log,"%s",buffer.c_str());
304 if ((written >= 0) && (written < (int)buffer.length()))
306 buffer.erase(0, buffer.length());
307 ServerInstance->SE->AddFd(this);
309 else if (written == -1)
312 ServerInstance->SE->AddFd(this);
316 /* Wrote the whole buffer, and no need for write callback */
327 void FileWriter::Close()
331 ServerInstance->SE->Blocking(fileno(log));
334 fprintf(log,"%s",buffer.c_str());
337 ServerInstance->SE->DelFd(this);
347 FileWriter::~FileWriter()