Kea: logger.h Source File

| Kea 3.2.0-git

log/logger.h
Go to the documentation of this file.
1// Copyright (C) 2011-2025 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
22namespace isc {
23namespace log {
24
25 namespace interprocess {
26// Forward declaration to hide implementation details from normal
27// applications.
28class InterprocessSync;
29}
30
85
86class LoggerImpl; // Forward declaration of the implementation class
87
92 class BadInterprocessSync : public isc::Exception {
93public:
94 BadInterprocessSync(const char* file, size_t line, const char* what) :
95 isc::Exception(file, line, what)
96 {}
97};
98
102 class LoggerNameError : public isc::Exception {
103public:
104 LoggerNameError(const char* file, size_t line, const char* what) :
105 isc::Exception(file, line, what)
106 {}
107};
108
112 class LoggerNameNull : public isc::Exception {
113public:
114 LoggerNameNull(const char* file, size_t line, const char* what) :
115 isc::Exception(file, line, what)
116 {}
117};
118
123 class LoggingNotInitialized : public isc::Exception {
124public:
125 LoggingNotInitialized(const char* file, size_t line, const char* what) :
126 isc::Exception(file, line, what)
127 {}
128};
129
141
142 class Logger {
143public:
145 static const size_t MAX_LOGGER_NAME_SIZE = 31;
146
166 Logger(const char* name) : loggerptr_(0), initialized_(false) {
167
168 // Validate the name of the logger.
169 if (name) {
170 // Name not null, is it too short or too long?
171 size_t namelen = std::strlen(name);
172 if ((namelen == 0) || (namelen > MAX_LOGGER_NAME_SIZE)) {
173 isc_throw(LoggerNameError, "'" << name << "' is not a valid "
174 << "name for a logger: valid names must be between 1 "
175 << "and " << MAX_LOGGER_NAME_SIZE << " characters in "
176 << "length");
177 }
178 } else {
179 isc_throw(LoggerNameNull, "logger names may not be null");
180 }
181
182 // Do the copy, ensuring a trailing null in all cases.
183 std::strncpy(name_, name, MAX_LOGGER_NAME_SIZE);
184 name_[MAX_LOGGER_NAME_SIZE] = '\0';
185 }
186
188 virtual ~Logger();
189
191 static std::string getVersion();
192
194 typedef isc::log::Formatter<Logger> Formatter;
195
199 virtual std::string getName();
200
210 virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
211
216 virtual isc::log::Severity getSeverity();
217
223 virtual isc::log::Severity getEffectiveSeverity();
224
229 virtual int getDebugLevel();
230
236 virtual int getEffectiveDebugLevel();
237
243 virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
244
246 virtual bool isInfoEnabled();
247
249 virtual bool isWarnEnabled();
250
252 virtual bool isErrorEnabled();
253
255 virtual bool isFatalEnabled();
256
262 Formatter debug(int dbglevel, const MessageID& ident);
263
267 Formatter info(const MessageID& ident);
268
272 Formatter warn(const MessageID& ident);
273
277 Formatter error(const MessageID& ident);
278
282 Formatter fatal(const MessageID& ident);
283
298 void setInterprocessSync(isc::log::interprocess::InterprocessSync* sync);
299
305 bool hasAppender(OutputOption::Destination const destination);
306
312 bool operator==(Logger& other);
313
314private:
315 friend class isc::log::Formatter<Logger>;
316
323 void output(const Severity& severity, const std::string& message);
324
329 Logger(const Logger&);
330
335 Logger& operator=(const Logger&);
336
349 LoggerImpl* getLoggerPtr();
350
352 void initLoggerImpl();
353
355 LoggerImpl* loggerptr_;
356
358 char name_[MAX_LOGGER_NAME_SIZE + 1];
359
361 std::mutex mutex_;
362
364 std::atomic<bool> initialized_;
365};
366
367} // namespace log
368} // namespace isc
369
370
371#endif // LOGGER_H
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition exceptions/exceptions.h:23
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
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::log::BadInterprocessSync::BadInterprocessSync
BadInterprocessSync(const char *file, size_t line, const char *what)
Definition log/logger.h:94
isc::log::Formatter
The log message formatter.
Definition log_formatter.h:105
isc::log::LoggerImpl
Console Logger Implementation.
Definition logger_impl.h:59
isc::log::LoggerNameError
Logger Name Error.
Definition log/logger.h:102
isc::log::LoggerNameError::LoggerNameError
LoggerNameError(const char *file, size_t line, const char *what)
Definition log/logger.h:104
isc::log::LoggerNameNull
Logger Name is null.
Definition log/logger.h:112
isc::log::LoggerNameNull::LoggerNameNull
LoggerNameNull(const char *file, size_t line, const char *what)
Definition log/logger.h:114
isc::log::Logger::error
Formatter error(const MessageID &ident)
Output Error Message.
Definition log/logger.cc:179
isc::log::Logger::info
Formatter info(const MessageID &ident)
Output Informational Message.
Definition log/logger.cc:159
isc::log::Logger::Formatter
isc::log::Formatter< Logger > Formatter
The formatter used to replace placeholders.
Definition log/logger.h:194
isc::log::Logger::warn
Formatter warn(const MessageID &ident)
Output Warning Message.
Definition log/logger.cc:169
isc::log::Logger::setSeverity
virtual void setSeverity(isc::log::Severity severity, int dbglevel=1)
Set Severity Level for Logger.
Definition log/logger.cc:74
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:145
isc::log::Logger::setInterprocessSync
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition log/logger.cc:201
isc::log::Logger::debug
Formatter debug(int dbglevel, const MessageID &ident)
Output Debug Message.
Definition log/logger.cc:149
isc::log::Logger::getEffectiveDebugLevel
virtual int getEffectiveDebugLevel()
Get Effective Debug Level for Logger.
Definition log/logger.cc:103
isc::log::Logger::getEffectiveSeverity
virtual isc::log::Severity getEffectiveSeverity()
Get Effective Severity Level for Logger.
Definition log/logger.cc:88
isc::log::Logger::~Logger
virtual ~Logger()
Destructor.
Definition log/logger.cc:49
isc::log::Logger::getSeverity
virtual isc::log::Severity getSeverity()
Get Severity Level for Logger.
Definition log/logger.cc:81
isc::log::Logger::isWarnEnabled
virtual bool isWarnEnabled()
Is WARNING Enabled?
Definition log/logger.cc:120
isc::log::Logger::isFatalEnabled
virtual bool isFatalEnabled()
Is FATAL Enabled?
Definition log/logger.cc:130
isc::log::Logger::Logger
Logger(const char *name)
Constructor.
Definition log/logger.h:166
isc::log::Logger::fatal
Formatter fatal(const MessageID &ident)
Output Fatal Message.
Definition log/logger.cc:189
isc::log::Logger::isDebugEnabled
virtual bool isDebugEnabled(int dbglevel=MIN_DEBUG_LEVEL)
Returns if Debug Message Should Be Output.
Definition log/logger.cc:110
isc::log::Logger::isInfoEnabled
virtual bool isInfoEnabled()
Is INFO Enabled?
Definition log/logger.cc:115
isc::log::Logger::operator==
bool operator==(Logger &other)
Equality.
Definition log/logger.cc:213
isc::log::Logger::getName
virtual std::string getName()
Get Name of Logger.
Definition log/logger.cc:67
isc::log::Logger::isErrorEnabled
virtual bool isErrorEnabled()
Is ERROR Enabled?
Definition log/logger.cc:125
isc::log::Logger::getVersion
static std::string getVersion()
Version.
Definition log/logger.cc:60
isc::log::Logger::getDebugLevel
virtual int getDebugLevel()
Return DEBUG Level.
Definition log/logger.cc:95
isc::log::Logger::hasAppender
bool hasAppender(OutputOption::Destination const destination)
Check if this logger has an appender of the given type.
Definition log/logger.cc:206
isc::log::LoggingNotInitialized::LoggingNotInitialized
LoggingNotInitialized(const char *file, size_t line, const char *what)
Definition log/logger.h:125
isc::log::interprocess::InterprocessSync
Interprocess Sync Class.
Definition interprocess_sync.h:38
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
log_formatter.h
logger_level.h
message_types.h
isc::log::interprocess
Definition log/logger.h:25
isc::log
Definition buffer_appender_impl.cc:17
isc::log::MIN_DEBUG_LEVEL
const int MIN_DEBUG_LEVEL
Minimum/maximum debug levels.
Definition logger_level.h:35
isc::log::MessageID
const char * MessageID
Definition message_types.h:15
isc::log::Severity
Severity
Severity Levels.
Definition logger_level.h:23
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
output_option.h
isc::log::OutputOption::Destination
Destination
Destinations.
Definition output_option.h:48