2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
5 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /** Levels at which messages can be logged. */
34 /** Simple wrapper providing periodic flushing to a disk-backed file.
36 class CoreExport FileWriter
39 /** The log file (fd is inside this somewhere,
40 * we get it out with fileno())
44 /** The number of write operations after which we should flush.
48 /** Number of write operations that have occured
50 unsigned int writeops;
53 /** The constructor takes an already opened logfile.
55 FileWriter(FILE* logfile, unsigned int flushcount);
57 /** Write one or more preformatted log lines.
58 * If the data cannot be written immediately,
59 * this class will insert itself into the
60 * SocketEngine, and register a write event,
61 * and when the write event occurs it will
62 * attempt again to write the data.
64 void WriteLogLine(const std::string &line);
66 /** Close the log file and cancel any events.
68 virtual ~FileWriter();
76 * Logging used to be a simple affair, a FILE * handled by a nonblocking logging class inheriting from EventHandler, that was inserted
77 * into the socket engine, and wrote lines. If nofork was on, it was printf()'d.
79 * We decided to horribly overcomplicate matters, and create vastly customisable logging. LogManager and LogStream form the visible basis
80 * of the new interface. Basically, a LogStream can be inherited to do different things with logging output. We inherit from it once in core
81 * to create a FileLogStream, that writes to a file, for example. Different LogStreams can hook different types of log messages, and different
82 * levels of output too, for extreme customisation. Multiple LogStreams can hook the same message/levels of output, meaning that e.g. output
83 * can go to a channel as well as a file.
86 * LogManager handles all instances of LogStreams, classes derived from LogStream are instantiated and passed to it.
89 /** LogStream base class. Modules (and other stuff) inherit from this to decide what logging they are interested in, and what to do with it.
91 class CoreExport LogStream : public classbase
96 static const char LogHeader[];
98 LogStream(LogLevel loglevel) : loglvl(loglevel)
102 /* A LogStream's destructor should do whatever it needs to close any resources it was using (or indicate that it is no longer using a resource
103 * in the event that the resource is shared, see for example FileLogStream).
105 virtual ~LogStream() { }
107 /** Changes the loglevel for this LogStream on-the-fly.
108 * This is needed for -nofork. But other LogStreams could use it to change loglevels.
110 void ChangeLevel(LogLevel lvl) { this->loglvl = lvl; }
112 /** Called when there is stuff to log for this particular logstream. The derived class may take no action with it, or do what it
113 * wants with the output, basically. loglevel and type are primarily for informational purposes (the level and type of the event triggered)
114 * and msg is, of course, the actual message to log.
116 virtual void OnLog(LogLevel loglevel, const std::string &type, const std::string &msg) = 0;
119 typedef std::map<FileWriter*, int> FileLogMap;
121 class CoreExport LogManager : public fakederef<LogManager>
124 /** Lock variable, set to true when a log is in progress, which prevents further loggging from happening and creating a loop.
128 /** Map of active log types and what LogStreams will receive them.
130 std::map<std::string, std::vector<LogStream *> > LogStreams;
132 /** Refcount map of all LogStreams managed by LogManager.
133 * If a logstream is not listed here, it won't be automatically closed by LogManager, even if it's loaded in one of the other lists.
135 std::map<LogStream *, int> AllLogStreams;
137 /** LogStreams with type * (which means everything), and a list a logtypes they are excluded from (eg for "* -USERINPUT -USEROUTPUT").
139 std::map<LogStream *, std::vector<std::string> > GlobalLogStreams;
141 /** Refcounted map of all FileWriters in use by FileLogStreams, for file stream sharing.
149 /** Adds a FileWriter instance to LogManager, or increments the reference count of an existing instance.
150 * Used for file-stream sharing for FileLogStreams.
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 /** Indicates that a FileWriter reference has been removed. Reference count is decreased, and if zeroed, the FileWriter is closed.
167 void DelLoggerRef(FileWriter* fw)
169 FileLogMap::iterator i = FileLogs.find(fw);
170 if (i == FileLogs.end()) return; /* Maybe should log this? */
178 /** Opens all logfiles defined in the configuration file using \<log method="file">.
182 /** Removes all LogStreams, meaning they have to be readded for logging to continue.
183 * Only LogStreams that were listed in AllLogStreams are actually closed.
187 /** Adds a single LogStream to multiple logtypes.
188 * This automatically handles things like "* -USERINPUT -USEROUTPUT" to mean all but USERINPUT and USEROUTPUT types.
189 * It is not a good idea to mix values of autoclose for the same LogStream.
190 * @param type The type string (from configuration, or whatever) to parse.
191 * @param l The LogStream to add.
192 * @param autoclose True to have the LogStream automatically closed when all references to it are removed from LogManager. False to leave it open.
194 void AddLogTypes(const std::string &type, LogStream *l, bool autoclose);
196 /** Registers a new logstream into the logging core, so it can be called for future events
197 * It is not a good idea to mix values of autoclose for the same LogStream.
198 * @param type The type to add this LogStream to.
199 * @param l The LogStream to add.
200 * @param autoclose True to have the LogStream automatically closed when all references to it are removed from LogManager. False to leave it open.
201 * @return True if the LogStream was added successfully, False otherwise.
203 bool AddLogType(const std::string &type, LogStream *l, bool autoclose);
205 /** Removes a logstream from the core. After removal, it will not recieve further events.
206 * If the LogStream was ever added with autoclose, it will be closed after this call (this means the pointer won't be valid anymore).
208 void DelLogStream(LogStream* l);
210 /** Removes a LogStream from a single type. If the LogStream has been registered for "*" it will still receive the type unless you remove it from "*" specifically.
211 * If the LogStream was added with autoclose set to true, then when the last occurrence of the stream is removed it will automatically be closed (freed).
213 bool DelLogType(const std::string &type, LogStream *l);
215 /** Logs an event, sending it to all LogStreams registered for the type.
216 * @param type Log message type (ex: "USERINPUT", "MODULE", ...)
217 * @param loglevel Log message level (LOG_DEBUG, LOG_VERBOSE, LOG_DEFAULT, LOG_SPARSE, LOG_NONE)
218 * @param msg The message to be logged (literal).
220 void Log(const std::string &type, LogLevel loglevel, const std::string &msg);
222 /** Logs an event, sending it to all LogStreams registered for the type.
223 * @param type Log message type (ex: "USERINPUT", "MODULE", ...)
224 * @param loglevel Log message level (LOG_DEBUG, LOG_VERBOSE, LOG_DEFAULT, LOG_SPARSE, LOG_NONE)
225 * @param fmt The format of the message to be logged. See your C manual on printf() for details.
227 void Log(const std::string &type, LogLevel loglevel, const char *fmt, ...) CUSTOM_PRINTF(4, 5);