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