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()
104 std::map<std::string, std::vector<LogStream*> >().swap(LogStreams); /* Clear it */
105 std::vector<LogStream*>().swap(GlobalLogStreams); /* Clear it */
106 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
110 std::map<LogStream*, int>().swap(AllLogStreams); /* And clear it */
112 /* Now close FileLoggers, for those logstreams that neglected to properly free their stuff. */
113 for (FileLogMap::iterator it = FileLogs.begin(); it != FileLogs.end(); ++it)
118 FileLogMap().swap(FileLogs);
121 bool LogManager::AddLogType(const std::string &type, LogStream *l)
123 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
125 if (i != LogStreams.end())
126 i->second.push_back(l);
129 std::vector<LogStream *> v;
131 LogStreams[type] = v;
135 GlobalLogStreams.push_back(l);
137 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
138 if (ai == AllLogStreams.end())
140 AllLogStreams.insert(std::make_pair(l, 1));
150 void LogManager::DelLogStream(LogStream* l)
152 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
154 std::vector<LogStream*>::iterator it;
155 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
157 if (it == i->second.end()) continue;
161 std::vector<LogStream *>::iterator gi = std::find(GlobalLogStreams.begin(), GlobalLogStreams.end(), l);
162 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
163 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
164 if (ai == AllLogStreams.end()) return; /* Done. */
166 AllLogStreams.erase(ai);
169 bool LogManager::DelLogType(const std::string &type, LogStream *l)
171 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
174 std::vector<LogStream *>::iterator gi = std::find(GlobalLogStreams.begin(), GlobalLogStreams.end(), l);
175 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
178 if (i != LogStreams.end())
180 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
182 if (it != i->second.end())
185 if (i->second.size() == 0)
200 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
201 if (ai == AllLogStreams.end()) return true;
203 if ((--ai->second) < 1)
205 AllLogStreams.erase(ai);
212 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
218 static char buf[65536];
221 vsnprintf(buf, 65536, fmt, a);
224 this->Log(type, loglevel, std::string(buf));
227 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
234 std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
236 while (gi != GlobalLogStreams.end())
238 (*gi)->OnLog(loglevel, type, msg);
242 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
244 if (i != LogStreams.end())
246 std::vector<LogStream *>::iterator it = i->second.begin();
248 while (it != i->second.end())
250 (*it)->OnLog(loglevel, type, msg);
259 FileWriter::FileWriter(InspIRCd* Instance, FILE* logfile)
260 : ServerInstance(Instance), log(logfile), writeops(0)
264 Instance->SE->NonBlocking(fileno(log));
270 bool FileWriter::Readable()
275 void FileWriter::HandleEvent(EventType, int)
279 ServerInstance->SE->DelFd(this);
282 void FileWriter::WriteLogLine(const std::string &line)
289 int written = fprintf(log,"%s",buffer.c_str());
293 if ((written >= 0) && (written < (int)buffer.length()))
295 buffer.erase(0, buffer.length());
296 ServerInstance->SE->AddFd(this);
298 else if (written == -1)
301 ServerInstance->SE->AddFd(this);
305 /* Wrote the whole buffer, and no need for write callback */
316 void FileWriter::Close()
320 ServerInstance->SE->Blocking(fileno(log));
323 fprintf(log,"%s",buffer.c_str());
326 ServerInstance->SE->DelFd(this);
336 FileWriter::~FileWriter()