Kea  1.9.9-git
log/logger.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2021 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 
21 namespace isc {
22 namespace log {
23 
24 namespace interprocess {
25 // Forward declaration to hide implementation details from normal
26 // applications.
27 class InterprocessSync;
28 }
29 
84 
85 class LoggerImpl; // Forward declaration of the implementation class
86 
92 public:
93  BadInterprocessSync(const char* file, size_t line, const char* what) :
94  isc::Exception(file, line, what)
95  {}
96 };
97 
102 public:
103  LoggerNameError(const char* file, size_t line, const char* what) :
104  isc::Exception(file, line, what)
105  {}
106 };
107 
112 public:
113  LoggerNameNull(const char* file, size_t line, const char* what) :
114  isc::Exception(file, line, what)
115  {}
116 };
117 
123 public:
124  LoggingNotInitialized(const char* file, size_t line, const char* what) :
125  isc::Exception(file, line, what)
126  {}
127 };
128 
140 
141 class Logger {
142 public:
144  static const size_t MAX_LOGGER_NAME_SIZE = 31;
145 
163  Logger(const char* name) : loggerptr_(0), initialized_(false) {
164 
165  // Validate the name of the logger.
166  if (name) {
167  // Name not null, is it too short or too long?
168  size_t namelen = std::strlen(name);
169  if ((namelen == 0) || (namelen > MAX_LOGGER_NAME_SIZE)) {
170  isc_throw(LoggerNameError, "'" << name << "' is not a valid "
171  << "name for a logger: valid names must be between 1 "
172  << "and " << MAX_LOGGER_NAME_SIZE << " characters in "
173  << "length");
174  }
175  } else {
176  isc_throw(LoggerNameNull, "logger names may not be null");
177  }
178 
179  // Do the copy, ensuring a trailing null in all cases.
180  std::strncpy(name_, name, MAX_LOGGER_NAME_SIZE);
181  name_[MAX_LOGGER_NAME_SIZE] = '\0';
182  }
183 
185  virtual ~Logger();
186 
188  static std::string getVersion();
189 
192 
196  virtual std::string getName();
197 
207  virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
208 
214 
221 
226  virtual int getDebugLevel();
227 
233  virtual int getEffectiveDebugLevel();
234 
240  virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
241 
243  virtual bool isInfoEnabled();
244 
246  virtual bool isWarnEnabled();
247 
249  virtual bool isErrorEnabled();
250 
252  virtual bool isFatalEnabled();
253 
259  Formatter debug(int dbglevel, const MessageID& ident);
260 
264  Formatter info(const MessageID& ident);
265 
269  Formatter warn(const MessageID& ident);
270 
274  Formatter error(const MessageID& ident);
275 
279  Formatter fatal(const MessageID& ident);
280 
296 
302  bool operator==(Logger& other);
303 
304 private:
306 
313  void output(const Severity& severity, const std::string& message);
314 
319  Logger(const Logger&);
320 
325  Logger& operator=(const Logger&);
326 
339  LoggerImpl* getLoggerPtr();
340 
342  void initLoggerImpl();
343 
345  LoggerImpl* loggerptr_;
346 
348  char name_[MAX_LOGGER_NAME_SIZE + 1];
349 
351  std::mutex mutex_;
352 
354  std::atomic<bool> initialized_;
355 };
356 
357 } // namespace log
358 } // namespace isc
359 
360 
361 #endif // LOGGER_H
Logger(const char *name)
Constructor.
Definition: log/logger.h:163
Logger Class.
Definition: log/logger.h:141
virtual bool isWarnEnabled()
Is WARNING Enabled?
Definition: log/logger.cc:122
virtual bool isErrorEnabled()
Is ERROR Enabled?
Definition: log/logger.cc:127
virtual bool isInfoEnabled()
Is INFO Enabled?
Definition: log/logger.cc:117
Bad Interprocess Sync.
Definition: log/logger.h:91
Formatter fatal(const MessageID &ident)
Output Fatal Message.
Definition: log/logger.cc:191
virtual isc::log::Severity getSeverity()
Get Severity Level for Logger.
Definition: log/logger.cc:83
virtual ~Logger()
Destructor.
Definition: log/logger.cc:51
Logger Name is null.
Definition: log/logger.h:111
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Formatter debug(int dbglevel, const MessageID &ident)
Output Debug Message.
Definition: log/logger.cc:151
const int MIN_DEBUG_LEVEL
Minimum/maximum debug levels.
Definition: logger_level.h:35
Console Logger Implementation.
Definition: logger_impl.h:58
Severity
Severity Levels.
Definition: logger_level.h:23
Logging Not Initialized.
Definition: log/logger.h:122
virtual bool isFatalEnabled()
Is FATAL Enabled?
Definition: log/logger.cc:132
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
virtual bool isDebugEnabled(int dbglevel=MIN_DEBUG_LEVEL)
Returns if Debug Message Should Be Output.
Definition: log/logger.cc:112
LoggingNotInitialized(const char *file, size_t line, const char *what)
Definition: log/logger.h:124
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
Logger Name Error.
Definition: log/logger.h:101
static const size_t MAX_LOGGER_NAME_SIZE
Maximum size of a logger name.
Definition: log/logger.h:144
The log message formatter.
virtual int getDebugLevel()
Return DEBUG Level.
Definition: log/logger.cc:97
virtual int getEffectiveDebugLevel()
Get Effective Debug Level for Logger.
Definition: log/logger.cc:105
Formatter error(const MessageID &ident)
Output Error Message.
Definition: log/logger.cc:181
const Name & name_
Definition: dns/message.cc:693
BadInterprocessSync(const char *file, size_t line, const char *what)
Definition: log/logger.h:93
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 std::string getVersion()
Version.
Definition: log/logger.cc:62
LoggerNameError(const char *file, size_t line, const char *what)
Definition: log/logger.h:103
isc::log::Formatter< Logger > Formatter
The formatter used to replace placeholders.
Definition: log/logger.h:191
virtual std::string getName()
Get Name of Logger.
Definition: log/logger.cc:69
virtual isc::log::Severity getEffectiveSeverity()
Get Effective Severity Level for Logger.
Definition: log/logger.cc:90
Formatter info(const MessageID &ident)
Output Informational Message.
Definition: log/logger.cc:161
const char * MessageID
Definition: message_types.h:15
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition: log/logger.cc:203
bool operator==(Logger &other)
Equality.
Definition: log/logger.cc:210
Formatter & operator=(const Formatter &other)
Assignment operator.
LoggerNameNull(const char *file, size_t line, const char *what)
Definition: log/logger.h:113