logger_support.cc 4.3 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. /// \brief Temporary Logger Support
  15. ///
  16. /// Performs run-time initialization of the logger system. In particular, it
  17. /// is passed information from the command line and:
  18. ///
  19. /// a) Sets the severity of the messages being logged (and debug level if
  20. /// appropriate).
  21. /// b) Reads in the local message file is one has been supplied.
  22. ///
  23. /// These functions will be replaced once the code has been written to obtain
  24. /// the logging parameters from the configuration database.
  25. #include <iostream>
  26. #include <algorithm>
  27. #include <iostream>
  28. #include <string>
  29. #include <log/logger_level.h>
  30. #include <log/logger_manager.h>
  31. #include <log/logger_support.h>
  32. using namespace std;
  33. namespace {
  34. // Flag to hold logging initialization state.
  35. bool logging_init_state = false;
  36. } // Anonymous namespace
  37. namespace isc {
  38. namespace log {
  39. // Return initialization state.
  40. bool
  41. isLoggingInitialized() {
  42. return (logging_init_state);
  43. }
  44. // Set initialization state. (Note: as logging can be initialized via a direct
  45. // call to LoggerManager::init(), this function is called from there, not from
  46. // the initialization functions in this file.
  47. void
  48. setLoggingInitialized(bool state) {
  49. logging_init_state = state;
  50. }
  51. // Logger Run-Time Initialization.
  52. void
  53. initLogger(const string& root, isc::log::Severity severity, int dbglevel,
  54. const char* file) {
  55. LoggerManager::init(root, severity, dbglevel, file);
  56. }
  57. // Logger Run-Time Initialization via Environment Variables
  58. void initLogger(isc::log::Severity severity, int dbglevel) {
  59. // Root logger name is defined by the environment variable B10_LOGGER_ROOT.
  60. // If not present, the name is "bind10".
  61. const char* DEFAULT_ROOT = "bind10";
  62. const char* root = getenv("B10_LOGGER_ROOT");
  63. if (! root) {
  64. root = DEFAULT_ROOT;
  65. }
  66. // Set the logging severity. The environment variable is
  67. // B10_LOGGER_SEVERITY, and can be one of "DEBUG", "INFO", "WARN", "ERROR"
  68. // of "FATAL". Note that the string must be in upper case with no leading
  69. // of trailing blanks.
  70. const char* sev_char = getenv("B10_LOGGER_SEVERITY");
  71. if (sev_char) {
  72. severity = isc::log::getSeverity(sev_char);
  73. }
  74. // If the severity is debug, get the debug level (environment variable
  75. // B10_LOGGER_DBGLEVEL), which should be in the range 0 to 99.
  76. if (severity == isc::log::DEBUG) {
  77. const char* dbg_char = getenv("B10_LOGGER_DBGLEVEL");
  78. if (dbg_char) {
  79. int level = 0;
  80. try {
  81. level = boost::lexical_cast<int>(dbg_char);
  82. if (level < MIN_DEBUG_LEVEL) {
  83. cerr << "**ERROR** debug level of " << level
  84. << " is invalid - a value of " << MIN_DEBUG_LEVEL
  85. << " will be used\n";
  86. level = MIN_DEBUG_LEVEL;
  87. } else if (level > MAX_DEBUG_LEVEL) {
  88. cerr << "**ERROR** debug level of " << level
  89. << " is invalid - a value of " << MAX_DEBUG_LEVEL
  90. << " will be used\n";
  91. level = MAX_DEBUG_LEVEL;
  92. }
  93. } catch (...) {
  94. // Error, but not fatal to the test
  95. cerr << "**ERROR** Unable to translate "
  96. "B10_LOGGER_DBGLEVEL - a value of 0 will be used\n";
  97. }
  98. dbglevel = level;
  99. }
  100. }
  101. /// Set the local message file
  102. const char* localfile = getenv("B10_LOGGER_LOCALMSG");
  103. // Initialize logging
  104. initLogger(root, severity, dbglevel, localfile);
  105. }
  106. } // namespace log
  107. } // namespace isc