InspIRCd  4.0
logger.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2014 Attila Molnar <[email protected]>
5  * Copyright (C) 2012-2013, 2017 Sadie Powell <[email protected]>
6  * Copyright (C) 2012, 2019 Robby <[email protected]>
7  * Copyright (C) 2010 Craig Edwards <[email protected]>
8  * Copyright (C) 2009 Daniel De Graaf <[email protected]>
9  * Copyright (C) 2008, 2012 Robin Burchell <[email protected]>
10  * Copyright (C) 2008 Thomas Stagner <[email protected]>
11  *
12  * This file is part of InspIRCd. InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 
26 #pragma once
27 
29 enum LogLevel
30 {
31  LOG_RAWIO = 5,
32  LOG_DEBUG = 10,
33  LOG_VERBOSE = 20,
34  LOG_DEFAULT = 30,
35  LOG_SPARSE = 40,
36  LOG_NONE = 50
37 };
38 
41 class CoreExport FileWriter
42 {
43  protected:
47  FILE* log;
48 
51  unsigned int flush;
52 
55  unsigned int writeops = 0;
56 
57  public:
60  FileWriter(FILE* logfile, unsigned int flushcount);
61 
69  void WriteLogLine(const std::string &line);
70 
73  ~FileWriter();
74 };
75 
76 
77 
78 /*
79  * New world logging!
80  * The brief summary:
81  * Logging used to be a simple affair, a FILE * handled by a nonblocking logging class inheriting from EventHandler, that was inserted
82  * into the socket engine, and wrote lines. If nofork was on, it was printf()'d.
83  *
84  * We decided to horribly overcomplicate matters, and create vastly customisable logging. LogManager and LogStream form the visible basis
85  * of the new interface. Basically, a LogStream can be inherited to do different things with logging output. We inherit from it once in core
86  * to create a FileLogStream, that writes to a file, for example. Different LogStreams can hook different types of log messages, and different
87  * levels of output too, for extreme customisation. Multiple LogStreams can hook the same message/levels of output, meaning that e.g. output
88  * can go to a channel as well as a file.
89  *
90  * HOW THIS WORKS
91  * LogManager handles all instances of LogStreams, classes derived from LogStream are instantiated and passed to it.
92  */
93 
96 class CoreExport LogStream : public Cullable
97 {
98  protected:
99  LogLevel loglvl;
100  public:
101  static const char LogHeader[];
102 
103  LogStream(LogLevel loglevel) : loglvl(loglevel)
104  {
105  }
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
122 {
123  private:
126  bool Logging = false;
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:
150  {
151  FileLogMap::iterator i = FileLogs.find(fw);
152  if (i == FileLogs.end())
153  {
154  FileLogs.emplace(fw, 1);
155  }
156  else
157  {
158  ++i->second;
159  }
160  }
161 
165  {
166  FileLogMap::iterator i = FileLogs.find(fw);
167  if (i == FileLogs.end()) return; /* Maybe should log this? */
168  if (--i->second < 1)
169  {
170  delete i->first;
171  FileLogs.erase(i);
172  }
173  }
174 
177  void OpenFileLogs();
178 
182  void CloseLogs();
183 
191  void AddLogTypes(const std::string &type, LogStream *l, bool autoclose);
192 
200  bool AddLogType(const std::string &type, LogStream *l, bool autoclose);
201 
205  void DelLogStream(LogStream* l);
206 
210  bool DelLogType(const std::string &type, LogStream *l);
211 
217  void Log(const std::string &type, LogLevel loglevel, const std::string &msg);
218 
224  void Log(const std::string &type, LogLevel loglevel, const char *fmt, ...) CUSTOM_PRINTF(4, 5);
225 };
Definition: cull.h:30
Definition: logger.h:42
unsigned int flush
Definition: logger.h:51
FILE * log
Definition: logger.h:47
Definition: logger.h:122
void DelLoggerRef(FileWriter *fw)
Definition: logger.h:164
void AddLoggerRef(FileWriter *fw)
Definition: logger.h:149
Definition: logger.h:97
virtual void OnLog(LogLevel loglevel, const std::string &type, const std::string &msg)=0
void ChangeLevel(LogLevel lvl)
Definition: logger.h:110