]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/logger.cpp
Fix a bug in new logging API (global logstreams weren't notified of events if a speci...
[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 void LogManager::CloseLogs()
46 {
47         /*
48          * This doesn't remove logstreams from the map/vector etc, because if this is called, shit is hitting the fan
49          * and we're going down anyway - this just provides a "nice" way for logstreams to clean up. -- w
50          */
51         std::map<std::string, std::vector<LogStream *> >::iterator i;
52
53         while (LogStreams.begin() != LogStreams.end())
54         {
55                 i = LogStreams.begin();
56
57                 while (i->second.begin() != i->second.end())
58                 {
59                         std::vector<LogStream *>::iterator it = i->second.begin();
60
61                         delete (*it);
62                         i->second.erase(it);
63                 }
64
65                 LogStreams.erase(i);
66         }
67 }
68
69 bool LogManager::AddLogType(const std::string &type, LogStream *l)
70 {
71         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
72
73         if (i != LogStreams.end())
74                 i->second.push_back(l);
75         else
76         {
77                 std::vector<LogStream *> v;
78                 v.push_back(l);
79                 LogStreams[type] = v;
80         }
81
82         if (type == "*")
83                 GlobalLogStreams.push_back(l);
84
85         return true;
86 }
87
88 bool LogManager::DelLogType(const std::string &type, LogStream *l)
89 {
90         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
91         std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
92
93         while (gi != GlobalLogStreams.end())
94         {
95                 if ((*gi) == l)
96                 {
97                         GlobalLogStreams.erase(gi);
98                         break;
99                 }
100         }
101
102         if (i != LogStreams.end())
103         {
104                 std::vector<LogStream *>::iterator it = i->second.begin();
105
106                 while (it != i->second.end())
107                 {
108                         if (*it == l)
109                         {
110                                 i->second.erase(it);
111
112                                 if (i->second.size() == 0)
113                                 {
114                                         LogStreams.erase(i);
115                                 }
116
117                                 delete l;
118                                 return true;
119                         }
120
121                         it++;
122                 }
123         }
124
125         return false;
126 }
127
128 void LogManager::Log(const std::string &type, int loglevel, const char *fmt, ...)
129 {
130         if (Logging)
131                 return;
132
133         va_list a;
134         static char buf[65536];
135
136         va_start(a, fmt);
137         vsnprintf(buf, 65536, fmt, a);
138         va_end(a);
139
140         this->Log(type, loglevel, std::string(buf));
141 }
142
143 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
144 {
145         if (Logging)
146                 return;
147
148         Logging = true;
149
150         std::vector<LogStream *>::iterator gi = GlobalLogStreams.begin();
151
152         while (gi != GlobalLogStreams.end())
153         {
154                 (*gi)->OnLog(loglevel, type, msg);
155                 gi++;
156         }
157
158         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
159
160         if (i != LogStreams.end())
161         {
162                 std::vector<LogStream *>::iterator it = i->second.begin();
163
164                 while (it != i->second.end())
165                 {
166                         (*it)->OnLog(loglevel, type, msg);
167                         it++;
168                 }
169         }
170
171         Logging = false;
172 }
173
174