1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 if (ServerInstance->Config->cmdline.forcedebug)
58 ServerInstance->Config->RawLog = true;
61 /* Skip rest of logfile opening if we are running -nolog. */
62 if (!ServerInstance->Config->cmdline.writelog)
64 std::map<std::string, FileWriter*> logmap;
65 ConfigTagList tags = ServerInstance->Config->ConfTags("log");
66 for(ConfigIter i = tags.first; i != tags.second; ++i)
68 ConfigTag* tag = i->second;
69 std::string method = tag->getString("method");
74 std::string type = tag->getString("type");
75 std::string level = tag->getString("level");
76 int loglevel = DEFAULT;
80 ServerInstance->Config->RawLog = true;
82 else if (level == "debug")
86 else if (level == "verbose")
90 else if (level == "default")
94 else if (level == "sparse")
98 else if (level == "none")
103 std::string target = tag->getString("target");
104 std::map<std::string, FileWriter*>::iterator fwi = logmap.find(target);
105 if (fwi == logmap.end())
107 char realtarget[MAXBUF];
108 time_t time = ServerInstance->Time();
109 struct tm *mytime = gmtime(&time);
110 strftime(realtarget, MAXBUF, target.c_str(), mytime);
111 FILE* f = fopen(realtarget, "a");
112 fw = new FileWriter(f);
113 logmap.insert(std::make_pair(target, fw));
119 FileLogStream* fls = new FileLogStream(loglevel, fw);
120 fls->OnLog(SPARSE, "HEADER", InspIRCd::LogHeader);
121 AddLogTypes(type, fls, true);
125 void LogManager::CloseLogs()
127 if (ServerInstance->Config && ServerInstance->Config->cmdline.forcedebug)
129 std::map<std::string, std::vector<LogStream*> >().swap(LogStreams); /* Clear it */
130 std::map<LogStream*, std::vector<std::string> >().swap(GlobalLogStreams); /* Clear it */
131 for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
135 std::map<LogStream*, int>().swap(AllLogStreams); /* And clear it */
138 void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
140 irc::spacesepstream css(types);
142 std::vector<std::string> excludes;
143 while (css.GetToken(tok))
149 if (tok.at(0) == '-')
152 excludes.push_back(tok.substr(1));
156 AddLogType(tok, l, autoclose);
159 // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
160 // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
161 for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
165 /* -* == Exclude all. Why someone would do this, I dunno. */
171 // Now if it's registered as a global, add the exclusions there too.
172 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
173 if (gi != GlobalLogStreams.end())
175 gi->second.swap(excludes); // Swap with the vector in the hash.
179 bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)
181 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
183 if (i != LogStreams.end())
185 i->second.push_back(l);
189 std::vector<LogStream *> v;
191 LogStreams[type] = v;
196 GlobalLogStreams.insert(std::make_pair(l, std::vector<std::string>()));
201 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
202 if (ai == AllLogStreams.end())
204 AllLogStreams.insert(std::make_pair(l, 1));
215 void LogManager::DelLogStream(LogStream* l)
217 for (std::map<std::string, std::vector<LogStream*> >::iterator i = LogStreams.begin(); i != LogStreams.end(); ++i)
219 std::vector<LogStream*>::iterator it;
220 while ((it = std::find(i->second.begin(), i->second.end(), l)) != i->second.end())
222 if (it == i->second.end())
227 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
228 if (gi != GlobalLogStreams.end())
230 GlobalLogStreams.erase(gi);
232 std::map<LogStream*, int>::iterator ai = AllLogStreams.begin();
233 if (ai == AllLogStreams.end())
238 AllLogStreams.erase(ai);
241 bool LogManager::DelLogType(const std::string &type, LogStream *l)
243 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
246 std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
247 if (gi != GlobalLogStreams.end()) GlobalLogStreams.erase(gi);
250 if (i != LogStreams.end())
252 std::vector<LogStream *>::iterator it = std::find(i->second.begin(), i->second.end(), l);
254 if (it != i->second.end())
257 if (i->second.size() == 0)
272 std::map<LogStream*, int>::iterator ai = AllLogStreams.find(l);
273 if (ai == AllLogStreams.end())
278 if ((--ai->second) < 1)
280 AllLogStreams.erase(ai);
287 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
295 static char buf[65536];
298 vsnprintf(buf, 65536, fmt, a);
301 this->Log(type, loglevel, std::string(buf));
304 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
313 for (std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.begin(); gi != GlobalLogStreams.end(); ++gi)
315 if (std::find(gi->second.begin(), gi->second.end(), type) != gi->second.end())
319 gi->first->OnLog(loglevel, type, msg);
322 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
324 if (i != LogStreams.end())
326 for (std::vector<LogStream *>::iterator it = i->second.begin(); it != i->second.end(); ++it)
328 (*it)->OnLog(loglevel, type, msg);
336 FileWriter::FileWriter(FILE* logfile)
337 : log(logfile), writeops(0)
341 void FileWriter::WriteLogLine(const std::string &line)
345 // 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
346 // throw CoreException("FileWriter::WriteLogLine called with a closed logfile");
348 fprintf(log,"%s",line.c_str());
355 FileWriter::~FileWriter()