Kea  1.5.0
log/logger.h
Go to the documentation of this file.
1 // Copyright (C) 2011-2015 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 <cassert>
11 #include <cstdlib>
12 #include <string>
13 #include <cstring>
14 
15 #include <boost/static_assert.hpp>
16 
17 #include <exceptions/exceptions.h>
18 #include <log/logger_level.h>
19 #include <log/message_types.h>
20 #include <log/log_formatter.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 
86 
87 class LoggerImpl; // Forward declaration of the implementation class
88 
94 public:
95  BadInterprocessSync(const char* file, size_t line, const char* what) :
96  isc::Exception(file, line, what)
97  {}
98 };
99 
104 public:
105  LoggerNameError(const char* file, size_t line, const char* what) :
106  isc::Exception(file, line, what)
107  {}
108 };
109 
114 public:
115  LoggerNameNull(const char* file, size_t line, const char* what) :
116  isc::Exception(file, line, what)
117  {}
118 };
119 
125 public:
126  LoggingNotInitialized(const char* file, size_t line, const char* what) :
127  isc::Exception(file, line, what)
128  {}
129 };
130 
142 
143 class Logger {
144 public:
146  static const size_t MAX_LOGGER_NAME_SIZE = 31;
147 
165  Logger(const char* name) : loggerptr_(NULL) {
166 
167  // Validate the name of the logger.
168  if (name == NULL) {
169  isc_throw(LoggerNameNull, "logger names may not be null");
170 
171  } else {
172  // Name not null, is it too short or too long?
173  size_t namelen = std::strlen(name);
174  if ((namelen == 0) || (namelen > MAX_LOGGER_NAME_SIZE)) {
175  isc_throw(LoggerNameError, "'" << name << "' is not a valid "
176  << "name for a logger: valid names must be between 1 "
177  << "and " << MAX_LOGGER_NAME_SIZE << " characters in "
178  << "length");
179  }
180  }
181 
182  // The checks above and the assertion below ensure that the contents of
183  // "name" plus a trailing null will fit into the space allocated for
184  // "name_".
185  BOOST_STATIC_ASSERT(MAX_LOGGER_NAME_SIZE < sizeof(name_));
186 
187  // Do the copy, ensuring a trailing NULL in all cases.
188  std::strncpy(name_, name, MAX_LOGGER_NAME_SIZE);
189  name_[MAX_LOGGER_NAME_SIZE] = '\0';
190  }
191 
193  virtual ~Logger();
194 
196  static std::string getVersion();
197 
200 
204  virtual std::string getName();
205 
215  virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
216 
222 
229 
234  virtual int getDebugLevel();
235 
241  virtual int getEffectiveDebugLevel();
242 
248  virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
249 
251  virtual bool isInfoEnabled();
252 
254  virtual bool isWarnEnabled();
255 
257  virtual bool isErrorEnabled();
258 
260  virtual bool isFatalEnabled();
261 
267  Formatter debug(int dbglevel, const MessageID& ident);
268 
272  Formatter info(const MessageID& ident);
273 
277  Formatter warn(const MessageID& ident);
278 
282  Formatter error(const MessageID& ident);
283 
287  Formatter fatal(const MessageID& ident);
288 
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  if (!loggerptr_) {
349  initLoggerImpl();
350  }
351  return (loggerptr_);
352  }
353 
355  void initLoggerImpl();
356 
357  LoggerImpl* loggerptr_;
358  char name_[MAX_LOGGER_NAME_SIZE + 1];
359 };
360 
361 } // namespace log
362 } // namespace isc
363 
364 
365 #endif // LOGGER_H
Logger(const char *name)
Constructor.
Definition: log/logger.h:165
Logger Class.
Definition: log/logger.h:143
virtual bool isWarnEnabled()
Is WARNING Enabled?
Definition: log/logger.cc:109
virtual bool isErrorEnabled()
Is ERROR Enabled?
Definition: log/logger.cc:114
virtual bool isInfoEnabled()
Is INFO Enabled?
Definition: log/logger.cc:104
Bad Interprocess Sync.
Definition: log/logger.h:93
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Formatter fatal(const MessageID &ident)
Output Fatal Message.
Definition: log/logger.cc:178
virtual isc::log::Severity getSeverity()
Get Severity Level for Logger.
Definition: log/logger.cc:70
virtual ~Logger()
Destructor.
Definition: log/logger.cc:38
Logger Name is Null.
Definition: log/logger.h:113
#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:138
const int MIN_DEBUG_LEVEL
Minimum/maximum debug levels.
Definition: logger_level.h:35
Console Logger Implementation.
Definition: logger_impl.h:57
Severity
Severity Levels.
Definition: logger_level.h:23
Logging Not Initialized.
Definition: log/logger.h:124
virtual bool isFatalEnabled()
Is FATAL Enabled?
Definition: log/logger.cc:119
virtual bool isDebugEnabled(int dbglevel=MIN_DEBUG_LEVEL)
Returns if Debug Message Should Be Output.
Definition: log/logger.cc:99
LoggingNotInitialized(const char *file, size_t line, const char *what)
Definition: log/logger.h:126
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:103
static const size_t MAX_LOGGER_NAME_SIZE
Maximum size of a logger name.
Definition: log/logger.h:146
The log message formatter.
virtual int getDebugLevel()
Return DEBUG Level.
Definition: log/logger.cc:84
virtual int getEffectiveDebugLevel()
Get Effective Debug Level for Logger.
Definition: log/logger.cc:92
Formatter error(const MessageID &ident)
Output Error Message.
Definition: log/logger.cc:168
const Name & name_
Definition: dns/message.cc:693
BadInterprocessSync(const char *file, size_t line, const char *what)
Definition: log/logger.h:95
Formatter warn(const MessageID &ident)
Output Warning Message.
Definition: log/logger.cc:158
virtual void setSeverity(isc::log::Severity severity, int dbglevel=1)
Set Severity Level for Logger.
Definition: log/logger.cc:63
static std::string getVersion()
Version.
Definition: log/logger.cc:49
LoggerNameError(const char *file, size_t line, const char *what)
Definition: log/logger.h:105
isc::log::Formatter< Logger > Formatter
The formatter used to replace placeholders.
Definition: log/logger.h:199
virtual std::string getName()
Get Name of Logger.
Definition: log/logger.cc:56
virtual isc::log::Severity getEffectiveSeverity()
Get Effective Severity Level for Logger.
Definition: log/logger.cc:77
Formatter info(const MessageID &ident)
Output Informational Message.
Definition: log/logger.cc:148
const char * MessageID
Definition: message_types.h:15
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition: log/logger.cc:190
bool operator==(Logger &other)
Equality.
Definition: log/logger.cc:197
Formatter & operator=(const Formatter &other)
Assignment operator.
LoggerNameNull(const char *file, size_t line, const char *what)
Definition: log/logger.h:115