Kea 3.1.5
logger_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
10#include <log/logger_name.h>
11#include <log/logger_support.h>
12
13#include <cstdlib>
14#include <string>
15
16using namespace std;
17
18namespace {
19
20// Flag to hold logging initialization state.
21bool logging_init_state = false;
22
23} // Anonymous namespace
24
25namespace isc {
26namespace log {
27
28// Return initialization state.
29bool
31 return (logging_init_state);
32}
33
34// Set initialization state. (Note: as logging can be initialized via a direct
35// call to LoggerManager::init(), this function is called from there, not from
36// the initialization functions in this file.
37void
39 logging_init_state = state;
40}
41
42// Logger Run-Time Initialization.
43
44void
45initLogger(const string& root, isc::log::Severity severity, int dbglevel,
46 const char* file, bool buffer) {
47 LoggerManager::init(root, severity, dbglevel, file, buffer);
48}
49
50// Reset characteristics of the root logger to that set by the environment
51// variables KEA_LOGGER_SEVERITY, KEA_LOGGER_DBGLEVEL and KEA_LOGGER_DESTINATION.
52
53void
55
56 using namespace isc::log;
57
58 // Constants: not declared static as this is function is expected to be
59 // called once only
60 const string DEVNULL = "/dev/null";
61 const string STDOUT = "stdout";
62 const string STDERR = "stderr";
63 const string SYSLOG = "syslog";
64 const string SYSLOG_COLON = "syslog:";
65
66 // Get the destination. If not specified, assume /dev/null. (The default
67 // severity for unit tests is DEBUG, which generates a lot of output.
68 // Routing the logging to /dev/null will suppress that, whilst still
69 // ensuring that the code paths are tested.)
70 const char* destination = getenv("KEA_LOGGER_DESTINATION");
71 const string dest((destination == NULL) ? DEVNULL : destination);
72
73 // Prepare the objects to define the logging specification
78 OutputOption option;
79
80 // Set up output option according to destination specification
81 if (dest == STDOUT) {
84
85 } else if (dest == STDERR) {
88
89 } else if (dest == SYSLOG) {
91 // Use default specified in OutputOption constructor for the
92 // syslog destination
93
94 } else if (dest.find(SYSLOG_COLON) == 0) {
96 // Must take account of the string actually being "syslog:"
97 if (dest == SYSLOG_COLON) {
98 cerr << "**ERROR** value for KEA_LOGGER_DESTINATION of " <<
99 SYSLOG_COLON << " is invalid, " << SYSLOG <<
100 " will be used instead\n";
101 // Use default for logging facility
102
103 } else {
104 // Everything else in the string is the facility name
105 option.facility = dest.substr(SYSLOG_COLON.size());
106 }
107
108 } else {
109 // Not a recognized destination, assume a file.
111 option.filename = dest;
112 }
113
114 // ... and set the destination
115 spec.addOutputOption(option);
116 LoggerManager manager;
117 manager.process(spec);
118}
119
120} // namespace log
121} // namespace isc
static void init(const std::string &root, isc::log::Severity severity=isc::log::INFO, int dbglevel=0, const char *file=NULL, bool buffer=false)
Run-Time Initialization.
void process(T start, T finish)
Process Specifications.
void addOutputOption(const OutputOption &option)
Add output option.
Logging initialization functions.
void setLoggingInitialized(bool state)
Set state of "logging initialized" flag.
const std::string & getRootLoggerName()
Get root logger name.
void initLogger(const string &root, isc::log::Severity severity, int dbglevel, const char *file, bool buffer)
Run-time initialization.
bool isLoggingInitialized()
Is logging initialized?
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.
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.
Destination destination
Members.
std::string facility
syslog facility
Stream stream
stdout/stderr if console output
std::string filename
Filename if file output.