InspIRCd  2.0
 All Classes Namespaces Functions Variables Typedefs Enumerations
logger.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
5  * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd. InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef LOGGER_H
22 #define LOGGER_H
23 
26 class CoreExport FileWriter
27 {
28  protected:
32  FILE* log;
33 
36  int writeops;
37 
38  public:
41  FileWriter(FILE* logfile);
42 
50  void WriteLogLine(const std::string &line);
51 
54  virtual ~FileWriter();
55 };
56 
57 
58 
59 /*
60  * New world logging!
61  * The brief summary:
62  * Logging used to be a simple affair, a FILE * handled by a nonblocking logging class inheriting from EventHandler, that was inserted
63  * into the socket engine, and wrote lines. If nofork was on, it was printf()'d.
64  *
65  * We decided to horribly overcomplicate matters, and create vastly customisable logging. LogManager and LogStream form the visible basis
66  * of the new interface. Basically, a LogStream can be inherited to do different things with logging output. We inherit from it once in core
67  * to create a FileLogStream, that writes to a file, for example. Different LogStreams can hook different types of log messages, and different
68  * levels of output too, for extreme customisation. Multiple LogStreams can hook the same message/levels of output, meaning that e.g. output
69  * can go to a channel as well as a file.
70  *
71  * HOW THIS WORKS
72  * LogManager handles all instances of LogStreams, classes derived from LogStream are instantiated and passed to it.
73  */
74 
77 class CoreExport LogStream : public classbase
78 {
79  protected:
80  int loglvl;
81  public:
82  LogStream(int loglevel) : loglvl(loglevel)
83  {
84  }
85 
86  /* A LogStream's destructor should do whatever it needs to close any resources it was using (or indicate that it is no longer using a resource
87  * in the event that the resource is shared, see for example FileLogStream).
88  */
89  virtual ~LogStream() { }
90 
94  void ChangeLevel(int lvl) { this->loglvl = lvl; }
95 
100  virtual void OnLog(int loglevel, const std::string &type, const std::string &msg) = 0;
101 };
102 
103 typedef std::map<FileWriter*, int> FileLogMap;
104 
105 class CoreExport LogManager
106 {
107  private:
110  bool Logging;
111 
114  std::map<std::string, std::vector<LogStream *> > LogStreams;
115 
119  std::map<LogStream *, int> AllLogStreams;
120 
123  std::map<LogStream *, std::vector<std::string> > GlobalLogStreams;
124 
127  FileLogMap FileLogs;
128 
129  public:
130 
131  LogManager();
132  ~LogManager();
133 
138  {
139  FileLogMap::iterator i = FileLogs.find(fw);
140  if (i == FileLogs.end())
141  {
142  FileLogs.insert(std::make_pair(fw, 1));
143  }
144  else
145  {
146  ++i->second;
147  }
148  }
149 
153  {
154  FileLogMap::iterator i = FileLogs.find(fw);
155  if (i == FileLogs.end()) return; /* Maybe should log this? */
156  if (--i->second < 1)
157  {
158  delete i->first;
159  FileLogs.erase(i);
160  }
161  }
162 
165  void OpenFileLogs();
166 
170  void CloseLogs();
171 
179  void AddLogTypes(const std::string &type, LogStream *l, bool autoclose);
180 
188  bool AddLogType(const std::string &type, LogStream *l, bool autoclose);
189 
193  void DelLogStream(LogStream* l);
194 
198  bool DelLogType(const std::string &type, LogStream *l);
199 
205  void Log(const std::string &type, int loglevel, const std::string &msg);
206 
212  void Log(const std::string &type, int loglevel, const char *fmt, ...) CUSTOM_PRINTF(4, 5);
213 };
214 
215 #endif
FILE * log
Definition: logger.h:32
Definition: logger.h:77
void DelLoggerRef(FileWriter *fw)
Definition: logger.h:152
void ChangeLevel(int lvl)
Definition: logger.h:94
Definition: logger.h:105
Definition: base.h:42
int writeops
Definition: logger.h:36
void AddLoggerRef(FileWriter *fw)
Definition: logger.h:137
Definition: logger.h:26