Kea 2.5.9
log/logger.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2024 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#include <config.h>
8
9#include <stdarg.h>
10#include <stdio.h>
11
12#include <log/logger.h>
13#include <log/logger_impl.h>
14#include <log/logger_name.h>
15#include <log/logger_support.h>
17#include <log/message_types.h>
18
19using namespace std;
20
21namespace isc {
22namespace log {
23
24LoggerImpl*
25Logger::getLoggerPtr() {
26 if (!initialized_) {
27 lock_guard<mutex> lk(mutex_);
28 if (!initialized_) {
29 initLoggerImpl();
30 }
31 initialized_ = true;
32 }
33 return (loggerptr_);
34}
35
36// Initialize underlying logger, but only if logging has been initialized.
37void
38Logger::initLoggerImpl() {
40 loggerptr_ = new LoggerImpl(name_);
41 } else {
42 isc_throw(LoggingNotInitialized, "attempt to access logging function "
43 "before logging has been initialized");
44 }
45}
46
47// Destructor.
48
50 delete loggerptr_;
51
52 // The next statement is required for the Kea hooks framework, where a
53 // statically-linked Kea loads and unloads multiple libraries. See the hooks
54 // documentation for more details.
55 loggerptr_ = 0;
56}
57
58// Get Version
59std::string
61 return (LoggerImpl::getVersion());
62}
63
64// Get Name of Logger
65
66std::string
68 return (getLoggerPtr()->getName());
69}
70
71// Set the severity for logging.
72
73void
74Logger::setSeverity(isc::log::Severity severity, int dbglevel) {
75 getLoggerPtr()->setSeverity(severity, dbglevel);
76}
77
78// Return the severity of the logger.
79
82 return (getLoggerPtr()->getSeverity());
83}
84
85// Get Effective Severity Level for Logger
86
89 return (getLoggerPtr()->getEffectiveSeverity());
90}
91
92// Debug level (only relevant if messages of severity DEBUG are being logged).
93
94int
96 return (getLoggerPtr()->getDebugLevel());
97}
98
99// Effective debug level (only relevant if messages of severity DEBUG are being
100// logged).
101
102int
104 return (getLoggerPtr()->getEffectiveDebugLevel());
105}
106
107// Check on the current severity settings
108
109bool
111 return (getLoggerPtr()->isDebugEnabled(dbglevel));
112}
113
114bool
116 return (getLoggerPtr()->isInfoEnabled());
117}
118
119bool
121 return (getLoggerPtr()->isWarnEnabled());
122}
123
124bool
126 return (getLoggerPtr()->isErrorEnabled());
127}
128
129bool
131 return (getLoggerPtr()->isFatalEnabled());
132}
133
134// Format a message: looks up the message text in the dictionary and formats
135// it, replacing tokens with arguments.
136//
137// Owing to the use of variable arguments, this must be inline (hence the
138// definition of the macro). Also note that it expects that the message buffer
139// "message" is declared in the compilation unit.
140
141// Output methods
142
143void
144Logger::output(const Severity& severity, const std::string& message) {
145 getLoggerPtr()->outputRaw(severity, message);
146}
147
149Logger::debug(int dbglevel, const isc::log::MessageID& ident) {
150 if (isDebugEnabled(dbglevel)) {
151 return (Formatter(DEBUG, getLoggerPtr()->lookupMessage(ident),
152 this));
153 } else {
154 return (Formatter());
155 }
156}
157
160 if (isInfoEnabled()) {
161 return (Formatter(INFO, getLoggerPtr()->lookupMessage(ident),
162 this));
163 } else {
164 return (Formatter());
165 }
166}
167
170 if (isWarnEnabled()) {
171 return (Formatter(WARN, getLoggerPtr()->lookupMessage(ident),
172 this));
173 } else {
174 return (Formatter());
175 }
176}
177
180 if (isErrorEnabled()) {
181 return (Formatter(ERROR, getLoggerPtr()->lookupMessage(ident),
182 this));
183 } else {
184 return (Formatter());
185 }
186}
187
190 if (isFatalEnabled()) {
191 return (Formatter(FATAL, getLoggerPtr()->lookupMessage(ident),
192 this));
193 } else {
194 return (Formatter());
195 }
196}
197
198// Replace the interprocess synchronization object
199
200void
202 getLoggerPtr()->setInterprocessSync(sync);
203}
204
205bool
207 return getLoggerPtr()->hasAppender(destination);
208}
209
210// Comparison (testing only)
211
212bool
214 return (*getLoggerPtr() == *other.getLoggerPtr());
215}
216
217} // namespace log
218} // namespace isc
The log message formatter.
void outputRaw(const Severity &severity, const std::string &message)
Raw output.
Definition: logger_impl.cc:154
virtual void setSeverity(Severity severity, int dbglevel=1)
Set Severity Level for Logger.
Definition: logger_impl.cc:97
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition: logger_impl.cc:143
bool hasAppender(OutputOption::Destination const destination)
Check if this logger has an appender of the given type.
Definition: logger_impl.cc:203
static std::string getVersion()
Version.
Definition: logger_impl.cc:88
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
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
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
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Logging initialization functions.
bool isLoggingInitialized()
Is logging initialized?
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