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