Kea 2.5.7
log/logger.cc
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#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
19#include <util/strutil.h>
20
21using namespace std;
22
23namespace isc {
24namespace log {
25
26LoggerImpl*
27Logger::getLoggerPtr() {
28 if (!initialized_) {
29 lock_guard<mutex> lk(mutex_);
30 if (!initialized_) {
31 initLoggerImpl();
32 }
33 initialized_ = true;
34 }
35 return (loggerptr_);
36}
37
38// Initialize underlying logger, but only if logging has been initialized.
39void
40Logger::initLoggerImpl() {
42 loggerptr_ = new LoggerImpl(name_);
43 } else {
44 isc_throw(LoggingNotInitialized, "attempt to access logging function "
45 "before logging has been initialized");
46 }
47}
48
49// Destructor.
50
52 delete loggerptr_;
53
54 // The next statement is required for the Kea hooks framework, where a
55 // statically-linked Kea loads and unloads multiple libraries. See the hooks
56 // documentation for more details.
57 loggerptr_ = 0;
58}
59
60// Get Version
61std::string
63 return (LoggerImpl::getVersion());
64}
65
66// Get Name of Logger
67
68std::string
70 return (getLoggerPtr()->getName());
71}
72
73// Set the severity for logging.
74
75void
76Logger::setSeverity(isc::log::Severity severity, int dbglevel) {
77 getLoggerPtr()->setSeverity(severity, dbglevel);
78}
79
80// Return the severity of the logger.
81
84 return (getLoggerPtr()->getSeverity());
85}
86
87// Get Effective Severity Level for Logger
88
91 return (getLoggerPtr()->getEffectiveSeverity());
92}
93
94// Debug level (only relevant if messages of severity DEBUG are being logged).
95
96int
98 return (getLoggerPtr()->getDebugLevel());
99}
100
101// Effective debug level (only relevant if messages of severity DEBUG are being
102// logged).
103
104int
106 return (getLoggerPtr()->getEffectiveDebugLevel());
107}
108
109// Check on the current severity settings
110
111bool
113 return (getLoggerPtr()->isDebugEnabled(dbglevel));
114}
115
116bool
118 return (getLoggerPtr()->isInfoEnabled());
119}
120
121bool
123 return (getLoggerPtr()->isWarnEnabled());
124}
125
126bool
128 return (getLoggerPtr()->isErrorEnabled());
129}
130
131bool
133 return (getLoggerPtr()->isFatalEnabled());
134}
135
136// Format a message: looks up the message text in the dictionary and formats
137// it, replacing tokens with arguments.
138//
139// Owing to the use of variable arguments, this must be inline (hence the
140// definition of the macro). Also note that it expects that the message buffer
141// "message" is declared in the compilation unit.
142
143// Output methods
144
145void
146Logger::output(const Severity& severity, const std::string& message) {
147 getLoggerPtr()->outputRaw(severity, message);
148}
149
151Logger::debug(int dbglevel, const isc::log::MessageID& ident) {
152 if (isDebugEnabled(dbglevel)) {
153 return (Formatter(DEBUG, getLoggerPtr()->lookupMessage(ident),
154 this));
155 } else {
156 return (Formatter());
157 }
158}
159
162 if (isInfoEnabled()) {
163 return (Formatter(INFO, getLoggerPtr()->lookupMessage(ident),
164 this));
165 } else {
166 return (Formatter());
167 }
168}
169
172 if (isWarnEnabled()) {
173 return (Formatter(WARN, getLoggerPtr()->lookupMessage(ident),
174 this));
175 } else {
176 return (Formatter());
177 }
178}
179
182 if (isErrorEnabled()) {
183 return (Formatter(ERROR, getLoggerPtr()->lookupMessage(ident),
184 this));
185 } else {
186 return (Formatter());
187 }
188}
189
192 if (isFatalEnabled()) {
193 return (Formatter(FATAL, getLoggerPtr()->lookupMessage(ident),
194 this));
195 } else {
196 return (Formatter());
197 }
198}
199
200// Replace the interprocess synchronization object
201
202void
204 getLoggerPtr()->setInterprocessSync(sync);
205}
206
207bool
209 return getLoggerPtr()->hasAppender(destination);
210}
211
212// Comparison (testing only)
213
214bool
216 return (*getLoggerPtr() == *other.getLoggerPtr());
217}
218
219} // namespace log
220} // namespace isc
The log message formatter.
void outputRaw(const Severity &severity, const std::string &message)
Raw output.
Definition: logger_impl.cc:156
virtual void setSeverity(Severity severity, int dbglevel=1)
Set Severity Level for Logger.
Definition: logger_impl.cc:99
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition: logger_impl.cc:145
bool hasAppender(OutputOption::Destination const destination)
Check if this logger has an appender of the given type.
Definition: logger_impl.cc:205
static std::string getVersion()
Version.
Definition: logger_impl.cc:90
Logger Class.
Definition: log/logger.h:142
Formatter error(const MessageID &ident)
Output Error Message.
Definition: log/logger.cc:181
Formatter info(const MessageID &ident)
Output Informational Message.
Definition: log/logger.cc:161
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:171
virtual void setSeverity(isc::log::Severity severity, int dbglevel=1)
Set Severity Level for Logger.
Definition: log/logger.cc:76
void setInterprocessSync(isc::log::interprocess::InterprocessSync *sync)
Replace the interprocess synchronization object.
Definition: log/logger.cc:203
Formatter debug(int dbglevel, const MessageID &ident)
Output Debug Message.
Definition: log/logger.cc:151
virtual int getEffectiveDebugLevel()
Get Effective Debug Level for Logger.
Definition: log/logger.cc:105
virtual isc::log::Severity getEffectiveSeverity()
Get Effective Severity Level for Logger.
Definition: log/logger.cc:90
virtual ~Logger()
Destructor.
Definition: log/logger.cc:51
virtual isc::log::Severity getSeverity()
Get Severity Level for Logger.
Definition: log/logger.cc:83
virtual bool isWarnEnabled()
Is WARNING Enabled?
Definition: log/logger.cc:122
virtual bool isFatalEnabled()
Is FATAL Enabled?
Definition: log/logger.cc:132
Formatter fatal(const MessageID &ident)
Output Fatal Message.
Definition: log/logger.cc:191
virtual bool isDebugEnabled(int dbglevel=MIN_DEBUG_LEVEL)
Returns if Debug Message Should Be Output.
Definition: log/logger.cc:112
virtual bool isInfoEnabled()
Is INFO Enabled?
Definition: log/logger.cc:117
bool operator==(Logger &other)
Equality.
Definition: log/logger.cc:215
virtual std::string getName()
Get Name of Logger.
Definition: log/logger.cc:69
virtual bool isErrorEnabled()
Is ERROR Enabled?
Definition: log/logger.cc:127
static std::string getVersion()
Version.
Definition: log/logger.cc:62
virtual int getDebugLevel()
Return DEBUG Level.
Definition: log/logger.cc:97
bool hasAppender(OutputOption::Destination const destination)
Check if this logger has an appender of the given type.
Definition: log/logger.cc:208
#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