1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #include "filelogger.h"
19 * Suggested implementation...
21 * bool AddLogType(const std::string &type, enum loglevel, LogStream *)
22 * bool DelLogType(const std::string &type, LogStream *)
23 * Log(const std::string &type, enum loglevel, const std::string &msg)
24 * std::map<std::string, std::vector<LogStream *> > logstreams (holds a 'chain' of logstreams for each type that are all notified when a log happens)
28 * virtual void OnLog(enum loglevel, const std::string &msg)
31 * Modules create their own logstream types (core will create one for 'file logging' for example) and create instances of these logstream types
32 * and register interest in a certain logtype. Globbing is not here, with the exception of * - for all events.. loglevel is used to drop
33 * events that are of no interest to a logstream.
35 * When Log is called, the vector of logstreams for that type is iterated (along with the special vector for "*"), and all registered logstreams
36 * 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.
37 * 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.
39 * NOTE: Somehow we have to let LogManager manage the non-blocking file streams and provide an interface to share them with various LogStreams,
40 * as, for example, a user may want to let 'KILL' and 'XLINE' snotices go to /home/ircd/inspircd/logs/operactions.log, or whatever. How
41 * can we accomplish this easily? I guess with a map of pre-loved logpaths, and a pointer of FILE *..
45 LogManager::LogManager()
50 LogManager::~LogManager()
54 void LogManager::OpenFileLogs()
56 /* Skip rest of logfile opening if we are running -nolog. */
57 if (!ServerInstance->Config->cmdline.writelog)
59 std::map<std::string, FileWriter*> logmap;
60 ConfigTagList tags = ServerInstance->Config->ConfTags("log");
61 for(ConfigIter i = tags.first; i != tags.second; ++i)
63 ConfigTag* tag = i->second;
64 std::string method = tag->getString("method");
69 std::string type = tag->getString("type");
70 std::string level = tag->getString("level");
71 int loglevel = DEFAULT;
76 else if (level == "verbose")
80 else if (level == "default")
84 else if (level == "sparse")
88 else if (level == "none")
93 std::string target = tag->getString("target");
94 std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
95 if (fwi == logmap.end())
97 FILE* f = fopen(target.c_str(), "a");
98 fw = new FileWriter(f);
99 logmap.insert(std::make_pair(target, fw));
105 FileLogStream* fls = new FileLogStream(loglevel, fw);
106 AddLogTypes(type, fls, true);
110 void LogManager::CloseLogs()
112 std::map<std::string, std::vector<LogStream*> >().swap(LogStreams); /* Clear it */
113 std::map<LogStream*, std::vector<std::string> >().swap(GlobalLogStreams); /* Clear it */
114 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
118 std::map<LogStream*, int>().swap(AllLogStreams); /* And clear it */
121 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
123 irc::spacesepstream css(types);
125 std::vector<std::string> excludes;
126 while (css.GetToken(tok))
132 if (tok.at(0) == '-')
135 excludes.push_back(tok.substr(1));
139 AddLogType(tok, l, autoclose);
142 // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
143 // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
144 for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
148 /* -* == Exclude all. Why someone would do this, I dunno. */
154 // Now if it's registered as a global, add the exclusions there too.
155 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
156 if (gi != GlobalLogStreams.end())
158 gi->second.swap(excludes); // Swap with the vector in the hash.
162 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
164 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
166 if (i != LogStreams.end())
168 i->second.push_back(l);
172 std::vector<LogStream *> v;
174 LogStreams[type] = v;
179 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
184 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
185 if (ai == AllLogStreams.end())
187 AllLogStreams.insert(std::make_pair(l, 1));
198 void LogManager::DelLogStream(LogStream* l)
200 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
202 std::vector<LogStream*>::iterator it;
203 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
205 if (it == i->second.end())
210 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
211 if (gi != GlobalLogStreams.end())
213 GlobalLogStreams.erase(gi);
215 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
216 if (ai == AllLogStreams.end())
221 AllLogStreams.erase(ai);
224 bool LogManager::DelLogType(const std::string &type, LogStream *l)
226 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
229 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
230 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
233 if (i != LogStreams.end())
235 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
237 if (it != i->second.end())
240 if (i->second.size() == 0)
255 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
256 if (ai == AllLogStreams.end())
261 if ((--ai->second) < 1)
263 AllLogStreams.erase(ai);
270 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
278 static char buf[65536];
281 vsnprintf(buf, 65536, fmt, a);
284 this->Log(type, loglevel, std::string(buf));
287 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
296 for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
298 if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
302 gi->first->OnLog(loglevel, type, msg);
305 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
307 if (i != LogStreams.end())
309 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
311 (*it)->OnLog(loglevel, type, msg);
319 FileWriter::FileWriter(FILE* logfile)
320 : log(logfile), writeops(0)
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 fprintf(log,"%s",line.c_str());
338 FileWriter::~FileWriter()