]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/logger.h
Add an oper only parameter to Simple{Channel,User}ModeHandler.
[user/henk/code/inspircd.git] / include / logger.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20
21 #pragma once
22
23 /** Levels at which messages can be logged. */
24 enum LogLevel
25 {
26         LOG_RAWIO   = 5,
27         LOG_DEBUG   = 10,
28         LOG_VERBOSE = 20,
29         LOG_DEFAULT = 30,
30         LOG_SPARSE  = 40,
31         LOG_NONE    = 50
32 };
33
34 /** Simple wrapper providing periodic flushing to a disk-backed file.
35  */
36 class CoreExport FileWriter
37 {
38  protected:
39         /** The log file (fd is inside this somewhere,
40          * we get it out with fileno())
41          */
42         FILE* log;
43
44         /** The number of write operations after which we should flush.
45          */
46         unsigned int flush;
47
48         /** Number of write operations that have occured
49          */
50         unsigned int writeops;
51
52  public:
53         /** The constructor takes an already opened logfile.
54          */
55         FileWriter(FILE* logfile, unsigned int flushcount);
56
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.
63          */
64         void WriteLogLine(const std::string &line);
65
66         /** Close the log file and cancel any events.
67          */
68         virtual ~FileWriter();
69 };
70
71
72
73 /*
74  * New world logging!
75  * The brief summary:
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.
78  *
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.
84  *
85  *  HOW THIS WORKS
86  *   LogManager handles all instances of LogStreams, classes derived from LogStream are instantiated and passed to it.
87  */
88
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.
90  */
91 class CoreExport LogStream : public classbase
92 {
93  protected:
94         LogLevel loglvl;
95  public:
96         static const char LogHeader[];
97
98         LogStream(LogLevel loglevel) : loglvl(loglevel)
99         {
100         }
101
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).
104          */
105         virtual ~LogStream() { }
106
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.
109          */
110         void ChangeLevel(LogLevel lvl) { this->loglvl = lvl; }
111
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.
115          */
116         virtual void OnLog(LogLevel loglevel, const std::string &type, const std::string &msg) = 0;
117 };
118
119 typedef std::map<FileWriter*, int> FileLogMap;
120
121 class CoreExport LogManager : public fakederef<LogManager>
122 {
123  private:
124         /** Lock variable, set to true when a log is in progress, which prevents further loggging from happening and creating a loop.
125          */
126         bool Logging;
127
128         /** Map of active log types and what LogStreams will receive them.
129          */
130         std::map<std::string, std::vector<LogStream *> > LogStreams;
131
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.
134          */
135         std::map<LogStream *, int> AllLogStreams;
136
137         /** LogStreams with type * (which means everything), and a list a logtypes they are excluded from (eg for "* -USERINPUT -USEROUTPUT").
138          */
139         std::map<LogStream *, std::vector<std::string> > GlobalLogStreams;
140
141         /** Refcounted map of all FileWriters in use by FileLogStreams, for file stream sharing.
142          */
143         FileLogMap FileLogs;
144
145  public:
146         LogManager();
147         ~LogManager();
148
149         /** Adds a FileWriter instance to LogManager, or increments the reference count of an existing instance.
150          * Used for file-stream sharing for FileLogStreams.
151          */
152         void AddLoggerRef(FileWriter* fw)
153         {
154                 FileLogMap::iterator i = FileLogs.find(fw);
155                 if (i == FileLogs.end())
156                 {
157                         FileLogs.insert(std::make_pair(fw, 1));
158                 }
159                 else
160                 {
161                         ++i->second;
162                 }
163         }
164
165         /** Indicates that a FileWriter reference has been removed. Reference count is decreased, and if zeroed, the FileWriter is closed.
166          */
167         void DelLoggerRef(FileWriter* fw)
168         {
169                 FileLogMap::iterator i = FileLogs.find(fw);
170                 if (i == FileLogs.end()) return; /* Maybe should log this? */
171                 if (--i->second < 1)
172                 {
173                         delete i->first;
174                         FileLogs.erase(i);
175                 }
176         }
177
178         /** Opens all logfiles defined in the configuration file using \<log method="file">.
179          */
180         void OpenFileLogs();
181
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.
184          */
185         void CloseLogs();
186
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.
193          */
194         void AddLogTypes(const std::string &type, LogStream *l, bool autoclose);
195
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.
202          */
203         bool AddLogType(const std::string &type, LogStream *l, bool autoclose);
204
205         /** Removes a logstream from the core. After removal, it will not receive 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).
207          */
208         void DelLogStream(LogStream* l);
209
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).
212          */
213         bool DelLogType(const std::string &type, LogStream *l);
214
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).
219          */
220         void Log(const std::string &type, LogLevel loglevel, const std::string &msg);
221
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.
226          */
227         void Log(const std::string &type, LogLevel loglevel, const char *fmt, ...) CUSTOM_PRINTF(4, 5);
228 };