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:
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  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
isc::log::interprocess::InterprocessSync
Interprocess Sync Class.
Definition: interprocess_sync.h:38
isc::log::Logger::getEffectiveDebugLevel
virtual int getEffectiveDebugLevel()
Get Effective Debug Level for Logger.
Definition: log/logger.cc:92
isc::log::Logger::isErrorEnabled
virtual bool isErrorEnabled()
Is ERROR Enabled?
Definition: log/logger.cc:114
isc::log::Logger
Logger Class.
Definition: log/logger.h:143
isc::log::LoggerNameNull
Logger Name is Null.
Definition: log/logger.h:113
isc::log::BadInterprocessSync::BadInterprocessSync
BadInterprocessSync(const char *file, size_t line, const char *what)
Definition: log/logger.h:95
isc::log::Logger::setInterprocessSync
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition: log/logger.cc:190
isc::log::Logger::getDebugLevel
virtual int getDebugLevel()
Return DEBUG Level.
Definition: log/logger.cc:84
isc::log::Logger::getVersion
static std::string getVersion()
Version.
Definition: log/logger.cc:49
isc::log::Logger::error
Formatter error(const MessageID &ident)
Output Error Message.
Definition: log/logger.cc:168
logger_level.h
isc::log::LoggingNotInitialized::LoggingNotInitialized
LoggingNotInitialized(const char *file, size_t line, const char *what)
Definition: log/logger.h:126
isc::log::LoggerNameError
Logger Name Error.
Definition: log/logger.h:103
isc::log::Severity
Severity
Severity Levels.
Definition: logger_level.h:23
isc::log::Logger::getEffectiveSeverity
virtual isc::log::Severity getEffectiveSeverity()
Get Effective Severity Level for Logger.
Definition: log/logger.cc:77
isc::log::Logger::isWarnEnabled
virtual bool isWarnEnabled()
Is WARNING Enabled?
Definition: log/logger.cc:109
isc::log::Logger::debug
Formatter debug(int dbglevel, const MessageID &ident)
Output Debug Message.
Definition: log/logger.cc:138
isc::log::Logger::isDebugEnabled
virtual bool isDebugEnabled(int dbglevel=MIN_DEBUG_LEVEL)
Returns if Debug Message Should Be Output.
Definition: log/logger.cc:99
isc::log::LoggerNameNull::LoggerNameNull
LoggerNameNull(const char *file, size_t line, const char *what)
Definition: log/logger.h:115
isc::log::Logger::Logger
Logger(const char *name)
Constructor.
Definition: log/logger.h:165
isc::log::Formatter::operator=
Formatter & operator=(const Formatter &other)
Assignment operator.
Definition: log_formatter.h:172
isc::log::Logger::getSeverity
virtual isc::log::Severity getSeverity()
Get Severity Level for Logger.
Definition: log/logger.cc:70
isc::log::Logger::~Logger
virtual ~Logger()
Destructor.
Definition: log/logger.cc:38
log_formatter.h
isc::log::LoggingNotInitialized
Logging Not Initialized.
Definition: log/logger.h:124
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::log::LoggerImpl
Console Logger Implementation.
Definition: logger_impl.h:57
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::log::Logger::MAX_LOGGER_NAME_SIZE
static const size_t MAX_LOGGER_NAME_SIZE
Maximum size of a logger name.
Definition: log/logger.h:146
isc::log::Logger::setSeverity
virtual void setSeverity(isc::log::Severity severity, int dbglevel=1)
Set Severity Level for Logger.
Definition: log/logger.cc:63
isc::log::Logger::info
Formatter info(const MessageID &ident)
Output Informational Message.
Definition: log/logger.cc:148
message_types.h
isc::log::Logger::Formatter
isc::log::Formatter< Logger > Formatter
The formatter used to replace placeholders.
Definition: log/logger.h:199
isc::log::Logger::isInfoEnabled
virtual bool isInfoEnabled()
Is INFO Enabled?
Definition: log/logger.cc:104
isc::log::Logger::isFatalEnabled
virtual bool isFatalEnabled()
Is FATAL Enabled?
Definition: log/logger.cc:119
isc::log::MessageID
const char * MessageID
Definition: message_types.h:15
exceptions.h
name_
const Name & name_
Definition: dns/message.cc:693
isc::log::Logger::warn
Formatter warn(const MessageID &ident)
Output Warning Message.
Definition: log/logger.cc:158
isc::log::BadInterprocessSync
Bad Interprocess Sync.
Definition: log/logger.h:93
isc::log::Logger::operator==
bool operator==(Logger &other)
Equality.
Definition: log/logger.cc:197
isc::log::Formatter
The log message formatter.
Definition: log_formatter.h:102
isc::log::LoggerNameError::LoggerNameError
LoggerNameError(const char *file, size_t line, const char *what)
Definition: log/logger.h:105
isc::log::Logger::getName
virtual std::string getName()
Get Name of Logger.
Definition: log/logger.cc:56
isc::log::Logger::fatal
Formatter fatal(const MessageID &ident)
Output Fatal Message.
Definition: log/logger.cc:178
isc::log::MIN_DEBUG_LEVEL
const int MIN_DEBUG_LEVEL
Minimum/maximum debug levels.
Definition: logger_level.h:35