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 #ifndef __LOGMANAGER_H
15 #define __LOGMANAGER_H
17 /** This class implements a nonblocking writer.
18 * Most people writing an ircd give little thought to their disk
19 * i/o. On a congested system, disk writes can block for long
20 * periods of time (e.g. if the system is busy and/or swapping
21 * a lot). If we just use a blocking fprintf() call, this could
22 * block for undesirable amounts of time (half of a second through
23 * to whole seconds). We DO NOT want this, so we make our logfile
24 * nonblocking and hook it into the SocketEngine.
25 * NB: If the operating system does not support nonblocking file
26 * I/O (linux seems to, as does freebsd) this will default to
29 class CoreExport FileWriter : public EventHandler
32 /** The creator/owner of this object
34 InspIRCd* ServerInstance;
36 /** The log file (fd is inside this somewhere,
37 * we get it out with fileno())
41 /** Buffer of pending log lines to be written
45 /** Number of write operations that have occured
50 /** The constructor takes an already opened logfile.
52 FileWriter(InspIRCd* Instance, FILE* logfile);
54 /** This returns false, logfiles are writeable.
56 virtual bool Readable();
58 /** Handle pending write events.
59 * This will flush any waiting data to disk.
60 * If any data remains after the fprintf call,
61 * another write event is scheduled to write
62 * the rest of the data when possible.
64 virtual void HandleEvent(EventType et, int errornum = 0);
66 /** Write one or more preformatted log lines.
67 * If the data cannot be written immediately,
68 * this class will insert itself into the
69 * SocketEngine, and register a write event,
70 * and when the write event occurs it will
71 * attempt again to write the data.
73 void WriteLogLine(const std::string &line);
75 /** Close the log file and cancel any events.
79 /** Close the log file and cancel any events.
80 * (indirectly call Close()
82 virtual ~FileWriter();
90 * Logging used to be a simple affair, a FILE * handled by a nonblocking logging class inheriting from EventHandler, that was inserted
91 * into the socket engine, and wrote lines. If nofork was on, it was printf()'d.
93 * We decided to horribly overcomplicate matters, and create vastly customisable logging. LogManager and LogStream form the visible basis
94 * of the new interface. Basically, a LogStream can be inherited to do different things with logging output. We inherit from it once in core
95 * to create a FileLogStream, that writes to a file, for example. Different LogStreams can hook different types of log messages, and different
96 * levels of output too, for extreme customisation. Multiple LogStreams can hook the same message/levels of output, meaning that e.g. output
97 * can go to a channel as well as a file.
100 * LogManager handles all instances of LogStreams, LogStreams (or more likely, derived classes) are instantiated and passed to it.
103 /** LogStream base class. Modules (and other stuff) inherit from this to decide what logging they are interested in, and what to do with it.
105 class CoreExport LogStream : public classbase
108 InspIRCd *ServerInstance;
111 LogStream(InspIRCd *Instance, int loglevel) : loglvl(loglevel)
113 this->ServerInstance = Instance;
116 virtual ~LogStream() { }
118 /** XXX document me properly.
119 * Used for on the fly changing of loglevel.
121 void ChangeLevel(int lvl) { this->loglvl = lvl; }
123 /** Called when there is stuff to log for this particular logstream. The derived class may take no action with it, or do what it
124 * wants with the output, basically. loglevel and type are primarily for informational purposes (the level and type of the event triggered)
125 * and msg is, of course, the actual message to log.
127 virtual void OnLog(int loglevel, const std::string &type, const std::string &msg) = 0;
130 typedef std::map<FileWriter*, int> FileLogMap;
132 class CoreExport LogManager : public classbase
135 bool Logging; // true when logging, avoids recursion
136 LogStream* noforkstream; // LogStream for nofork.
137 InspIRCd *ServerInstance;
138 std::map<std::string, std::vector<LogStream *> > LogStreams;
139 std::map<LogStream *, int> AllLogStreams; // holds all logstreams
140 std::vector<LogStream *> GlobalLogStreams; //holds all logstreams with a type of *
141 FileLogMap FileLogs; // Holds all file logs, refcounted
143 LogManager(InspIRCd *Instance)
145 ServerInstance = Instance;
151 /** XXX document me properly. */
152 void AddLoggerRef(FileWriter* fw)
154 FileLogMap::iterator i = FileLogs.find(fw);
155 if (i == FileLogs.end())
157 FileLogs.insert(std::make_pair(fw, 1));
165 /** XXX document me properly. */
166 void DelLoggerRef(FileWriter* fw)
168 FileLogMap::iterator i = FileLogs.find(fw);
169 if (i == FileLogs.end()) return; /* Maybe should log this? */
177 /** XXX document me properly. */
178 void OpenSingleFile(FILE* f, const std::string& type, int loglevel);
180 /** XXX document me properly. */
183 /** Gives all logstreams a chance to clear up (in destructors) while it deletes them.
187 /** Registers a new logstream into the logging core, so it can be called for future events
188 * XXX document me properly.
190 bool AddLogType(const std::string &type, LogStream *l, bool autoclose);
192 /** Removes a logstream from the core. After removal, it will not recieve further events.
194 void DelLogStream(LogStream* l);
196 /** XXX document me properly. */
197 bool DelLogType(const std::string &type, LogStream *l);
199 /** Pretty self explanatory.
201 void Log(const std::string &type, int loglevel, const std::string &msg);
205 void Log(const std::string &type, int loglevel, const char *fmt, ...);