]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/logger.cpp
Initial totally untested logger implementation that does nothing.
[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         return true;
59 }
60
61 bool LogManager::DelLogType(const std::string &type, LogStream *l)
62 {
63         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
64
65         if (i != LogStreams.end())
66         {
67                 std::vector<LogStream *>::iterator it = i->second.begin();
68
69                 while (it != i->second.end())
70                 {
71                         if (*it == l)
72                         {
73                                 i->second.erase(it);
74
75                                 if (i->second.size() == 0)
76                                 {
77                                         LogStreams.erase(i);
78                                 }
79
80                                 delete l;
81                                 return true;
82                         }
83                 }
84         }
85
86         return false;
87 }
88
89 void LogManager::Log(const std::string &type, int loglevel, const std::string &msg)
90 {
91         std::map<std::string, std::vector<LogStream *> >::iterator i = LogStreams.find(type);
92
93         if (i != LogStreams.end())
94         {
95                 std::vector<LogStream *>::iterator it = i->second.begin();
96
97                 while (it != i->second.end())
98                 {
99                         (*it)->OnLog(loglevel, msg);
100                 }
101
102                 return;
103         }
104
105         // blurp, no handler for this type
106         return;
107 }
108
109