logger_manager.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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_MANAGER_H
  15. #define __LOGGER_MANAGER_H
  16. #include "exceptions/exceptions.h"
  17. #include <log/logger_specification.h>
  18. // Generated if, when updating the logging specification, an unknown
  19. // destination is encountered.
  20. class UnknownLoggingDestination : public isc::Exception {
  21. public:
  22. UnknownLoggingDestination(const char* file, size_t line, const char* what) :
  23. isc::Exception(file, line, what)
  24. {}
  25. };
  26. namespace isc {
  27. namespace log {
  28. class LoggerManagerImpl;
  29. /// \brief Logger Manager
  30. ///
  31. /// The logger manager class exists to process the set of logger specifications
  32. /// and use them to set up the logging in the program appropriately.
  33. ///
  34. /// To isolate the underlying implementation from basic processing, the
  35. /// LoggerManager is implemented using the "pimpl" idiom.
  36. class LoggerManager {
  37. public:
  38. /// \brief Constructor
  39. LoggerManager();
  40. /// \brief Destructor
  41. ~LoggerManager();
  42. /// \brief Process Specifications
  43. ///
  44. /// Replaces the current logging configuration by the one given.
  45. ///
  46. /// \param start Iterator pointing to the start of the collection of
  47. /// logging specifications.
  48. /// \param finish Iterator pointing to the end of the collection of
  49. /// logging specification.
  50. template <typename T>
  51. void process(T start, T finish) {
  52. processInit();
  53. for (T i = start; i != finish; ++i) {
  54. processSpecification(*i);
  55. }
  56. processEnd();
  57. }
  58. /// \brief Process a single specification
  59. ///
  60. /// A convenience function for a single specification.
  61. ///
  62. /// \param spec Specification to process
  63. void process(const LoggerSpecification& spec) {
  64. processInit();
  65. processSpecification(spec);
  66. processEnd();
  67. }
  68. /// \brief Run-Time Initialization
  69. ///
  70. /// Performs run-time initialization of the logger system, in particular
  71. /// supplying the root logger name and name of a replacement message file.
  72. ///
  73. /// This must be the first logging function called in the program. If
  74. /// an attempt is made to log a message before this is function is called,
  75. /// the results will be dependent on the underlying logging package.
  76. ///
  77. /// \param root Name of the root logger. This should be set to the name of
  78. /// the program.
  79. /// \param file Name of the local message file. This must be NULL if there
  80. /// is no local message file.
  81. /// \param severity Severity at which to log
  82. /// \param dbglevel Debug severity (ignored if "severity" is not "DEBUG")
  83. static void init(const std::string& root, const char* file = NULL,
  84. isc::log::Severity severity = isc::log::INFO,
  85. int dbglevel = 0);
  86. /// \brief Reset logging
  87. ///
  88. /// Resets logging to default (just the root logger output INFO or above
  89. /// messages to the console.
  90. static void reset();
  91. /// \brief Read local message file
  92. ///
  93. /// Reads the local message file into the global dictionary, overwriting
  94. /// existing messages. If the file contained any message IDs not in the
  95. /// dictionary, they are listed in a warning message.
  96. ///
  97. /// \param file Name of the local message file
  98. static void readLocalMessageFile(const char* file);
  99. private:
  100. /// \brief Initialize Processing
  101. ///
  102. /// Initializes the processing of a list of specifications by resetting all
  103. /// loggers to their defaults, which is to pass the message to their
  104. /// parent logger. (Except for the root logger, where the default action is
  105. /// to output the message.)
  106. void processInit();
  107. /// \brief Process Logging Specification
  108. ///
  109. /// Processes the given specification. It is assumed at this point that
  110. /// either the logger does not exist or has been made inactive.
  111. void processSpecification(const LoggerSpecification& spec);
  112. /// \brief End Processing
  113. ///
  114. /// Place holder for finish processing.
  115. /// TODO: Check that the root logger has something enabled
  116. void processEnd();
  117. // Members
  118. LoggerManagerImpl* impl_; ///< Pointer to implementation
  119. };
  120. } // namespace log
  121. } // namespace isc
  122. #endif // __LOGGER_MANAGER_H