Kea  2.1.7-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 
220  virtual isc::log::Severity getEffectiveSeverity();
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 
295  void setInterprocessSync(isc::log::interprocess::InterprocessSync* sync);
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
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:91
bool operator==(const Element &a, const Element &b)
Definition: data.cc:210
Logger Name is null.
Definition: log/logger.h:111
#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
Console Logger Implementation.
Definition: logger_impl.h:58
Severity
Severity Levels.
Definition: logger_level.h:23
Logging Not Initialized.
Definition: log/logger.h:122
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-lfc.
Logger Name Error.
Definition: log/logger.h:101
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:93
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
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:113