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 it = FileLogs.begin(); it != FileLogs.end(); ++i)
130 FileLogMap().swap(FileLogs); /* Swap with empty map to clear */
133 bool LogManager::AddLogType(const std::string &type, LogStream *l)
135 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
137 if (i != LogStreams.end())
138 i->second.push_back(l);
141 std::vector<LogStream *> v;
143 LogStreams[type] = v;
147 GlobalLogStreams.push_back(l);
152 bool LogManager::DelLogType(const std::string &type, LogStream *l)
154 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
155 std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
157 while (gi != GlobalLogStreams.end())
161 GlobalLogStreams.erase(gi);
166 if (i != LogStreams.end())
168 std::vector<LogStream *>::iterator it = i->second.begin();
170 while (it != i->second.end())
176 if (i->second.size() == 0)
192 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
198 static char buf[65536];
201 vsnprintf(buf, 65536, fmt, a);
204 this->Log(type, loglevel, std::string(buf));
207 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
214 std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
216 while (gi != GlobalLogStreams.end())
218 (*gi)->OnLog(loglevel, type, msg);
222 std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
224 if (i != LogStreams.end())
226 std::vector<LogStream *>::iterator it = i->second.begin();
228 while (it != i->second.end())
230 (*it)->OnLog(loglevel, type, msg);
239 FileWriter::FileWriter(InspIRCd* Instance, FILE* logfile)
240 : ServerInstance(Instance), log(logfile), writeops(0)
244 Instance->SE->NonBlocking(fileno(log));
250 bool FileWriter::Readable()
255 void FileWriter::HandleEvent(EventType, int)
259 ServerInstance->SE->DelFd(this);
262 void FileWriter::WriteLogLine(const std::string &line)
269 int written = fprintf(log,"%s",buffer.c_str());
273 if ((written >= 0) && (written < (int)buffer.length()))
275 buffer.erase(0, buffer.length());
276 ServerInstance->SE->AddFd(this);
278 else if (written == -1)
281 ServerInstance->SE->AddFd(this);
285 /* Wrote the whole buffer, and no need for write callback */
296 void FileWriter::Close()
300 ServerInstance->SE->Blocking(fileno(log));
303 fprintf(log,"%s",buffer.c_str());
306 ServerInstance->SE->DelFd(this);
316 FileWriter::~FileWriter()