Kea 3.1.5
logger_unittest_support.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 <log/logger_level.h>
10#include <log/logger_manager.h>
11#include <log/logger_name.h>
13#include <log/logger_support.h>
15#include <log/output_option.h>
16
17#include <algorithm>
18#include <cstdlib>
19#include <iostream>
20#include <string>
21
22using namespace std;
23
24namespace isc {
25namespace log {
26
27// Get the logging severity. This is defined by the environment variable
28// KEA_LOGGER_SEVERITY, and can be one of "DEBUG", "INFO", "WARN", "ERROR"
29// of "FATAL". (Note that the string must be in upper case with no leading
30// of trailing blanks.) If not present, the default severity passed to the
31// function is returned.
34 const char* sev_char = getenv("KEA_LOGGER_SEVERITY");
35 if (sev_char) {
36 return (isc::log::getSeverity(sev_char));
37 }
38 return (defseverity);
39}
40
41// Get the debug level. This is defined by the environment variable
42// KEA_LOGGER_DBGLEVEL. If not defined, a default value passed to the function
43// is returned.
44int
45keaLoggerDbglevel(int defdbglevel) {
46 const char* dbg_char = getenv("KEA_LOGGER_DBGLEVEL");
47 if (dbg_char) {
48 int level = 0;
49 try {
50 level = boost::lexical_cast<int>(dbg_char);
51 if (level < MIN_DEBUG_LEVEL) {
52 std::cerr << "**ERROR** debug level of " << level
53 << " is invalid - a value of " << MIN_DEBUG_LEVEL
54 << " will be used\n";
55 level = MIN_DEBUG_LEVEL;
56 } else if (level > MAX_DEBUG_LEVEL) {
57 std::cerr << "**ERROR** debug level of " << level
58 << " is invalid - a value of " << MAX_DEBUG_LEVEL
59 << " will be used\n";
60 level = MAX_DEBUG_LEVEL;
61 }
62 } catch (...) {
63 // Error, but not fatal to the test
64 std::cerr << "**ERROR** Unable to translate "
65 "KEA_LOGGER_DBGLEVEL - a value of 0 will be used\n";
66 }
67 return (level);
68 }
69
70 return (defdbglevel);
71}
72
73// Logger Run-Time Initialization via Environment Variables
74void initLogger(isc::log::Severity severity, int dbglevel) {
75
76 // Root logger name is defined by the environment variable KEA_LOGGER_ROOT.
77 // If not present, the name is "kea".
78 const char* root = getenv("KEA_LOGGER_ROOT");
79 if (! root) {
80 // If not present, the name is "kea".
82 }
83
84 // Set the local message file
85 const char* localfile = getenv("KEA_LOGGER_LOCALMSG");
86
87 // Set a directory for creating lockfiles when running tests
88 setenv("KEA_LOCKFILE_DIR", TOP_BUILDDIR, 0);
89
90 // Initialize logging
91 initLogger(root, severity, dbglevel, localfile);
92
93 // Now set reset the output destination of the root logger, overriding
94 // the default severity, debug level and destination with those specified
95 // in the environment variables. (The two-step approach is used as the
96 // setUnitTestRootLoggerCharacteristics() function is used in several
97 // places in the Kea tests, and it avoid duplicating code.)
99}
100
101} // namespace log
102} // namespace isc
Logging initialization functions.
Miscellaneous logging functions used by the unit tests.
void initLogger(const string &root, isc::log::Severity severity, int dbglevel, const char *file, bool buffer)
Run-time initialization.
isc::log::Severity getSeverity(const std::string &sev_str)
Returns the isc::log::Severity value represented by the given string.
const std::string & getDefaultRootLoggerName()
Returns the default ('kea') root logger name.
const int MAX_DEBUG_LEVEL
int keaLoggerDbglevel(int defdbglevel)
Obtains logging debug level from KEA_LOGGER_DBGLEVEL.
void setDefaultLoggingOutput(bool verbose)
Reset root logger characteristics.
const int MIN_DEBUG_LEVEL
Minimum/maximum debug levels.
Severity
Severity Levels.
isc::log::Severity keaLoggerSeverity(isc::log::Severity defseverity)
Obtains logging severity from KEA_LOGGER_SEVERITY.
Defines the logger used by the top-level component of kea-lfc.