logger_support.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.h>
  30. #include <log/logger_manager.h>
  31. #include <log/logger_support.h>
  32. namespace isc {
  33. namespace log {
  34. using namespace std;
  35. // Declare a logger for the logging subsystem. This is a sub-logger of the
  36. // root logger and is used in all functions in this file.
  37. Logger logger("log");
  38. /// Logger Run-Time Initialization
  39. void
  40. initLogger(const string& root, isc::log::Severity severity, int dbglevel,
  41. const char* file) {
  42. LoggerManager::init(root, file, severity, dbglevel);
  43. }
  44. /// Logger Run-Time Initialization via Environment Variables
  45. void initLogger() {
  46. // Root logger name is defined by the environment variable B10_LOGGER_ROOT.
  47. // If not present, the name is "b10root".
  48. const char* DEFAULT_ROOT = "b10root";
  49. const char* root = getenv("B10_LOGGER_ROOT");
  50. if (! root) {
  51. root = DEFAULT_ROOT;
  52. }
  53. // Set the logging severity. The environment variable is
  54. // B10_LOGGER_SEVERITY, and can be one of "DEBUG", "INFO", "WARN", "ERROR"
  55. // of "FATAL". Note that the string must be in upper case with no leading
  56. // of trailing blanks.
  57. isc::log::Severity severity = isc::log::DEBUG;
  58. const char* sev_char = getenv("B10_LOGGER_SEVERITY");
  59. if (sev_char) {
  60. string sev_string(sev_char);
  61. if (sev_string == "DEBUG") {
  62. severity = isc::log::DEBUG;
  63. } else if (sev_string == "INFO") {
  64. severity = isc::log::INFO;
  65. } else if (sev_string == "WARN") {
  66. severity = isc::log::WARN;
  67. } else if (sev_string == "ERROR") {
  68. severity = isc::log::ERROR;
  69. } else if (sev_string == "FATAL") {
  70. severity = isc::log::FATAL;
  71. } else {
  72. std::cerr << "**ERROR** unrecognised logger severity of '"
  73. << sev_string << "' - default severity will be used\n";
  74. }
  75. }
  76. // If the severity is debug, get the debug level (environment variable
  77. // B10_LOGGER_DBGLEVEL), which should be in the range 0 to 99.
  78. int dbglevel = 0;
  79. if (severity == isc::log::DEBUG) {
  80. const char* dbg_char = getenv("B10_LOGGER_DBGLEVEL");
  81. if (dbg_char) {
  82. int level = 0;
  83. try {
  84. level = boost::lexical_cast<int>(dbg_char);
  85. if (level < MIN_DEBUG_LEVEL) {
  86. std::cerr << "**ERROR** debug level of " << level
  87. << " is invalid - a value of " << MIN_DEBUG_LEVEL
  88. << " will be used\n";
  89. level = MIN_DEBUG_LEVEL;
  90. } else if (level > MAX_DEBUG_LEVEL) {
  91. std::cerr << "**ERROR** debug level of " << level
  92. << " is invalid - a value of " << MAX_DEBUG_LEVEL
  93. << " will be used\n";
  94. level = MAX_DEBUG_LEVEL;
  95. }
  96. } catch (...) {
  97. // Error, but not fatal to the test
  98. std::cerr << "**ERROR** Unable to translate "
  99. "B10_LOGGER_DBGLEVEL - a value of 0 will be used\n";
  100. }
  101. dbglevel = level;
  102. }
  103. }
  104. /// Set the local message file
  105. const char* localfile = getenv("B10_LOGGER_LOCALMSG");
  106. // Initialize logging
  107. initLogger(root, severity, dbglevel, localfile);
  108. }
  109. } // namespace log
  110. } // namespace isc