Kea  2.3.5-git
log/logger.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef LOGGER_H
8 #define LOGGER_H
9 
10 #include <atomic>
11 #include <cstdlib>
12 #include <cstring>
13 #include <mutex>
14 #include <string>
15 
16 #include <exceptions/exceptions.h>
17 #include <log/logger_level.h>
18 #include <log/message_types.h>
19 #include <log/log_formatter.h>
20 #include <log/output_option.h>
21 
22 namespace isc {
23 namespace log {
24 
25 namespace interprocess {
26 // Forward declaration to hide implementation details from normal
27 // applications.
28 class InterprocessSync;
29 }
30 
85 
86 class LoggerImpl; // Forward declaration of the implementation class
87 
93 public:
94  BadInterprocessSync(const char* file, size_t line, const char* what) :
95  isc::Exception(file, line, what)
96  {}
97 };
98 
103 public:
104  LoggerNameError(const char* file, size_t line, const char* what) :
105  isc::Exception(file, line, what)
106  {}
107 };
108 
113 public:
114  LoggerNameNull(const char* file, size_t line, const char* what) :
115  isc::Exception(file, line, what)
116  {}
117 };
118 
124 public:
125  LoggingNotInitialized(const char* file, size_t line, const char* what) :
126  isc::Exception(file, line, what)
127  {}
128 };
129 
141 
142 class Logger {
143 public:
145  static const size_t MAX_LOGGER_NAME_SIZE = 31;
146 
164  Logger(const char* name) : loggerptr_(0), initialized_(false) {
165 
166  // Validate the name of the logger.
167  if (name) {
168  // Name not null, is it too short or too long?
169  size_t namelen = std::strlen(name);
170  if ((namelen == 0) || (namelen > MAX_LOGGER_NAME_SIZE)) {
171  isc_throw(LoggerNameError, "'" << name << "' is not a valid "
172  << "name for a logger: valid names must be between 1 "
173  << "and " << MAX_LOGGER_NAME_SIZE << " characters in "
174  << "length");
175  }
176  } else {
177  isc_throw(LoggerNameNull, "logger names may not be null");
178  }
179 
180  // Do the copy, ensuring a trailing null in all cases.
181  std::strncpy(name_, name, MAX_LOGGER_NAME_SIZE);
182  name_[MAX_LOGGER_NAME_SIZE] = '\0';
183  }
184 
186  virtual ~Logger();
187 
189  static std::string getVersion();
190 
193 
197  virtual std::string getName();
198 
208  virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
209 
215 
221  virtual isc::log::Severity getEffectiveSeverity();
222 
227  virtual int getDebugLevel();
228 
234  virtual int getEffectiveDebugLevel();
235 
241  virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
242 
244  virtual bool isInfoEnabled();
245 
247  virtual bool isWarnEnabled();
248 
250  virtual bool isErrorEnabled();
251 
253  virtual bool isFatalEnabled();
254 
260  Formatter debug(int dbglevel, const MessageID& ident);
261 
265  Formatter info(const MessageID& ident);
266 
270  Formatter warn(const MessageID& ident);
271 
275  Formatter error(const MessageID& ident);
276 
280  Formatter fatal(const MessageID& ident);
281 
296  void setInterprocessSync(isc::log::interprocess::InterprocessSync* sync);
297 
303  bool hasAppender(OutputOption::Destination const destination);
304 
310  bool operator==(Logger& other);
311 
312 private:
314 
321  void output(const Severity& severity, const std::string& message);
322 
327  Logger(const Logger&);
328 
333  Logger& operator=(const Logger&);
334 
347  LoggerImpl* getLoggerPtr();
348 
350  void initLoggerImpl();
351 
353  LoggerImpl* loggerptr_;
354 
356  char name_[MAX_LOGGER_NAME_SIZE + 1];
357 
359  std::mutex mutex_;
360 
362  std::atomic<bool> initialized_;
363 };
364 
365 } // namespace log
366 } // namespace isc
367 
368 
369 #endif // LOGGER_H
Logger(const char *name)
Constructor.
Definition: log/logger.h:164
Logger Class.
Definition: log/logger.h:142
isc::log::Severity getSeverity(const std::string &sev_str)
Returns the isc::log::Severity value represented by the given string.
Definition: logger_level.cc:20
Bad Interprocess Sync.
Definition: log/logger.h:92
bool operator==(const Element &a, const Element &b)
Definition: data.cc:210
Logger Name is null.
Definition: log/logger.h:112
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Destination
Destinations.
Definition: output_option.h:48
const int MIN_DEBUG_LEVEL
Minimum/maximum debug levels.
Definition: logger_level.h:35
Console Logger Implementation.
Definition: logger_impl.h:59
Severity
Severity Levels.
Definition: logger_level.h:23
Logging Not Initialized.
Definition: log/logger.h:123
LoggingNotInitialized(const char *file, size_t line, const char *what)
Definition: log/logger.h:125
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
Logger Name Error.
Definition: log/logger.h:102
The log message formatter.
const Name & name_
Definition: dns/message.cc:693
BadInterprocessSync(const char *file, size_t line, const char *what)
Definition: log/logger.h:94
LoggerNameError(const char *file, size_t line, const char *what)
Definition: log/logger.h:104
isc::log::Formatter< Logger > Formatter
The formatter used to replace placeholders.
Definition: log/logger.h:192
const char * MessageID
Definition: message_types.h:15
Formatter & operator=(const Formatter &other)
Assignment operator.
LoggerNameNull(const char *file, size_t line, const char *what)
Definition: log/logger.h:114