]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/filelogger.h
Fix a bug in new logging API (global logstreams weren't notified of events if a speci...
[user/henk/code/inspircd.git] / include / filelogger.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 __LOG_H__
15 #define __LOG_H__
16
17 /** Debug levels for use with InspIRCd::Log()
18  *  */
19 enum DebugLevel
20 {
21     DEBUG       =   10,
22     VERBOSE     =   20,
23     DEFAULT     =   30,
24     SPARSE      =   40,
25     NONE        =   50
26 };
27
28
29 /* Forward declaration -- required */
30 class InspIRCd;
31
32 /** This class implements a nonblocking log-writer.
33  * Most people writing an ircd give little thought to their disk
34  * i/o. On a congested system, disk writes can block for long
35  * periods of time (e.g. if the system is busy and/or swapping
36  * a lot). If we just use a blocking fprintf() call, this could
37  * block for undesirable amounts of time (half of a second through
38  * to whole seconds). We DO NOT want this, so we make our logfile
39  * nonblocking and hook it into the SocketEngine.
40  * NB: If the operating system does not support nonblocking file
41  * I/O (linux seems to, as does freebsd) this will default to
42  * blocking behaviour.
43  */
44 class CoreExport FileLogger : public EventHandler
45 {
46  protected:
47         /** The creator/owner of this object
48          */
49         InspIRCd* ServerInstance;
50         /** The log file (fd is inside this somewhere,
51          * we get it out with fileno())
52          */
53         FILE* log;
54         /** Buffer of pending log lines to be written
55          */
56         std::string buffer;
57         /** Number of write operations that have occured
58          */
59         int writeops;
60  public:
61         /** The constructor takes an already opened logfile.
62          */
63         FileLogger(InspIRCd* Instance, FILE* logfile);
64         /** This returns false, logfiles are writeable.
65          */
66         virtual bool Readable();
67         /** Handle pending write events.
68          * This will flush any waiting data to disk.
69          * If any data remains after the fprintf call,
70          * another write event is scheduled to write
71          * the rest of the data when possible.
72          */
73         virtual void HandleEvent(EventType et, int errornum = 0);
74         /** Write one or more preformatted log lines.
75          * If the data cannot be written immediately,
76          * this class will insert itself into the
77          * SocketEngine, and register a write event,
78          * and when the write event occurs it will
79          * attempt again to write the data.
80          */
81         void WriteLogLine(const std::string &line);
82         /** Close the log file and cancel any events.
83          */
84         virtual void Close();
85         /** Close the log file and cancel any events.
86          * (indirectly call Close()
87          */
88         virtual ~FileLogger();
89 };
90
91 class CoreExport FileLogStream : public LogStream
92 {
93  private:
94         FileLogger *f;
95  public:
96         FileLogStream(InspIRCd *Instance, FILE *f) : LogStream(Instance)
97         {
98                 this->f = new FileLogger(Instance, f);
99         }
100
101         ~FileLogStream()
102         {
103                 delete this->f;
104         }
105
106         virtual void OnLog(int loglevel, const std::string &type, const std::string &msg);
107 };
108
109 #endif