InspIRCd  3.0
logger.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2008 Thomas Stagner <[email protected]>
5  * Copyright (C) 2008 Robin Burchell <[email protected]>
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 #pragma once
22 
24 enum LogLevel
25 {
26  LOG_RAWIO = 5,
27  LOG_DEBUG = 10,
28  LOG_VERBOSE = 20,
29  LOG_DEFAULT = 30,
30  LOG_SPARSE = 40,
31  LOG_NONE = 50
32 };
33 
36 class CoreExport FileWriter
37 {
38  protected:
42  FILE* log;
43 
46  unsigned int flush;
47 
50  unsigned int writeops;
51 
52  public:
55  FileWriter(FILE* logfile, unsigned int flushcount);
56 
64  void WriteLogLine(const std::string &line);
65 
68  virtual ~FileWriter();
69 };
70 
71 
72 
73 /*
74  * New world logging!
75  * The brief summary:
76  * Logging used to be a simple affair, a FILE * handled by a nonblocking logging class inheriting from EventHandler, that was inserted
77  * into the socket engine, and wrote lines. If nofork was on, it was printf()'d.
78  *
79  * We decided to horribly overcomplicate matters, and create vastly customisable logging. LogManager and LogStream form the visible basis
80  * of the new interface. Basically, a LogStream can be inherited to do different things with logging output. We inherit from it once in core
81  * to create a FileLogStream, that writes to a file, for example. Different LogStreams can hook different types of log messages, and different
82  * levels of output too, for extreme customisation. Multiple LogStreams can hook the same message/levels of output, meaning that e.g. output
83  * can go to a channel as well as a file.
84  *
85  * HOW THIS WORKS
86  * LogManager handles all instances of LogStreams, classes derived from LogStream are instantiated and passed to it.
87  */
88 
91 class CoreExport LogStream : public classbase
92 {
93  protected:
94  LogLevel loglvl;
95  public:
96  static const char LogHeader[];
97 
98  LogStream(LogLevel loglevel) : loglvl(loglevel)
99  {
100  }
101 
102  /* 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
103  * in the event that the resource is shared, see for example FileLogStream).
104  */
105  virtual ~LogStream() { }
106 
110  void ChangeLevel(LogLevel lvl) { this->loglvl = lvl; }
111 
116  virtual void OnLog(LogLevel loglevel, const std::string &type, const std::string &msg) = 0;
117 };
118 
119 typedef std::map<FileWriter*, int> FileLogMap;
120 
121 class CoreExport LogManager : public fakederef<LogManager>
122 {
123  private:
126  bool Logging;
127 
130  std::map<std::string, std::vector<LogStream *> > LogStreams;
131 
135  std::map<LogStream *, int> AllLogStreams;
136 
139  std::map<LogStream *, std::vector<std::string> > GlobalLogStreams;
140 
143  FileLogMap FileLogs;
144 
145  public:
146  LogManager();
147  ~LogManager();
148 
153  {
154  FileLogMap::iterator i = FileLogs.find(fw);
155  if (i == FileLogs.end())
156  {
157  FileLogs.insert(std::make_pair(fw, 1));
158  }
159  else
160  {
161  ++i->second;
162  }
163  }
164 
168  {
169  FileLogMap::iterator i = FileLogs.find(fw);
170  if (i == FileLogs.end()) return; /* Maybe should log this? */
171  if (--i->second < 1)
172  {
173  delete i->first;
174  FileLogs.erase(i);
175  }
176  }
177 
180  void OpenFileLogs();
181 
185  void CloseLogs();
186 
194  void AddLogTypes(const std::string &type, LogStream *l, bool autoclose);
195 
203  bool AddLogType(const std::string &type, LogStream *l, bool autoclose);
204 
208  void DelLogStream(LogStream* l);
209 
213  bool DelLogType(const std::string &type, LogStream *l);
214 
220  void Log(const std::string &type, LogLevel loglevel, const std::string &msg);
221 
227  void Log(const std::string &type, LogLevel loglevel, const char *fmt, ...) CUSTOM_PRINTF(4, 5);
228 };
FILE * log
Definition: logger.h:42
Definition: logger.h:91
void DelLoggerRef(FileWriter *fw)
Definition: logger.h:167
Definition: logger.h:121
unsigned int flush
Definition: logger.h:46
Definition: base.h:41
void AddLoggerRef(FileWriter *fw)
Definition: logger.h:152
void ChangeLevel(LogLevel lvl)
Definition: logger.h:110
Definition: inspircd.h:59
unsigned int writeops
Definition: logger.h:50
Definition: logger.h:36