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 void LogManager::SetupNoFork()
49 FileWriter* fw = new FileWriter(stdout);
50 noforkstream = new FileLogStream(ServerInstance->Config->forcedebug ? DEBUG : DEFAULT, fw);
54 noforkstream->ChangeLevel(ServerInstance->Config->forcedebug ? DEBUG : DEFAULT);
56 AddLogType("*", noforkstream, false);
59 void LogManager::OpenFileLogs()
61 /* Re-register the nofork stream if necessary. */
62 if (ServerInstance->Config->nofork)
66 /* Skip rest of logfile opening if we are running -nolog. */
67 if (!ServerInstance->Config->writelog)
72 std::map<std::string, FileWriter*> logmap;
73 std::map<std::string, FileWriter*>::iterator i;
74 for (int index = 0;; ++index)
76 ConfigTag* tag = ServerInstance->Config->ConfValue("log", index);
79 std::string method = tag->getString("method");
84 std::string type = tag->getString("type");
85 std::string level = tag->getString("level");
86 int loglevel = DEFAULT;
87 if (level == "debug" || ServerInstance->Config->forcedebug)
90 ServerInstance->Config->debugging = true;
92 else if (level == "verbose")
96 else if (level == "default")
100 else if (level == "sparse")
104 else if (level == "none")
109 std::string target = Conf.ReadValue("log", "target", index);
110 if ((i = logmap.find(target)) == logmap.end())
112 FILE* f = fopen(target.c_str(), "a");
113 fw = new FileWriter(f);
114 logmap.insert(std::make_pair(target, fw));
120 FileLogStream* fls = new FileLogStream(loglevel, fw);
121 AddLogTypes(type, fls, true);
125 void LogManager::CloseLogs()
127 std::map<std::string, std::vector<LogStream*> >().swap(LogStreams); /* Clear it */
128 std::map<LogStream*, std::vector<std::string> >().swap(GlobalLogStreams); /* Clear it */
129 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
133 std::map<LogStream*, int>().swap(AllLogStreams); /* And clear it */
136 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
138 irc::spacesepstream css(types);
140 std::vector<std::string> excludes;
141 while (css.GetToken(tok))
147 if (tok.at(0) == '-')
150 excludes.push_back(tok.substr(1));
154 AddLogType(tok, l, autoclose);
157 // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
158 // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
159 for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
163 /* -* == Exclude all. Why someone would do this, I dunno. */
169 // Now if it's registered as a global, add the exclusions there too.
170 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
171 if (gi != GlobalLogStreams.end())
173 gi->second.swap(excludes); // Swap with the vector in the hash.
177 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
179 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
181 if (i != LogStreams.end())
183 i->second.push_back(l);
187 std::vector<LogStream *> v;
189 LogStreams[type] = v;
194 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
199 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
200 if (ai == AllLogStreams.end())
202 AllLogStreams.insert(std::make_pair(l, 1));
213 void LogManager::DelLogStream(LogStream* l)
215 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
217 std::vector<LogStream*>::iterator it;
218 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
220 if (it == i->second.end())
225 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
226 if (gi != GlobalLogStreams.end())
228 GlobalLogStreams.erase(gi);
230 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
231 if (ai == AllLogStreams.end())
236 AllLogStreams.erase(ai);
239 bool LogManager::DelLogType(const std::string &type, LogStream *l)
241 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
244 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
245 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
248 if (i != LogStreams.end())
250 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
252 if (it != i->second.end())
255 if (i->second.size() == 0)
270 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
271 if (ai == AllLogStreams.end())
276 if ((--ai->second) < 1)
278 AllLogStreams.erase(ai);
285 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
293 static char buf[65536];
296 vsnprintf(buf, 65536, fmt, a);
299 this->Log(type, loglevel, std::string(buf));
302 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
311 for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
313 if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
317 gi->first->OnLog(loglevel, type, msg);
320 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
322 if (i != LogStreams.end())
324 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
326 (*it)->OnLog(loglevel, type, msg);
334 FileWriter::FileWriter(FILE* logfile)
335 : log(logfile), writeops(0)
339 void FileWriter::HandleEvent(EventType ev, int)
343 void FileWriter::WriteLogLine(const std::string &line)
347 // 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
348 // throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
350 fprintf(log,"%s",line.c_str());
357 void FileWriter::Close()
367 FileWriter::~FileWriter()