Kea 2.5.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
164 Logger(const char* name) : loggerptr_(0), initialized_(false) {
165
166 // Validate the name of the logger.
167 if (name) {
168 // Name not null, is it too short or too long?
169 size_t namelen = std::strlen(name);
170 if ((namelen == 0) || (namelen > MAX_LOGGER_NAME_SIZE)) {
171 isc_throw(LoggerNameError, "'" << name << "' is not a valid "
172 << "name for a logger: valid names must be between 1 "
173 << "and " << MAX_LOGGER_NAME_SIZE << " characters in "
174 << "length");
175 }
176 } else {
177 isc_throw(LoggerNameNull, "logger names may not be null");
178 }
179
180 // Do the copy, ensuring a trailing null in all cases.
181 std::strncpy(name_, name, MAX_LOGGER_NAME_SIZE);
182 name_[MAX_LOGGER_NAME_SIZE] = '\0';
183 }
184
186 virtual ~Logger();
187
189 static std::string getVersion();
190
193
197 virtual std::string getName();
198
208 virtual void setSeverity(isc::log::Severity severity, int dbglevel = 1);
209
215
222
227 virtual int getDebugLevel();
228
234 virtual int getEffectiveDebugLevel();
235
241 virtual bool isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL);
242
244 virtual bool isInfoEnabled();
245
247 virtual bool isWarnEnabled();
248
250 virtual bool isErrorEnabled();
251
253 virtual bool isFatalEnabled();
254
260 Formatter debug(int dbglevel, const MessageID& ident);
261
265 Formatter info(const MessageID& ident);
266
270 Formatter warn(const MessageID& ident);
271
275 Formatter error(const MessageID& ident);
276
280 Formatter fatal(const MessageID& ident);
281
297
303 bool hasAppender(OutputOption::Destination const destination);
304
310 bool operator==(Logger& other);
311
312private:
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
350 void initLoggerImpl();
351
353 LoggerImpl* loggerptr_;
354
356 char name_[MAX_LOGGER_NAME_SIZE + 1];
357
359 std::mutex mutex_;
360
362 std::atomic<bool> initialized_;
363};
364
365} // namespace log
366} // namespace isc
367
368
369#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.
Definition: log/logger.cc:179
Formatter info(const MessageID &ident)
Output Informational Message.
Definition: log/logger.cc:159
isc::log::Formatter< Logger > Formatter
The formatter used to replace placeholders.
Definition: log/logger.h:192
Formatter warn(const MessageID &ident)
Output Warning Message.
Definition: log/logger.cc:169
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
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition: log/logger.cc:201
Formatter debug(int dbglevel, const MessageID &ident)
Output Debug Message.
Definition: log/logger.cc:149
virtual int getEffectiveDebugLevel()
Get Effective Debug Level for Logger.
Definition: log/logger.cc:103
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?
Definition: log/logger.cc:120
virtual bool isFatalEnabled()
Is FATAL Enabled?
Definition: log/logger.cc:130
Logger(const char *name)
Constructor.
Definition: log/logger.h:164
Formatter fatal(const MessageID &ident)
Output Fatal Message.
Definition: log/logger.cc:189
virtual bool isDebugEnabled(int dbglevel=MIN_DEBUG_LEVEL)
Returns if Debug Message Should Be Output.
Definition: log/logger.cc:110
virtual bool isInfoEnabled()
Is INFO Enabled?
Definition: log/logger.cc:115
bool operator==(Logger &other)
Equality.
Definition: log/logger.cc:213
virtual std::string getName()
Get Name of Logger.
Definition: log/logger.cc:67
virtual bool isErrorEnabled()
Is ERROR Enabled?
Definition: log/logger.cc:125
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.
Definition: log/logger.cc:206
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_
Definition: dns/message.cc:696
#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
const char * MessageID
Definition: message_types.h:15
Severity
Severity Levels.
Definition: logger_level.h:23
Defines the logger used by the top-level component of kea-lfc.
Destination
Destinations.
Definition: output_option.h:48