]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/logger.cpp
New logging implementation. Also write messages about InspIRCd::Log() being deprecate...
[user/henk/code/inspircd.git] / src / logger.cpp
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 /* $Core: libIRCDlogger */
15
16 #include "inspircd.h"
17
18 /*
19  * Suggested implementation...
20  *      class LogManager
21  *              bool AddLogType(const std::string &type, enum loglevel, LogStream *)
22  *              bool DelLogType(const std::string &type, LogStream *)
23  *              Log(const std::string &type, enum loglevel, const std::string &msg)
24  *              std::map<std::string, std::vector<LogStream *> > logstreams (holds a 'chain' of logstreams for each type that are all notified when a log happens)
25  *
26  *  class LogStream
27  *              std::string type
28  *      virtual void OnLog(enum loglevel, const std::string &msg)
29  *
30  * How it works:
31  *  Modules create their own logstream types (core will create one for 'file logging' for example) and create instances of these logstream types
32  *  and register interest in a certain logtype. Globbing is not here, with the exception of * - for all events.. loglevel is used to drop 
33  *  events that are of no interest to a logstream.
34  *
35  *  When Log is called, the vector of logstreams for that type is iterated (along with the special vector for "*"), and all registered logstreams
36  *  are called back ("OnLog" or whatever) to do whatever they like with the message. In the case of the core, this will write to a file.
37  *  In the case of the module I plan to write (m_logtochannel or something), it will log to the channel(s) for that logstream, etc.
38  *
39  * NOTE: Somehow we have to let LogManager manage the non-blocking file streams and provide an interface to share them with various LogStreams,
40  *       as, for example, a user may want to let 'KILL' and 'XLINE' snotices go to /home/ircd/inspircd/logs/operactions.log, or whatever. How
41  *       can we accomplish this easily? I guess with a map of pre-loved logpaths, and a pointer of FILE *..
42  * 
43  */
44
45 bool LogManager::AddLogType(const std::string &type, LogStream *l)
46 {
47         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
48
49         if (i != LogStreams.end())
50                 i->second.push_back(l);
51         else
52         {
53                 std::vector<LogStream *> v;
54                 v.push_back(l);
55                 LogStreams[type] = v;
56         }
57
58         if (type == "*")
59                 GlobalLogStreams.push_back(l);
60
61         return true;
62 }
63
64 bool LogManager::DelLogType(const std::string &type, LogStream *l)
65 {
66         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
67         std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
68
69         while (gi != GlobalLogStreams.end())
70         {
71                 if ((*gi) == l)
72                 {
73                         GlobalLogStreams.erase(gi);
74                         break;
75                 }
76         }
77
78         if (i != LogStreams.end())
79         {
80                 std::vector<LogStream *>::iterator it = i->second.begin();
81
82                 while (it != i->second.end())
83                 {
84                         if (*it == l)
85                         {
86                                 i->second.erase(it);
87
88                                 if (i->second.size() == 0)
89                                 {
90                                         LogStreams.erase(i);
91                                 }
92
93                                 delete l;
94                                 return true;
95                         }
96
97                         it++;
98                 }
99         }
100
101         return false;
102 }
103
104 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
105 {
106         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
107
108         if (i != LogStreams.end())
109         {
110                 std::vector<LogStream *>::iterator it = i->second.begin();
111
112                 while (it != i->second.end())
113                 {
114                         (*it)->OnLog(loglevel, msg);
115                         it++;
116                 }
117
118                 return;
119         }
120
121         std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
122
123         while (gi != GlobalLogStreams.end())
124         {
125                 (*gi)->OnLog(loglevel, msg);
126                 gi++;
127         }
128
129         // blurp, no handler for this type
130         return;
131 }
132
133