logger_unittest_support.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef __LOGGER_UNITTEST_SUPPORT_H
  15. #define __LOGGER_UNITTEST_SUPPORT_H
  16. #include <string>
  17. #include <log/logger.h>
  18. /// \file
  19. /// \brief Miscellaneous logging functions used by the unit tests.
  20. ///
  21. /// As the configuration database is unsually unavailable during unit tests,
  22. /// the functions defined here allow a limited amount of logging configuration
  23. /// through the use of environment variables
  24. namespace isc {
  25. namespace log {
  26. /// \brief Run-Time Initialization for Unit Tests from Environment
  27. ///
  28. /// Performs run-time initialization of the logger via the setting of
  29. /// environment variables. These are:
  30. ///
  31. /// - B10_LOGGER_ROOT\n
  32. /// Name of the root logger. If not given, the string "bind10" will be used.
  33. ///
  34. /// - B10_LOGGER_SEVERITY\n
  35. /// Severity of messages that will be logged. This must be one of the strings
  36. /// "DEBUG", "INFO", "WARN", "ERROR", "FATAL" or "NONE". (Must be upper case
  37. /// and must not contain leading or trailing spaces.) If not specified (or if
  38. /// specified but incorrect), the default passed as argument to this function
  39. /// (currently DEBUG) will be used.
  40. ///
  41. /// - B10_LOGGER_DBGLEVEL\n
  42. /// Ignored if the level is not DEBUG, this should be a number between 0 and
  43. /// 99 indicating the logging severity. The default is 0. If outside these
  44. /// limits or if not a number, The value passed to this function (default
  45. /// of MAX_DEBUG_LEVEL) is used.
  46. ///
  47. /// - B10_LOGGER_LOCALMSG\n
  48. /// If defined, the path specification of a file that contains message
  49. /// definitions replacing ones in the default dictionary.
  50. ///
  51. /// - B10_LOGGER_DESTINATION\n
  52. /// If defined, the destination of the logging output. This can be one of:
  53. /// - \c stdout Send output to stdout.
  54. /// - \c stderr Send output to stderr
  55. /// - \c syslog Send output to syslog using the facility local0.
  56. /// - \c syslog:xxx Send output to syslog, using the facility xxx. ("xxx"
  57. /// should be one of the syslog facilities such as "local0".) There must
  58. /// be a colon between "syslog" and "xxx
  59. /// - \c other Anything else is interpreted as the name of a file to which
  60. /// output is appended. If the file does not exist, it is created.
  61. ///
  62. /// Any errors in the settings cause messages to be output to stderr.
  63. ///
  64. /// This function is aimed at test programs, allowing the default settings to
  65. /// be overridden by the tester. It is not intended for use in production
  66. /// code.
  67. ///
  68. /// TODO: Rename. This function overloads the initLogger() function that can
  69. /// be used to initialize production programs. This may lead to confusion.
  70. void initLogger(isc::log::Severity severity = isc::log::DEBUG,
  71. int dbglevel = isc::log::MAX_DEBUG_LEVEL);
  72. /// \brief Obtains logging severity from B10_LOGGER_SEVERITY
  73. ///
  74. /// Support function called by the unit test logging initialization code.
  75. /// It returns the logging severity defined by B10_LOGGER_SEVERITY. If
  76. /// not defined it returns the default passed to it.
  77. ///
  78. /// \param defseverity Default severity used if B10_LOGGER_SEVERITY is not
  79. // defined.
  80. ///
  81. /// \return Severity to use for the logging.
  82. isc::log::Severity b10LoggerSeverity(isc::log::Severity defseverity);
  83. /// \brief Obtains logging debug level from B10_LOGGER_DBGLEVEL
  84. ///
  85. /// Support function called by the unit test logging initialization code.
  86. /// It returns the logging debug level defined by B10_LOGGER_DBGLEVEL. If
  87. /// not defined, it returns the default passed to it.
  88. ///
  89. /// N.B. If there is an error, a message is written to stderr and a value
  90. /// related to the error is used. (This is because (a) logging is not yet
  91. /// initialized, hence only the error stream is known to exist, and (b) this
  92. /// function is only used in unit test logging initialization, so incorrect
  93. /// selection of a level is not really an issue.)
  94. ///
  95. /// \param defdbglevel Default debug level to be used if B10_LOGGER_DBGLEVEL
  96. /// is not defined.
  97. ///
  98. /// \return Debug level to use.
  99. int b10LoggerDbglevel(int defdbglevel);
  100. /// \brief Reset root logger characteristics
  101. ///
  102. /// This is a simplified interface into the resetting of the characteristics
  103. /// of the root logger. It is aimed for use in unit tests and resets the
  104. /// characteristics of the root logger to use a severity, debug level and
  105. /// destination set by the environment variables B10_LOGGER_SEVERITY,
  106. /// B10_LOGGER_DBGLEVEL and B10_LOGGER_DESTINATION.
  107. void
  108. resetUnitTestRootLogger();
  109. } // namespace log
  110. } // namespace isc
  111. #endif // __LOGGER_UNITTEST_SUPPORT_H