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