Branch data Line data Source code
1 : : // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #include <iostream>
16 : : #include <algorithm>
17 : : #include <string>
18 : :
19 : : #include <log/logger_level.h>
20 : : #include <log/logger_name.h>
21 : : #include <log/logger_manager.h>
22 : : #include <log/logger_specification.h>
23 : : #include <log/logger_unittest_support.h>
24 : : #include <log/logger_support.h>
25 : : #include <log/output_option.h>
26 : :
27 : : using namespace std;
28 : :
29 : : namespace isc {
30 : : namespace log {
31 : :
32 : : // Get the logging severity. This is defined by the environment variable
33 : : // B10_LOGGER_SEVERITY, and can be one of "DEBUG", "INFO", "WARN", "ERROR"
34 : : // of "FATAL". (Note that the string must be in upper case with no leading
35 : : // of trailing blanks.) If not present, the default severity passed to the
36 : : // function is returned.
37 : : isc::log::Severity
38 : 46 : b10LoggerSeverity(isc::log::Severity defseverity) {
39 : 46 : const char* sev_char = getenv("B10_LOGGER_SEVERITY");
40 [ + + ]: 46 : if (sev_char) {
41 [ + - ]: 46 : return (isc::log::getSeverity(sev_char));
42 : : }
43 : : return (defseverity);
44 : : }
45 : :
46 : : // Get the debug level. This is defined by the envornment variable
47 : : // B10_LOGGER_DBGLEVEL. If not defined, a default value passed to the function
48 : : // is returned.
49 : : int
50 : 46 : b10LoggerDbglevel(int defdbglevel) {
51 : 46 : const char* dbg_char = getenv("B10_LOGGER_DBGLEVEL");
52 [ + + ]: 46 : if (dbg_char) {
53 : 2 : int level = 0;
54 : : try {
55 : 2 : level = boost::lexical_cast<int>(dbg_char);
56 [ - + ]: 2 : if (level < MIN_DEBUG_LEVEL) {
57 [ # # ][ # # ]: 0 : std::cerr << "**ERROR** debug level of " << level
58 [ # # ][ # # ]: 0 : << " is invalid - a value of " << MIN_DEBUG_LEVEL
59 [ # # ]: 0 : << " will be used\n";
60 : : level = MIN_DEBUG_LEVEL;
61 [ - + ]: 2 : } else if (level > MAX_DEBUG_LEVEL) {
62 [ # # ][ # # ]: 0 : std::cerr << "**ERROR** debug level of " << level
63 [ # # ][ # # ]: 0 : << " is invalid - a value of " << MAX_DEBUG_LEVEL
64 [ # # ]: 0 : << " will be used\n";
65 : : level = MAX_DEBUG_LEVEL;
66 : : }
67 : 0 : } catch (...) {
68 : : // Error, but not fatal to the test
69 : : std::cerr << "**ERROR** Unable to translate "
70 [ # # ]: 0 : "B10_LOGGER_DBGLEVEL - a value of 0 will be used\n";
71 : : }
72 : 46 : return (level);
73 : : }
74 : :
75 : : return (defdbglevel);
76 : : }
77 : :
78 : :
79 : : // Reset characteristics of the root logger to that set by the environment
80 : : // variables B10_LOGGER_SEVERITY, B10_LOGGER_DBGLEVEL and B10_LOGGER_DESTINATION.
81 : :
82 : : void
83 : 46 : resetUnitTestRootLogger() {
84 : :
85 : : using namespace isc::log;
86 : :
87 : : // Constants: not declared static as this is function is expected to be
88 : : // called once only
89 : 92 : const string DEVNULL = "/dev/null";
90 [ + - ]: 92 : const string STDOUT = "stdout";
91 [ + - ]: 92 : const string STDERR = "stderr";
92 [ + - ]: 92 : const string SYSLOG = "syslog";
93 [ + - ]: 92 : const string SYSLOG_COLON = "syslog:";
94 : :
95 : : // Get the destination. If not specified, assume /dev/null. (The default
96 : : // severity for unit tests is DEBUG, which generates a lot of output.
97 : : // Routing the logging to /dev/null will suppress that, whilst still
98 : : // ensuring that the code paths are tested.)
99 : 46 : const char* destination = getenv("B10_LOGGER_DESTINATION");
100 [ + + ][ + - ]: 92 : const string dest((destination == NULL) ? DEVNULL : destination);
[ + - ]
101 : :
102 : : // Prepare the objects to define the logging specification
103 [ + - ]: 46 : LoggerSpecification spec(getRootLoggerName(),
104 : : b10LoggerSeverity(isc::log::DEBUG),
105 [ + - ][ + - ]: 92 : b10LoggerDbglevel(isc::log::MAX_DEBUG_LEVEL));
106 : 46 : OutputOption option;
107 : :
108 : : // Set up output option according to destination specification
109 [ + + ]: 46 : if (dest == STDOUT) {
110 : 4 : option.destination = OutputOption::DEST_CONSOLE;
111 : 4 : option.stream = OutputOption::STR_STDOUT;
112 : :
113 [ + + ]: 42 : } else if (dest == STDERR) {
114 : 1 : option.destination = OutputOption::DEST_CONSOLE;
115 : 1 : option.stream = OutputOption::STR_STDERR;
116 : :
117 [ - + ]: 41 : } else if (dest == SYSLOG) {
118 : 0 : option.destination = OutputOption::DEST_SYSLOG;
119 : : // Use default specified in OutputOption constructor for the
120 : : // syslog destination
121 : :
122 [ + - ][ - + ]: 41 : } else if (dest.find(SYSLOG_COLON) == 0) {
123 : 0 : option.destination = OutputOption::DEST_SYSLOG;
124 : : // Must take account of the string actually being "syslog:"
125 [ # # ]: 0 : if (dest == SYSLOG_COLON) {
126 [ # # ]: 0 : cerr << "**ERROR** value for B10_LOGGER_DESTINATION of " <<
127 [ # # ][ # # ]: 0 : SYSLOG_COLON << " is invalid, " << SYSLOG <<
[ # # ]
128 [ # # ]: 0 : " will be used instead\n";
129 : : // Use default for logging facility
130 : :
131 : : } else {
132 : : // Everything else in the string is the facility name
133 [ # # ]: 0 : option.facility = dest.substr(SYSLOG_COLON.size());
134 : : }
135 : :
136 : : } else {
137 : : // Not a recognised destination, assume a file.
138 : 41 : option.destination = OutputOption::DEST_FILE;
139 : : option.filename = dest;
140 : : }
141 : :
142 : : // ... and set the destination
143 : : spec.addOutputOption(option);
144 [ + - ][ + - ]: 92 : LoggerManager manager;
145 : : manager.process(spec);
146 : 46 : }
147 : :
148 : :
149 : : // Logger Run-Time Initialization via Environment Variables
150 : 23 : void initLogger(isc::log::Severity severity, int dbglevel) {
151 : :
152 : : // Root logger name is defined by the environment variable B10_LOGGER_ROOT.
153 : : // If not present, the name is "bind10".
154 : 23 : const char* DEFAULT_ROOT = "bind10";
155 : 23 : const char* root = getenv("B10_LOGGER_ROOT");
156 [ + - ]: 23 : if (! root) {
157 : 23 : root = DEFAULT_ROOT;
158 : : }
159 : :
160 : : // Set the local message file
161 : 23 : const char* localfile = getenv("B10_LOGGER_LOCALMSG");
162 : :
163 : : // Initialize logging
164 [ + - ]: 23 : initLogger(root, isc::log::DEBUG, isc::log::MAX_DEBUG_LEVEL, localfile);
165 : :
166 : : // Now set reset the output destination of the root logger, overriding
167 : : // the default severity, debug level and destination with those specified
168 : : // in the environment variables. (The two-step approach is used as the
169 : : // setUnitTestRootLoggerCharacteristics() function is used in several
170 : : // places in the BIND 10 tests, and it avoid duplicating code.)
171 : 23 : resetUnitTestRootLogger();
172 : 23 : }
173 : :
174 : : } // namespace log
175 : 141 : } // namespace isc
|