Kea  1.9.9-git
log/logger.cc
Go to the documentation of this file.
1 // Copyright (C) 2011-2020 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>
16 #include <log/message_dictionary.h>
17 #include <log/message_types.h>
18 
19 #include <util/strutil.h>
20 
21 using namespace std;
22 
23 namespace isc {
24 namespace log {
25 
26 LoggerImpl*
27 Logger::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.
39 void
40 Logger::initLoggerImpl() {
41  if (isLoggingInitialized()) {
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 
51 Logger::~Logger() {
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
61 std::string
62 Logger::getVersion() {
63  return (LoggerImpl::getVersion());
64 }
65 
66 // Get Name of Logger
67 
68 std::string
69 Logger::getName() {
70  return (getLoggerPtr()->getName());
71 }
72 
73 // Set the severity for logging.
74 
75 void
76 Logger::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 
90 Logger::getEffectiveSeverity() {
91  return (getLoggerPtr()->getEffectiveSeverity());
92 }
93 
94 // Debug level (only relevant if messages of severity DEBUG are being logged).
95 
96 int
97 Logger::getDebugLevel() {
98  return (getLoggerPtr()->getDebugLevel());
99 }
100 
101 // Effective debug level (only relevant if messages of severity DEBUG are being
102 // logged).
103 
104 int
105 Logger::getEffectiveDebugLevel() {
106  return (getLoggerPtr()->getEffectiveDebugLevel());
107 }
108 
109 // Check on the current severity settings
110 
111 bool
112 Logger::isDebugEnabled(int dbglevel) {
113  return (getLoggerPtr()->isDebugEnabled(dbglevel));
114 }
115 
116 bool
117 Logger::isInfoEnabled() {
118  return (getLoggerPtr()->isInfoEnabled());
119 }
120 
121 bool
122 Logger::isWarnEnabled() {
123  return (getLoggerPtr()->isWarnEnabled());
124 }
125 
126 bool
127 Logger::isErrorEnabled() {
128  return (getLoggerPtr()->isErrorEnabled());
129 }
130 
131 bool
132 Logger::isFatalEnabled() {
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 
145 void
146 Logger::output(const Severity& severity, const std::string& message) {
147  getLoggerPtr()->outputRaw(severity, message);
148 }
149 
150 Logger::Formatter
151 Logger::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 
202 void
203 Logger::setInterprocessSync(isc::log::interprocess::InterprocessSync* sync) {
204  getLoggerPtr()->setInterprocessSync(sync);
205 }
206 
207 // Comparison (testing only)
208 
209 bool
211  return (*getLoggerPtr() == *other.getLoggerPtr());
212 }
213 
214 } // namespace log
215 } // namespace isc
Logger Class.
Definition: log/logger.h:141
isc::log::Severity getSeverity(const std::string &sev_str)
Returns the isc::log::Severity value represented by the given string.
Definition: logger_level.cc:20
STL namespace.
bool operator==(const Element &a, const Element &b)
Definition: data.cc:210
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Severity
Severity Levels.
Definition: logger_level.h:23
bool isLoggingInitialized()
Is logging initialized?
Defines the logger used by the top-level component of kea-dhcp-ddns.
The log message formatter.
Logging initialization functions.
const Name & name_
Definition: dns/message.cc:693
const char * MessageID
Definition: message_types.h:15