logger_unittest_support.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2011,2014,2015 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. // Logger Run-Time Initialization via Environment Variables
  71. void initLogger(isc::log::Severity severity, int dbglevel) {
  72. // Root logger name is defined by the environment variable KEA_LOGGER_ROOT.
  73. // If not present, the name is "kea".
  74. const char* root = getenv("KEA_LOGGER_ROOT");
  75. if (! root) {
  76. // If not present, the name is "kea".
  77. root = isc::log::getDefaultRootLoggerName().c_str();
  78. }
  79. // Set the local message file
  80. const char* localfile = getenv("KEA_LOGGER_LOCALMSG");
  81. // Set a directory for creating lockfiles when running tests
  82. setenv("KEA_LOCKFILE_DIR", TOP_BUILDDIR, 0);
  83. // Initialize logging
  84. initLogger(root, isc::log::DEBUG, isc::log::MAX_DEBUG_LEVEL, localfile);
  85. // Now set reset the output destination of the root logger, overriding
  86. // the default severity, debug level and destination with those specified
  87. // in the environment variables. (The two-step approach is used as the
  88. // setUnitTestRootLoggerCharacteristics() function is used in several
  89. // places in the Kea tests, and it avoid duplicating code.)
  90. isc::log::setDefaultLoggingOutput();
  91. }
  92. } // namespace log
  93. } // namespace isc