logger_manager.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (C) 2011, 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. #ifndef LOGGER_MANAGER_H
  15. #define LOGGER_MANAGER_H
  16. #include <exceptions/exceptions.h>
  17. #include <util/threads/sync.h>
  18. #include <log/logger_specification.h>
  19. #include <boost/noncopyable.hpp>
  20. // Generated if, when updating the logging specification, an unknown
  21. // destination is encountered.
  22. class UnknownLoggingDestination : public isc::Exception {
  23. public:
  24. UnknownLoggingDestination(const char* file, size_t line, const char* what) :
  25. isc::Exception(file, line, what)
  26. {}
  27. };
  28. namespace isc {
  29. namespace log {
  30. class LoggerManagerImpl;
  31. /// \brief Logger Manager
  32. ///
  33. /// The logger manager class exists to process the set of logger specifications
  34. /// and use them to set up the logging in the program appropriately.
  35. ///
  36. /// To isolate the underlying implementation from basic processing, the
  37. /// LoggerManager is implemented using the "pimpl" idiom.
  38. class LoggerManager : public boost::noncopyable {
  39. public:
  40. /// \brief Constructor
  41. LoggerManager();
  42. /// \brief Destructor
  43. ~LoggerManager();
  44. /// \brief Process Specifications
  45. ///
  46. /// Replaces the current logging configuration by the one given.
  47. ///
  48. /// \param start Iterator pointing to the start of the collection of
  49. /// logging specifications.
  50. /// \param finish Iterator pointing to the end of the collection of
  51. /// logging specification.
  52. template <typename T>
  53. void process(T start, T finish) {
  54. processInit();
  55. for (T i = start; i != finish; ++i) {
  56. processSpecification(*i);
  57. }
  58. processEnd();
  59. }
  60. /// \brief Process a single specification
  61. ///
  62. /// A convenience function for a single specification.
  63. ///
  64. /// \param spec Specification to process
  65. void process(const LoggerSpecification& spec) {
  66. processInit();
  67. processSpecification(spec);
  68. processEnd();
  69. }
  70. /// \brief Process 'empty' specification
  71. ///
  72. /// This will disable any existing output options, and set
  73. /// the logging to go to the built-in default (stdout).
  74. /// If the logger has been initialized with buffering enabled,
  75. /// all log messages up to now shall be printed to stdout.
  76. ///
  77. /// This is mainly useful in scenarios where buffering is needed,
  78. /// but it turns out there are no logging specifications to
  79. /// handle.
  80. void process() {
  81. processInit();
  82. processEnd();
  83. }
  84. /// \brief Run-Time Initialization
  85. ///
  86. /// Performs run-time initialization of the logger system, in particular
  87. /// supplying the root logger name and name of a replacement message file.
  88. ///
  89. /// This must be the first logging function called in the program. If
  90. /// an attempt is made to log a message before this is function is called,
  91. /// the results will be dependent on the underlying logging package.
  92. ///
  93. /// Any duplicate log IDs encountered are reported as warning, after which
  94. /// the global duplicates vector is cleared
  95. ///
  96. /// \param root Name of the root logger. This should be set to the name of
  97. /// the program.
  98. /// \param severity Severity at which to log
  99. /// \param dbglevel Debug severity (ignored if "severity" is not "DEBUG")
  100. /// \param file Name of the local message file. This must be NULL if there
  101. /// is no local message file.
  102. /// \param buffer If true, all log messages will be buffered until one of
  103. /// the \c process() methods is called. If false, initial logging
  104. /// shall go to the default output (i.e. stdout)
  105. static void init(const std::string& root,
  106. isc::log::Severity severity = isc::log::INFO,
  107. int dbglevel = 0, const char* file = NULL,
  108. bool buffer = false);
  109. /// \brief List duplicated log messages.
  110. ///
  111. /// Lists the duplicated log messages using warning severity. Then, it
  112. /// clears the list of duplicated messages. This method is called by the
  113. /// \c init method and by the \c isc::hooks::LibraryManager when the new
  114. /// hooks library is loaded.
  115. static void logDuplicatedMessages();
  116. /// \brief Reset logging
  117. ///
  118. /// Resets logging to whatever was set in the call to init(), expect for
  119. /// the buffer option.
  120. static void reset();
  121. /// \brief Read local message file
  122. ///
  123. /// Reads the local message file into the global dictionary, overwriting
  124. /// existing messages. If the file contained any message IDs not in the
  125. /// dictionary, they are listed in a warning message.
  126. ///
  127. /// \param file Name of the local message file
  128. static void readLocalMessageFile(const char* file);
  129. /// \brief Return a process-global mutex that's used for mutual
  130. /// exclusion among threads of a single process during logging
  131. /// calls.
  132. static isc::util::thread::Mutex& getMutex();
  133. private:
  134. /// \brief Initialize Processing
  135. ///
  136. /// Initializes the processing of a list of specifications by resetting all
  137. /// loggers to their defaults, which is to pass the message to their
  138. /// parent logger. (Except for the root logger, where the default action is
  139. /// to output the message.)
  140. void processInit();
  141. /// \brief Process Logging Specification
  142. ///
  143. /// Processes the given specification. It is assumed at this point that
  144. /// either the logger does not exist or has been made inactive.
  145. void processSpecification(const LoggerSpecification& spec);
  146. /// \brief End Processing
  147. ///
  148. /// Place holder for finish processing.
  149. /// TODO: Check that the root logger has something enabled
  150. void processEnd();
  151. // Members
  152. LoggerManagerImpl* impl_; ///< Pointer to implementation
  153. };
  154. } // namespace log
  155. } // namespace isc
  156. #endif // LOGGER_MANAGER_H