]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanlog.cpp
Zap printfs
[user/henk/code/inspircd.git] / src / modules / m_chanlog.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 #include "inspircd.h"
15
16 class ChannelLogStream : public LogStream
17 {
18  private:
19         std::string channel;
20
21  public:
22         ChannelLogStream(InspIRCd *Instance, int loglevel, const std::string &chan) : LogStream(Instance, loglevel), channel(chan)
23         {
24         }
25
26         virtual void OnLog(int loglevel, const std::string &type, const std::string &msg)
27         {
28                 Channel *c = ServerInstance->FindChan(channel);
29                 static bool Logging = false;
30
31                 if (loglevel < this->loglvl)
32                         return;
33
34                 if (Logging)
35                         return;
36
37                 if (c)
38                 {
39                         Logging = true; // this avoids (rare chance) loops with logging server IO on networks
40                         char buf[MAXBUF];
41                         snprintf(buf, MAXBUF, "\2%s\2: %s", type.c_str(), msg.c_str());
42
43                         c->WriteChannelWithServ(ServerInstance->Config->ServerName, "PRIVMSG %s :%s", c->name, buf);
44                         ServerInstance->PI->SendChannelPrivmsg(c, 0, buf);
45                         Logging = false;
46                 }
47         }
48 };
49
50 /* $ModDesc: Logs output to a channel instead of / as well as a file. */
51
52 class ModuleChanLog : public Module
53 {
54  private:
55         std::vector<ChannelLogStream*> cls;
56  public:
57         ModuleChanLog(InspIRCd* Me) : Module(Me)
58         {
59                 Implementation eventlist[] = { I_OnRehash };
60                 ServerInstance->Modules->Attach(eventlist, this, 1);
61
62                 OnRehash(NULL, "");
63         }
64
65         virtual ~ModuleChanLog()
66         {
67                 std::vector<ChannelLogStream*>::iterator i;
68                 while ((i = cls.begin()) != cls.end())
69                 {
70                         ServerInstance->Logs->DelLogStream(*i);
71                         cls.erase(i);
72                 }
73         }
74
75         virtual void OnRehash(User *user, const std::string &parameter)
76         {
77                 ConfigReader Conf(ServerInstance);
78
79                 /* Since the CloseLogs prior to this hook just wiped out our logstreams for us, we just need to wipe the vector. */
80                 std::vector<ChannelLogStream*>().swap(cls);
81                 int index, max = Conf.Enumerate("log");
82                 cls.reserve(max);
83
84                 for (index = 0; index < max; ++index)
85                 {
86                         std::string method = Conf.ReadValue("log", "method", index);
87
88
89                         //if (method != "file")
90                         //      continue;
91
92                         std::string type = Conf.ReadValue("log", "type", index);
93                         std::string level = Conf.ReadValue("log", "level", index);
94                         int loglevel = DEFAULT;
95
96                         if (level == "debug")
97                         {
98                                 loglevel = DEBUG;
99                                 ServerInstance->Config->debugging = true;
100                         }
101                         else if (level == "verbose")
102                         {
103                                 loglevel = VERBOSE;
104                         }
105                         else if (level == "default")
106                         {
107                                 loglevel = DEFAULT;
108                         }
109                         else if (level == "sparse")
110                         {
111                                 loglevel = SPARSE;
112                         }
113                         else if (level == "none")
114                         {
115                                 loglevel = NONE;
116                         }
117
118                         std::string target = Conf.ReadValue("log", "target", index);
119                         ChannelLogStream* c = new ChannelLogStream(ServerInstance, loglevel, target);
120                         ServerInstance->Logs->AddLogTypes(type, c, true);
121                         cls.push_back(c);
122                 }
123         }
124
125         virtual Version GetVersion()
126         {
127                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
128         }
129 };
130
131
132 MODULE_INIT(ModuleChanLog)
133