]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/logger.h
Add some docs
[user/henk/code/inspircd.git] / include / logger.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __LOGMANAGER_H
15 #define __LOGMANAGER_H
16
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
27  * blocking behaviour.
28  */
29 class CoreExport FileWriter : public EventHandler
30 {
31  protected:
32         /** The creator/owner of this object
33          */
34         InspIRCd* ServerInstance;
35
36         /** The log file (fd is inside this somewhere,
37          * we get it out with fileno())
38          */
39         FILE* log;
40
41         /** Buffer of pending log lines to be written
42          */
43         std::string buffer;
44
45         /** Number of write operations that have occured
46          */
47         int writeops;
48
49  public:
50         /** The constructor takes an already opened logfile.
51          */
52         FileWriter(InspIRCd* Instance, FILE* logfile);
53
54         /** This returns false, logfiles are writeable.
55          */
56         virtual bool Readable();
57
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.
63          */
64         virtual void HandleEvent(EventType et, int errornum = 0);
65
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.
72          */
73         void WriteLogLine(const std::string &line);
74
75         /** Close the log file and cancel any events.
76          */
77         virtual void Close();
78
79         /** Close the log file and cancel any events.
80          * (indirectly call Close()
81          */
82         virtual ~FileWriter();
83 };
84
85
86
87 /*
88  * New world logging!
89  * The brief summary:
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.
92  *
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.
98  *
99  *  HOW THIS WORKS
100  *   LogManager handles all instances of LogStreams, LogStreams (or more likely, derived classes) are instantiated and passed to it.
101  */
102
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.
104  */
105 class CoreExport LogStream : public classbase
106 {
107  protected:
108         InspIRCd *ServerInstance;
109         int loglvl;
110  public:
111         LogStream(InspIRCd *Instance, int loglevel) : loglvl(loglevel)
112         {
113                 this->ServerInstance = Instance;
114         }
115
116         virtual ~LogStream() { }
117
118         /** XXX document me properly.
119          * Used for on the fly changing of loglevel.
120          */
121         void ChangeLevel(int lvl) { this->loglvl = lvl; }
122
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.
126          */
127         virtual void OnLog(int loglevel, const std::string &type, const std::string &msg) = 0;
128 };
129
130 typedef std::map<FileWriter*, int> FileLogMap;
131
132 class CoreExport LogManager : public classbase
133 {
134  private:
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
142  public:
143         LogManager(InspIRCd *Instance)
144         {
145                 ServerInstance = Instance;
146                 Logging = false;
147         }
148
149         void SetupNoFork();
150
151         /** XXX document me properly. */
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         /** XXX document me properly. */
166         void DelLoggerRef(FileWriter* fw)
167         {
168                 FileLogMap::iterator i = FileLogs.find(fw);
169                 if (i == FileLogs.end()) return; /* Maybe should log this? */
170                 if (--i->second < 1)
171                 {
172                         delete i->first;
173                         FileLogs.erase(i);
174                 }
175         }
176
177         /** XXX document me properly. */
178         void OpenSingleFile(FILE* f, const std::string& type, int loglevel);
179         
180         /** XXX document me properly. */
181         void OpenFileLogs();
182         
183         /** Gives all logstreams a chance to clear up (in destructors) while it deletes them.
184          */
185         void CloseLogs();
186         
187         /** Registers a new logstream into the logging core, so it can be called for future events
188          * XXX document me properly.
189          */
190         bool AddLogType(const std::string &type, LogStream *l, bool autoclose);
191         
192         /** Removes a logstream from the core. After removal, it will not recieve further events.
193          */
194         void DelLogStream(LogStream* l);
195         
196         /** XXX document me properly. */
197         bool DelLogType(const std::string &type, LogStream *l);
198         
199         /** Pretty self explanatory.
200          */
201         void Log(const std::string &type, int loglevel, const std::string &msg);
202         
203         /** Duh.
204          */
205         void Log(const std::string &type, int loglevel, const char *fmt, ...);
206 };
207
208 #endif