Kea 3.1.5
logger_level_impl.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2026 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 <algorithm>
10#include <string.h>
11#include <iostream>
12#include <boost/lexical_cast.hpp>
13
14#include <log4cplus/logger.h>
15
16#include <log/logger_level.h>
19#include <log/macros.h>
20
21using namespace log4cplus;
22using namespace std;
23
24namespace {
26}
27
28namespace isc {
29namespace log {
30
31// Convert Kea level to a log4cplus logging level.
32log4cplus::LogLevel
34
35 // Kea logging levels are small integers so we can do a table lookup
36 static const log4cplus::LogLevel log4cplus_levels[] = {
37 log4cplus::NOT_SET_LOG_LEVEL,
38 log4cplus::DEBUG_LOG_LEVEL,
39 log4cplus::INFO_LOG_LEVEL,
40 log4cplus::WARN_LOG_LEVEL,
41 log4cplus::ERROR_LOG_LEVEL,
42 log4cplus::FATAL_LOG_LEVEL,
43 log4cplus::OFF_LOG_LEVEL
44 };
45
46 // ... with compile-time checks to ensure that table indexes are correct.
47 static_assert(static_cast<int>(DEFAULT) == 0,
48 "static_cast<int>(DEFAULT) == 0");
49 static_assert(static_cast<int>(DEBUG) == 1,
50 "static_cast<int>(DEBUG) == 1");
51 static_assert(static_cast<int>(INFO) == 2,
52 "static_cast<int>(INFO) == 2");
53 static_assert(static_cast<int>(WARN) == 3,
54 "static_cast<int>(WARN) == 3");
55 static_assert(static_cast<int>(ERROR) == 4,
56 "static_cast<int>(ERROR) == 4");
57 static_assert(static_cast<int>(FATAL) == 5,
58 "static_cast<int>(FATAL) == 5");
59 static_assert(static_cast<int>(NONE) == 6,
60 "static_cast<int>(NONE) == 6");
61
62 // Do the conversion
63 if (level.severity == DEBUG) {
64
65 // Debug severity, so the log4cplus level returned depends on the
66 // debug level. Silently limit the debug level to the range
67 // MIN_DEBUG_LEVEL to MAX_DEBUG_LEVEL (avoids the hassle of throwing
68 // and catching exceptions and besides, this is for debug information).
69 int limited = std::max(MIN_DEBUG_LEVEL,
70 std::min(level.dbglevel, MAX_DEBUG_LEVEL));
71 LogLevel newlevel = static_cast<int>(DEBUG_LOG_LEVEL -
72 (limited - MIN_DEBUG_LEVEL));
73 return (static_cast<log4cplus::LogLevel>(newlevel));
74
75 } else {
76
77 // Can do a table lookup to speed things up. There is no need to check
78 // that the index is out of range. That the variable is of type
79 // isc::log::Severity ensures that it must be one of the Severity enum
80 // members - conversion of a numeric value to an enum is not permitted.
81 return (log4cplus_levels[level.severity]);
82 }
83}
84
85// Convert log4cplus logging level to Kea debug level. It is up to the
86// caller to validate that the debug level is valid.
88LoggerLevelImpl::convertToBindLevel(const log4cplus::LogLevel loglevel) {
89
90 // Not easy to do a table lookup as the numerical values of log4cplus levels
91 // are quite high.
92 if (loglevel <= log4cplus::NOT_SET_LOG_LEVEL) {
93 return (Level(DEFAULT));
94
95 } else if (loglevel <= log4cplus::DEBUG_LOG_LEVEL) {
96
97 // Debug severity, so extract the debug level from the numeric value.
98 // If outside the limits, change the severity to the level above or
99 // below.
100 int dbglevel = MIN_DEBUG_LEVEL +
101 static_cast<int>(log4cplus::DEBUG_LOG_LEVEL) -
102 static_cast<int>(loglevel);
103 if (dbglevel > MAX_DEBUG_LEVEL) {
104 return (Level(DEFAULT));
105 } else if (dbglevel < MIN_DEBUG_LEVEL) {
106 return (Level(INFO));
107 }
108 return (Level(DEBUG, dbglevel));
109
110 } else if (loglevel <= log4cplus::INFO_LOG_LEVEL) {
111 return (Level(INFO));
112
113 } else if (loglevel <= log4cplus::WARN_LOG_LEVEL) {
114 return (Level(WARN));
115
116 } else if (loglevel <= log4cplus::ERROR_LOG_LEVEL) {
117 return (Level(ERROR));
118
119 } else if (loglevel <= log4cplus::FATAL_LOG_LEVEL) {
120 return (Level(FATAL));
121
122 }
123
124 return (Level(NONE));
125}
126
127
128// Convert string to appropriate logging level
129log4cplus::LogLevel
130LoggerLevelImpl::logLevelFromString(const log4cplus::tstring& level) {
131
132 std::string name = level; // Get to known type
133 size_t length = name.size(); // Length of the string
134
135 if (length < 5) {
136
137 // String can't possibly start DEBUG so we don't know what it is.
138 // As per documentation, return NOT_SET level.
139 return (NOT_SET_LOG_LEVEL);
140 } else {
141 if (strncasecmp(name.c_str(), "DEBUG", 5) == 0) {
142
143 // String starts "DEBUG" (or "debug" or any case mixture). The
144 // rest of the string - if any - should be a number.
145 if (length == 5) {
146
147 // It is plain "DEBUG". Take this as level 0.
148 return (DEBUG_LOG_LEVEL);
149 } else {
150
151 // Try converting the remainder to an integer. The "5" is
152 // the length of the string "DEBUG". Note that if the number
153 // is outside the range of debug levels, it is coerced to the
154 // nearest limit. Thus a level of DEBUG509 will end up as
155 // if DEBUG99 has been specified.
156 try {
157 int dbglevel = boost::lexical_cast<int>(name.substr(5));
158 if (dbglevel < MIN_DEBUG_LEVEL) {
159 LOG_WARN(logger, LOGIMPL_BELOW_MIN_DEBUG).arg(dbglevel)
160 .arg(MIN_DEBUG_LEVEL);
161 dbglevel = MIN_DEBUG_LEVEL;
162
163 } else if (dbglevel > MAX_DEBUG_LEVEL) {
164 LOG_WARN(logger, LOGIMPL_ABOVE_MAX_DEBUG).arg(dbglevel)
165 .arg(MAX_DEBUG_LEVEL);
166 dbglevel = MAX_DEBUG_LEVEL;
167
168 }
169 return convertFromBindLevel(Level(DEBUG, dbglevel));
170 }
171 catch (const boost::bad_lexical_cast&) {
172 LOG_ERROR(logger, LOGIMPL_BAD_DEBUG_STRING).arg(name);
173 return (NOT_SET_LOG_LEVEL);
174 }
175 }
176 } else {
177
178 // Unknown string - return default. Log4cplus will call any other
179 // registered conversion functions to interpret it.
180 return (NOT_SET_LOG_LEVEL);
181 }
182 }
183}
184
185// Convert logging level to string. If the level is a valid debug level,
186// return the string DEBUG, else return the empty string.
187#if LOG4CPLUS_VERSION < LOG4CPLUS_MAKE_VERSION(2, 0, 0)
189#else
191#endif
192LoggerLevelImpl::logLevelToString(log4cplus::LogLevel level) {
193 Level bindlevel = convertToBindLevel(level);
194 Severity& severity = bindlevel.severity;
195 int& dbglevel = bindlevel.dbglevel;
196 static LoggerLevelImpl::LogLevelString debug_ = tstring("DEBUG");
197 static LoggerLevelImpl::LogLevelString empty_ = tstring();
198
199 if ((severity == DEBUG) &&
200 ((dbglevel >= MIN_DEBUG_LEVEL) && (dbglevel <= MAX_DEBUG_LEVEL))) {
201 return (debug_);
202 }
203
204 // Unknown, so return empty string for log4cplus to try other conversion
205 // functions.
206 return (empty_);
207}
208
209// Initialization. Register the conversion functions with the LogLevelManager.
210void
212
213 // Get the singleton log-level manager.
214 LogLevelManager& manager = getLogLevelManager();
215
216 // Register the conversion functions
217 manager.pushFromStringMethod(LoggerLevelImpl::logLevelFromString);
218 manager.pushToStringMethod(LoggerLevelImpl::logLevelToString);
219}
220
221} // namespace log
222} // namespace isc
static log4cplus::LogLevel convertFromBindLevel(const isc::log::Level &level)
Convert Kea level to log4cplus logging level.
static const LogLevelString & logLevelToString(log4cplus::LogLevel level)
Convert log level to string.
static log4cplus::LogLevel logLevelFromString(const log4cplus::tstring &level)
Convert string to log4cplus logging level.
log4cplus::tstring LogLevelString
static isc::log::Level convertToBindLevel(const log4cplus::LogLevel loglevel)
Convert log4cplus logging level to Kea logging level.
static void init()
Initialize extended logging levels.
Logger Class.
Definition log/logger.h:142
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition macros.h:32
#define LOG_WARN(LOGGER, MESSAGE)
Macro to conveniently test warn output and log it.
Definition macros.h:26
isc::log::Logger logger("asiodns")
Use the ASIO logger.
const isc::log::MessageID LOGIMPL_BAD_DEBUG_STRING
const isc::log::MessageID LOGIMPL_BELOW_MIN_DEBUG
const isc::log::MessageID LOGIMPL_ABOVE_MAX_DEBUG
const int MAX_DEBUG_LEVEL
const int MIN_DEBUG_LEVEL
Minimum/maximum debug levels.
Severity
Severity Levels.
Defines the logger used by the top-level component of kea-lfc.
Log level structure.
int dbglevel
Debug level.
Severity severity
Logging severity.