logger_unittest_support.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include <iostream>
  15. #include <algorithm>
  16. #include <string>
  17. #include <log/logger_level.h>
  18. #include <log/logger_name.h>
  19. #include <log/logger_manager.h>
  20. #include <log/logger_specification.h>
  21. #include <log/logger_unittest_support.h>
  22. #include <log/logger_support.h>
  23. #include <log/output_option.h>
  24. using namespace std;
  25. namespace isc {
  26. namespace log {
  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.
  32. isc::log::Severity
  33. keaLoggerSeverity(isc::log::Severity defseverity) {
  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. // 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. return (defdbglevel);
  69. }
  70. // Reset characteristics of the root logger to that set by the environment
  71. // variables KEA_LOGGER_SEVERITY, KEA_LOGGER_DBGLEVEL and KEA_LOGGER_DESTINATION.
  72. void
  73. resetUnitTestRootLogger() {
  74. using namespace isc::log;
  75. // Constants: not declared static as this is function is expected to be
  76. // called once only
  77. const string DEVNULL = "/dev/null";
  78. const string STDOUT = "stdout";
  79. const string STDERR = "stderr";
  80. const string SYSLOG = "syslog";
  81. const string SYSLOG_COLON = "syslog:";
  82. // Get the destination. If not specified, assume /dev/null. (The default
  83. // severity for unit tests is DEBUG, which generates a lot of output.
  84. // Routing the logging to /dev/null will suppress that, whilst still
  85. // ensuring that the code paths are tested.)
  86. const char* destination = getenv("KEA_LOGGER_DESTINATION");
  87. const string dest((destination == NULL) ? DEVNULL : destination);
  88. // Prepare the objects to define the logging specification
  89. LoggerSpecification spec(getRootLoggerName(),
  90. keaLoggerSeverity(isc::log::DEBUG),
  91. keaLoggerDbglevel(isc::log::MAX_DEBUG_LEVEL));
  92. OutputOption option;
  93. // Set up output option according to destination specification
  94. if (dest == STDOUT) {
  95. option.destination = OutputOption::DEST_CONSOLE;
  96. option.stream = OutputOption::STR_STDOUT;
  97. } else if (dest == STDERR) {
  98. option.destination = OutputOption::DEST_CONSOLE;
  99. option.stream = OutputOption::STR_STDERR;
  100. } else if (dest == SYSLOG) {
  101. option.destination = OutputOption::DEST_SYSLOG;
  102. // Use default specified in OutputOption constructor for the
  103. // syslog destination
  104. } else if (dest.find(SYSLOG_COLON) == 0) {
  105. option.destination = OutputOption::DEST_SYSLOG;
  106. // Must take account of the string actually being "syslog:"
  107. if (dest == SYSLOG_COLON) {
  108. cerr << "**ERROR** value for KEA_LOGGER_DESTINATION of " <<
  109. SYSLOG_COLON << " is invalid, " << SYSLOG <<
  110. " will be used instead\n";
  111. // Use default for logging facility
  112. } else {
  113. // Everything else in the string is the facility name
  114. option.facility = dest.substr(SYSLOG_COLON.size());
  115. }
  116. } else {
  117. // Not a recognised destination, assume a file.
  118. option.destination = OutputOption::DEST_FILE;
  119. option.filename = dest;
  120. }
  121. // ... and set the destination
  122. spec.addOutputOption(option);
  123. LoggerManager manager;
  124. manager.process(spec);
  125. }
  126. // Logger Run-Time Initialization via Environment Variables
  127. void initLogger(isc::log::Severity severity, int dbglevel) {
  128. // Root logger name is defined by the environment variable KEA_LOGGER_ROOT.
  129. // If not present, the name is "kea".
  130. const char* DEFAULT_ROOT = "kea";
  131. const char* root = getenv("KEA_LOGGER_ROOT");
  132. if (! root) {
  133. root = DEFAULT_ROOT;
  134. }
  135. // Set the local message file
  136. const char* localfile = getenv("KEA_LOGGER_LOCALMSG");
  137. // Set a directory for creating lockfiles when running tests
  138. setenv("KEA_LOCKFILE_DIR_FROM_BUILD", TOP_BUILDDIR, 1);
  139. // Initialize logging
  140. initLogger(root, isc::log::DEBUG, isc::log::MAX_DEBUG_LEVEL, localfile);
  141. // Now set reset the output destination of the root logger, overriding
  142. // the default severity, debug level and destination with those specified
  143. // in the environment variables. (The two-step approach is used as the
  144. // setUnitTestRootLoggerCharacteristics() function is used in several
  145. // places in the Kea tests, and it avoid duplicating code.)
  146. resetUnitTestRootLogger();
  147. }
  148. } // namespace log
  149. } // namespace isc