logger.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright (C) 2010 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. // $Id$
  15. #ifndef __LOGGER_H
  16. #define __LOGGER_H
  17. #include <cstdlib>
  18. #include <string>
  19. #include <boost/lexical_cast.hpp>
  20. #include <log4cxx/logger.h>
  21. #include <log/dbglevels.h>
  22. #include <log/message_types.h>
  23. namespace isc {
  24. namespace log {
  25. class Logger {
  26. public:
  27. /// \brief Severity Levels
  28. typedef enum {
  29. DEFAULT, // Default to logging level of parent
  30. DEBUG,
  31. INFO,
  32. WARN,
  33. ERROR,
  34. FATAL,
  35. NONE // Disable logging
  36. } Severity;
  37. /// \brief Constructor
  38. ///
  39. /// Creates/attaches to a logger of a specific name.
  40. ///
  41. /// \param name Name of the logger. If the name is that of the root name,
  42. /// this creates an instance of the root logger; otherwise it creates a
  43. /// child of the root logger.
  44. ///
  45. /// \param exit_delete This argument is present to get round a bug in
  46. /// log4cxx. If a log4cxx logger is declared outside an execution unit, it
  47. /// is not deleted until the program runs down. At that point all such
  48. /// objects - including internal log4cxx objects - are deleted. However,
  49. /// there seems to be a bug in log4cxx where the way that such objects are
  50. /// destroyed causes a MutexException to be thrown (this is described in
  51. /// https://issues.apache.org/jira/browse/LOGCXX-322). As this only occurs
  52. /// during program rundown, the issue is not serious - it just looks bad to
  53. /// have the program crash instead of shut down cleanly.\n
  54. /// \n
  55. /// The original implementation of the isc::log::Logger had as a member a
  56. /// log4cxx logger (actually a LoggerPtr). If the isc:: Logger was declared
  57. /// statically, when it was destroyed at the end of the program the internal
  58. /// LoggerPtr was destroyed, which triggered the problem. The problem did
  59. /// not occur if the isc::log::Logger was created on the stack. To get
  60. /// round this, the internal LoggerPtr is now created dynamically. The
  61. /// exit_delete argument controls its destruction: if true, it is destroyed
  62. /// in the ISC Logger destructor. If false, it is not.\n
  63. /// \n
  64. /// When creating an isc::log::Logger on the stack, the argument should be
  65. /// false (the default); when the Logger is destroyed, all the internal
  66. /// log4cxx objects are destroyed. As only the logger (and not the internal
  67. /// log4cxx data structures are being destroyed), all is well. However,
  68. /// when creating the logger statically, the argument should be false. This
  69. /// means that the log4cxx objects are not destroyed at program rundown;
  70. /// instead memory is reclaimed and files are closed when the process is
  71. /// destroyed, something that does not trigger the bug.
  72. Logger(const std::string& name, bool exit_delete = false) :
  73. loggerptr_(), name_(name), exit_delete_(exit_delete)
  74. {}
  75. /// \brief Destructor
  76. virtual ~Logger();
  77. /// \brief Configure Options
  78. ///
  79. /// TEMPORARY: Pass in the command-line options to set the logging severity
  80. /// for the root logger. Future versions of the logger will get this
  81. /// information from the configuration database.
  82. ///
  83. /// \param severity Severity level to log
  84. /// \param dbglevel If the severity is DEBUG, this is the debug level.
  85. /// This can be in the range 1 to 100 and controls the verbosity. A value
  86. /// outside these limits is silently coerced to the nearest boundary.
  87. /// \param local_file If provided, the name of a message file to read in and
  88. /// supersede one or more of the current messages.
  89. static void runTimeInit(Severity severity = INFO, int dbglevel = 1,
  90. const char* local_file = NULL);
  91. /// \brief Get Name of Logger
  92. ///
  93. /// \return The full name of the logger (including the root name)
  94. virtual std::string getName() {
  95. return getLogger()->getName();
  96. }
  97. /// \brief Set Severity Level for Logger
  98. ///
  99. /// Sets the level at which this logger will log messages. If none is set,
  100. /// the level is inherited from the parent.
  101. ///
  102. /// \param severity Severity level to log
  103. /// \param dbglevel If the severity is DEBUG, this is the debug level.
  104. /// This can be in the range 1 to 100 and controls the verbosity. A value
  105. /// outside these limits is silently coerced to the nearest boundary.
  106. virtual void setSeverity(Severity severity, int dbglevel = 1);
  107. /// \brief Get Severity Level for Logger
  108. ///
  109. /// \return The current logging level of this logger. In most cases though,
  110. /// the effective logging level is what is required.
  111. virtual Severity getSeverity() {
  112. return getSeverityCommon(getLogger(), false);
  113. }
  114. /// \brief Get Effective Severity Level for Logger
  115. ///
  116. /// \return The effective severity level of the logger. This is the same
  117. /// as getSeverity() if the logger has a severity level set, but otherwise
  118. /// is the severity of the parent.
  119. virtual Severity getEffectiveSeverity() {
  120. return getSeverityCommon(getLogger(), true);
  121. }
  122. /// \brief Return DEBUG Level
  123. ///
  124. /// \return Current setting of debug level. This is returned regardless of
  125. /// whether the
  126. virtual int getDebugLevel();
  127. /// \brief Returns if Debug Message Should Be Output
  128. ///
  129. /// \param dbglevel Level for which debugging is checked. Debugging is
  130. /// enabled only if the logger has DEBUG enabled and if the dbglevel
  131. /// checked is less than or equal to the debug level set for the logger.
  132. virtual bool
  133. isDebugEnabled(int dbglevel = MIN_DEBUG_LEVEL) {
  134. return (getLogger()->getEffectiveLevel()->toInt() <=
  135. (log4cxx::Level::DEBUG_INT - dbglevel));
  136. }
  137. /// \brief Is INFO Enabled?
  138. virtual bool isInfoEnabled() {
  139. return (getLogger()->isInfoEnabled());
  140. }
  141. /// \brief Is WARNING Enabled?
  142. virtual bool isWarnEnabled() {
  143. return (getLogger()->isWarnEnabled());
  144. }
  145. /// \brief Is ERROR Enabled?
  146. virtual bool isErrorEnabled() {
  147. return (getLogger()->isErrorEnabled());
  148. }
  149. /// \brief Is FATAL Enabled?
  150. virtual bool isFatalEnabled() {
  151. return (getLogger()->isFatalEnabled());
  152. }
  153. /// \brief Output Debug Message
  154. ///
  155. /// \param dbglevel Debug level, ranging between 0 and 99. Higher numbers
  156. /// are used for more verbose output.
  157. /// \param ident Message identification.
  158. /// \param ... Optional arguments for the message.
  159. void debug(int dbglevel, MessageID ident, ...);
  160. /// \brief Output Informational Message
  161. ///
  162. /// \param ident Message identification.
  163. /// \param ... Optional arguments for the message.
  164. void info(MessageID ident, ...);
  165. /// \brief Output Warning Message
  166. ///
  167. /// \param ident Message identification.
  168. /// \param ... Optional arguments for the message.
  169. void warn(MessageID ident, ...);
  170. /// \brief Output Error Message
  171. ///
  172. /// \param ident Message identification.
  173. /// \param ... Optional arguments for the message.
  174. void error(MessageID ident, ...);
  175. /// \brief Output Fatal Message
  176. ///
  177. /// \param ident Message identification.
  178. /// \param ... Optional arguments for the message.
  179. void fatal(MessageID ident, ...);
  180. protected:
  181. /// \brief Equality
  182. ///
  183. /// Check if two instances of this logger refer to the same stream.
  184. /// (This method is principally for testing.)
  185. ///
  186. /// \return true if the logger objects are instances of the same logger.
  187. bool operator==(const Logger& other) const {
  188. return (*loggerptr_ == *other.loggerptr_);
  189. }
  190. /// \brief Logger Initialized
  191. ///
  192. /// Check that the logger has been properly initialized. (This method
  193. /// is principally for testing.)
  194. ///
  195. /// \return true if this logger object has been initialized.
  196. bool isInitialized() const {
  197. return (loggerptr_ != NULL);
  198. }
  199. /// \brief Get Severity Level for Logger
  200. ///
  201. /// This is common code for getSeverity() and getEffectiveSeverity() -
  202. /// it returns the severity of the logger; if not set (and the check_parent)
  203. /// flag is set, it searches up the parent-child tree until a severity
  204. /// level is found and uses that.
  205. ///
  206. /// \param ptrlogger Pointer to the log4cxx logger to check.
  207. /// \param check_parent true to search up the tree, false to return the
  208. /// current level.
  209. ///
  210. /// \return The effective severity level of the logger. This is the same
  211. /// as getSeverity() if the logger has a severity level set, but otherwise
  212. /// is the severity of the parent.
  213. Logger::Severity getSeverityCommon(const log4cxx::LoggerPtr& ptrlogger,
  214. bool check_parent) const;
  215. /// \brief Convert Between BIND-10 and log4cxx Logging Levels
  216. ///
  217. /// Converts between the numeric value of the log4cxx logging level
  218. /// and the BIND-10 severity level.
  219. ///
  220. /// \param value log4cxx numeric logging level
  221. ///
  222. /// \return BIND-10 logging severity
  223. Severity convertLevel(int value) const;
  224. /// \brief Initialize log4cxx Logger
  225. ///
  226. /// Creates the log4cxx logger used internally. A function is provided for
  227. /// this so that the creation does not take place when this Logger object
  228. /// is created but when it is used. As the latter occurs in executable
  229. /// code but the former can occur during initialization, this order
  230. /// guarantees that anything that is statically initialized has completed
  231. /// its initialization by the time the logger is used.
  232. void initLogger();
  233. /// \brief Return log4cxx Logger
  234. ///
  235. /// Returns the log4cxx logger, initializing it if not already initialized.
  236. ///
  237. /// \return Loggerptr object
  238. log4cxx::LoggerPtr& getLogger() {
  239. if (loggerptr_ == NULL) {
  240. initLogger();
  241. }
  242. return *loggerptr_;
  243. }
  244. /// \brief Read Local Message File
  245. ///
  246. /// Reads a local message file into the global dictionary, replacing any
  247. /// definitions there. Any messages found in the local file that do not
  248. /// replace ones in the global dictionary are listed.
  249. ///
  250. /// \param file Local message file to be read.
  251. static void readLocalMessageFile(const char* file);
  252. private:
  253. // Note that loggerptr_ is a pointer to a LoggerPtr, which is itself a
  254. // pointer to the underlying log4cxx logger. This is due to the problems
  255. // with memory deletion on program exit, explained in the comments for
  256. // the "exit_delete" parameter in this class's constructor.
  257. log4cxx::LoggerPtr* loggerptr_; ///< Pointer to the underlying logger
  258. std::string name_; ///< Name of this logger]
  259. bool exit_delete_; ///< Delete loggerptr_ on exit?
  260. // NOTE - THIS IS A PLACE HOLDER
  261. static bool init_; ///< Set true when initialized
  262. };
  263. } // namespace log
  264. } // namespace isc
  265. #endif // __LOGGER_H