Kea 2.7.8
log/logger.h
Go to the documentation of this file.
1// Copyright (C) 2011-2022 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
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
25namespace interprocess {
26// Forward declaration to hide implementation details from normal
27// applications.
29}
30
85
86class LoggerImpl; // Forward declaration of the implementation class
87
93public:
94 BadInterprocessSync(const char* file, size_t line, const char* what) :
95 isc::Exception(file, line, what)
96 {}
97};
98
103public:
104 LoggerNameError(const char* file, size_t line, const char* what) :
105 isc::Exception(file, line, what)
106 {}
107};
108
113public:
114 LoggerNameNull(const char* file, size_t line, const char* what) :
115 isc::Exception(file, line, what)
116 {}
117};
118
124public:
125 LoggingNotInitialized(const char* file, size_t line, const char* what) :
126 isc::Exception(file, line, what)
127 {}
128};
129
141
142class 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
195
199 virtual std::string getName();
200
210 virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
211
217
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
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
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Bad Interprocess Sync.
Definition log/logger.h:92
BadInterprocessSync(const char *file, size_t line, const char *what)
Definition log/logger.h:94
The log message formatter.
Console Logger Implementation.
Definition logger_impl.h:59
Logger Name Error.
Definition log/logger.h:102
LoggerNameError(const char *file, size_t line, const char *what)
Definition log/logger.h:104
Logger Name is null.
Definition log/logger.h:112
LoggerNameNull(const char *file, size_t line, const char *what)
Definition log/logger.h:114
Logger Class.
Definition log/logger.h:142
Formatter error(const MessageID &ident)
Output Error Message.
Formatter info(const MessageID &ident)
Output Informational Message.
Formatter warn(const MessageID &ident)
Output Warning Message.
virtual void setSeverity(isc::log::Severity severity, int dbglevel=1)
Set Severity Level for Logger.
Definition log/logger.cc:74
static const size_t MAX_LOGGER_NAME_SIZE
Maximum size of a logger name.
Definition log/logger.h:145
isc::log::Formatter< Logger > Formatter
The formatter used to replace placeholders.
Definition log/logger.h:194
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Formatter debug(int dbglevel, const MessageID &ident)
Output Debug Message.
virtual int getEffectiveDebugLevel()
Get Effective Debug Level for Logger.
virtual isc::log::Severity getEffectiveSeverity()
Get Effective Severity Level for Logger.
Definition log/logger.cc:88
virtual ~Logger()
Destructor.
Definition log/logger.cc:49
virtual isc::log::Severity getSeverity()
Get Severity Level for Logger.
Definition log/logger.cc:81
virtual bool isWarnEnabled()
Is WARNING Enabled?
virtual bool isFatalEnabled()
Is FATAL Enabled?
Logger(const char *name)
Constructor.
Definition log/logger.h:166
Formatter fatal(const MessageID &ident)
Output Fatal Message.
virtual bool isDebugEnabled(int dbglevel=MIN_DEBUG_LEVEL)
Returns if Debug Message Should Be Output.
virtual bool isInfoEnabled()
Is INFO Enabled?
bool operator==(Logger &other)
Equality.
virtual std::string getName()
Get Name of Logger.
Definition log/logger.cc:67
virtual bool isErrorEnabled()
Is ERROR Enabled?
static std::string getVersion()
Version.
Definition log/logger.cc:60
virtual int getDebugLevel()
Return DEBUG Level.
Definition log/logger.cc:95
bool hasAppender(OutputOption::Destination const destination)
Check if this logger has an appender of the given type.
Logging Not Initialized.
Definition log/logger.h:123
LoggingNotInitialized(const char *file, size_t line, const char *what)
Definition log/logger.h:125
const Name & name_
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
const int MIN_DEBUG_LEVEL
Minimum/maximum debug levels.
const char * MessageID
Severity
Severity Levels.
Defines the logger used by the top-level component of kea-lfc.