Kea  2.3.9
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 
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 
297 
303  bool hasAppender(OutputOption::Destination const destination);
304 
310  bool operator==(Logger& other);
311 
312 private:
313  friend class isc::log::Formatter<Logger>;
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
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Bad Interprocess Sync.
Definition: log/logger.h:92
BadInterprocessSync(const char *file, size_t line, const char *what)
Definition: log/logger.h:94
The log message formatter.
Formatter & operator=(const Formatter &other)
Assignment operator.
Console Logger Implementation.
Definition: logger_impl.h:59
Logger Name Error.
Definition: log/logger.h:102
LoggerNameError(const char *file, size_t line, const char *what)
Definition: log/logger.h:104
Logger Name is null.
Definition: log/logger.h:112
LoggerNameNull(const char *file, size_t line, const char *what)
Definition: log/logger.h:114
Logger Class.
Definition: log/logger.h:142
Formatter error(const MessageID &ident)
Output Error Message.
Definition: log/logger.cc:181
Formatter info(const MessageID &ident)
Output Informational Message.
Definition: log/logger.cc:161
isc::log::Formatter< Logger > Formatter
The formatter used to replace placeholders.
Definition: log/logger.h:192
Formatter warn(const MessageID &ident)
Output Warning Message.
Definition: log/logger.cc:171
virtual void setSeverity(isc::log::Severity severity, int dbglevel=1)
Set Severity Level for Logger.
Definition: log/logger.cc:76
static const size_t MAX_LOGGER_NAME_SIZE
Maximum size of a logger name.
Definition: log/logger.h:145
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition: log/logger.cc:203
Formatter debug(int dbglevel, const MessageID &ident)
Output Debug Message.
Definition: log/logger.cc:151
virtual int getEffectiveDebugLevel()
Get Effective Debug Level for Logger.
Definition: log/logger.cc:105
virtual isc::log::Severity getEffectiveSeverity()
Get Effective Severity Level for Logger.
Definition: log/logger.cc:90
virtual ~Logger()
Destructor.
Definition: log/logger.cc:51
virtual isc::log::Severity getSeverity()
Get Severity Level for Logger.
Definition: log/logger.cc:83
virtual bool isWarnEnabled()
Is WARNING Enabled?
Definition: log/logger.cc:122
virtual bool isFatalEnabled()
Is FATAL Enabled?
Definition: log/logger.cc:132
Logger(const char *name)
Constructor.
Definition: log/logger.h:164
Formatter fatal(const MessageID &ident)
Output Fatal Message.
Definition: log/logger.cc:191
virtual bool isDebugEnabled(int dbglevel=MIN_DEBUG_LEVEL)
Returns if Debug Message Should Be Output.
Definition: log/logger.cc:112
virtual bool isInfoEnabled()
Is INFO Enabled?
Definition: log/logger.cc:117
bool operator==(Logger &other)
Equality.
Definition: log/logger.cc:215
virtual std::string getName()
Get Name of Logger.
Definition: log/logger.cc:69
virtual bool isErrorEnabled()
Is ERROR Enabled?
Definition: log/logger.cc:127
static std::string getVersion()
Version.
Definition: log/logger.cc:62
virtual int getDebugLevel()
Return DEBUG Level.
Definition: log/logger.cc:97
bool hasAppender(OutputOption::Destination const destination)
Check if this logger has an appender of the given type.
Definition: log/logger.cc:208
Logging Not Initialized.
Definition: log/logger.h:123
LoggingNotInitialized(const char *file, size_t line, const char *what)
Definition: log/logger.h:125
const Name & name_
Definition: dns/message.cc:693
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
const int MIN_DEBUG_LEVEL
Minimum/maximum debug levels.
Definition: logger_level.h:35
const char * MessageID
Definition: message_types.h:15
Severity
Severity Levels.
Definition: logger_level.h:23
Defines the logger used by the top-level component of kea-lfc.
Destination
Destinations.
Definition: output_option.h:48