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::OpenFileLogs()
49 ConfigReader* Conf = new ConfigReader(ServerInstance);
50 std::map<std::string, FileWriter*> logmap;
51 std::map<std::string, FileWriter*>::iterator i;
52 for (int index = 0; index < Conf->Enumerate("log"); ++index)
54 std::string method = Conf->ReadValue("log", "method", index);
55 if (method != "file") continue;
56 std::string type = Conf->ReadValue("log", "type", index);
57 std::string level = Conf->ReadValue("log", "level", index);
58 int loglevel = DEFAULT;
62 ServerInstance->Config->debugging = true;
64 else if (level == "verbose")
68 else if (level == "default")
72 else if (level == "sparse")
76 else if (level == "none")
81 std::string target = Conf->ReadValue("log", "target", index);
82 if ((i = logmap.find(target)) == logmap.end())
84 FILE* f = fopen(target.c_str(), "a");
85 fw = new FileWriter(ServerInstance, f);
86 logmap.insert(std::make_pair(target, fw));
92 FileLogStream* fls = new FileLogStream(ServerInstance, loglevel, fw);
93 irc::commasepstream css(type);
95 while (css.GetToken(tok))
102 void LogManager::CloseLogs()
105 * This doesn't remove logstreams from the map/vector etc, because if this is called, shit is hitting the fan
106 * and we're going down anyway - this just provides a "nice" way for logstreams to clean up. -- w
108 std::map<std::string, std::vector<LogStream *> >::iterator i;
110 while (LogStreams.begin() != LogStreams.end())
112 i = LogStreams.begin();
114 while (i->second.begin() != i->second.end())
116 std::vector<LogStream *>::iterator it = i->second.begin();
124 /* Now close FileLoggers, for those logstreams that neglected to properly free their stuff. */
125 for (FileLogMap::iterator i = FileLogs.begin(); i != FileLogs.end(); ++i)
129 FileLogMap().swap(FileLogs); /* Swap with empty map to clear */
132 bool LogManager::AddLogType(const std::string &type, LogStream *l)
134 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
136 if (i != LogStreams.end())
137 i->second.push_back(l);
140 std::vector<LogStream *> v;
142 LogStreams[type] = v;
146 GlobalLogStreams.push_back(l);
151 bool LogManager::DelLogType(const std::string &type, LogStream *l)
153 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
154 std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
156 while (gi != GlobalLogStreams.end())
160 GlobalLogStreams.erase(gi);
165 if (i != LogStreams.end())
167 std::vector<LogStream *>::iterator it = i->second.begin();
169 while (it != i->second.end())
175 if (i->second.size() == 0)
191 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
197 static char buf[65536];
200 vsnprintf(buf, 65536, fmt, a);
203 this->Log(type, loglevel, std::string(buf));
206 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
213 std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
215 while (gi != GlobalLogStreams.end())
217 (*gi)->OnLog(loglevel, type, msg);
221 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
223 if (i != LogStreams.end())
225 std::vector<LogStream *>::iterator it = i->second.begin();
227 while (it != i->second.end())
229 (*it)->OnLog(loglevel, type, msg);
238 FileWriter::FileWriter(InspIRCd* Instance, FILE* logfile)
239 : ServerInstance(Instance), log(logfile), writeops(0)
243 Instance->SE->NonBlocking(fileno(log));
249 bool FileWriter::Readable()
254 void FileWriter::HandleEvent(EventType, int)
258 ServerInstance->SE->DelFd(this);
261 void FileWriter::WriteLogLine(const std::string &line)
268 int written = fprintf(log,"%s",buffer.c_str());
272 if ((written >= 0) && (written < (int)buffer.length()))
274 buffer.erase(0, buffer.length());
275 ServerInstance->SE->AddFd(this);
277 else if (written == -1)
280 ServerInstance->SE->AddFd(this);
284 /* Wrote the whole buffer, and no need for write callback */
295 void FileWriter::Close()
299 ServerInstance->SE->Blocking(fileno(log));
302 fprintf(log,"%s",buffer.c_str());
305 ServerInstance->SE->DelFd(this);
315 FileWriter::~FileWriter()