You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
7.0 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: loglevel.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2013 Tad E. Smith
  9. //
  10. // Licensed under the Apache License, Version 2.0 (the "License");
  11. // you may not use this file except in compliance with the License.
  12. // You may obtain a copy of the License at
  13. //
  14. // http://www.apache.org/licenses/LICENSE-2.0
  15. //
  16. // Unless required by applicable law or agreed to in writing, software
  17. // distributed under the License is distributed on an "AS IS" BASIS,
  18. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. // See the License for the specific language governing permissions and
  20. // limitations under the License.
  21. /** @file
  22. * This header defines the LogLevel type.
  23. */
  24. #ifndef LOG4CPLUS_LOGLEVEL_HEADER_
  25. #define LOG4CPLUS_LOGLEVEL_HEADER_
  26. #include <log4cplus/config.hxx>
  27. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  28. #pragma once
  29. #endif
  30. #include <vector>
  31. #include <log4cplus/tstring.h>
  32. namespace log4cplus {
  33. /**
  34. * \typedef int LogLevel
  35. * Defines the minimum set of priorities recognized by the system,
  36. * that is {@link #FATAL_LOG_LEVEL}, {@link #ERROR_LOG_LEVEL}, {@link
  37. * #WARN_LOG_LEVEL}, {@link #INFO_LOG_LEVEL}, {@link #DEBUG_LOG_LEVEL},
  38. * and {@link #TRACE_LOG_LEVEL}.
  39. */
  40. typedef int LogLevel;
  41. /** \var const LogLevel OFF_LOG_LEVEL
  42. * The <code>OFF_LOG_LEVEL</code> LogLevel is used during configuration to
  43. * turn off logging. */
  44. const LogLevel OFF_LOG_LEVEL = 60000;
  45. /** \var const LogLevel FATAL_LOG_LEVEL
  46. * The <code>FATAL_LOG_LEVEL</code> LogLevel designates very severe error
  47. * events that will presumably lead the application to abort. */
  48. const LogLevel FATAL_LOG_LEVEL = 50000;
  49. /** \var const LogLevel ERROR_LOG_LEVEL
  50. * The <code>ERROR_LOG_LEVEL</code> LogLevel designates error events that
  51. * might still allow the application to continue running. */
  52. const LogLevel ERROR_LOG_LEVEL = 40000;
  53. /** \var const LogLevel WARN_LOG_LEVEL
  54. * The <code>WARN_LOG_LEVEL</code> LogLevel designates potentially harmful
  55. * situations. */
  56. const LogLevel WARN_LOG_LEVEL = 30000;
  57. /** \var const LogLevel INFO_LOG_LEVEL
  58. * The <code>INFO_LOG_LEVEL</code> LogLevel designates informational
  59. * messages that highlight the progress of the application at
  60. * coarse-grained level. */
  61. const LogLevel INFO_LOG_LEVEL = 20000;
  62. /** \var const LogLevel DEBUG_LOG_LEVEL
  63. * The <code>DEBUG_LOG_LEVEL</code> LogLevel designates fine-grained
  64. * informational events that are most useful to debug an application. */
  65. const LogLevel DEBUG_LOG_LEVEL = 10000;
  66. /** \var const LogLevel TRACE_LOG_LEVEL
  67. * The <code>TRACE_LOG_LEVEL</code> LogLevel is used to "trace" entry
  68. * and exiting of methods. */
  69. const LogLevel TRACE_LOG_LEVEL = 0;
  70. /** \var const LogLevel ALL_LOG_LEVEL
  71. * The <code>ALL_LOG_LEVEL</code> LogLevel is used during configuration to
  72. * turn on all logging. */
  73. const LogLevel ALL_LOG_LEVEL = TRACE_LOG_LEVEL;
  74. /** \var const LogLevel NOT_SET_LOG_LEVEL
  75. * The <code>NOT_SET_LOG_LEVEL</code> LogLevel is used to indicated that
  76. * no particular LogLevel is desired and that the default should be used.
  77. */
  78. const LogLevel NOT_SET_LOG_LEVEL = -1;
  79. /**
  80. * This method type defined the signature of methods that convert LogLevels
  81. * into strings.
  82. *
  83. * <b>Note:</b> Must return an empty <code>tstring</code> for unrecognized values.
  84. */
  85. typedef log4cplus::tstring const & (*LogLevelToStringMethod)(LogLevel);
  86. //! This function type is for log4cplus 1.0.x callbacks.
  87. typedef log4cplus::tstring (*LogLevelToStringMethod_1_0) (LogLevel);
  88. /**
  89. * This method type defined the signature of methods that convert strings
  90. * into LogLevels.
  91. *
  92. * <b>Note:</b> Must return <code>NOT_SET_LOG_LEVEL</code> for unrecognized values.
  93. */
  94. typedef LogLevel (*StringToLogLevelMethod)(const log4cplus::tstring&);
  95. /**
  96. * This class is used to "manage" LogLevel definitions. This class is also
  97. * how "derived" LogLevels are created. Here are the steps to creating a
  98. * "derived" LogLevel:
  99. * <ol>
  100. * <li>Create a LogLevel constant (greater than 0)</li>
  101. * <li>Define a string to represent that constant</li>
  102. * <li>Implement a LogLevelToStringMethod method.</li>
  103. * <li>Implement a StringToLogLevelMethod method.</li>
  104. * <li>create a "static initializer" that registers those 2 methods
  105. * with the LogLevelManager singleton.</li>
  106. * </ol>
  107. */
  108. class LOG4CPLUS_EXPORT LogLevelManager {
  109. public:
  110. LogLevelManager();
  111. ~LogLevelManager();
  112. /**
  113. * This method is called by all Layout classes to convert a LogLevel
  114. * into a string.
  115. *
  116. * Note: It traverses the list of <code>LogLevelToStringMethod</code>
  117. * to do this, so all "derived" LogLevels are recognized as well.
  118. */
  119. log4cplus::tstring const & toString(LogLevel ll) const;
  120. /**
  121. * This method is called by all classes internally to log4cplus to
  122. * convert a string into a LogLevel.
  123. *
  124. * Note: It traverses the list of <code>StringToLogLevelMethod</code>
  125. * to do this, so all "derived" LogLevels are recognized as well.
  126. */
  127. LogLevel fromString(const log4cplus::tstring& s) const;
  128. /**
  129. * When creating a "derived" LogLevel, a <code>LogLevelToStringMethod</code>
  130. * should be defined and registered with the LogLevelManager by calling
  131. * this method.
  132. *
  133. * @see pushFromStringMethod
  134. */
  135. void pushToStringMethod(LogLevelToStringMethod newToString);
  136. //! For compatibility with log4cplus 1.0.x.
  137. void pushToStringMethod(LogLevelToStringMethod_1_0 newToString);
  138. /**
  139. * When creating a "derived" LogLevel, a <code>StringToLogLevelMethod</code>
  140. * should be defined and registered with the LogLevelManager by calling
  141. * this method.
  142. *
  143. * @see pushToStringMethod
  144. */
  145. void pushFromStringMethod(StringToLogLevelMethod newFromString);
  146. private:
  147. // Data
  148. struct LogLevelToStringMethodRec
  149. {
  150. union
  151. {
  152. LogLevelToStringMethod func;
  153. LogLevelToStringMethod_1_0 func_1_0;
  154. };
  155. bool use_1_0;
  156. };
  157. typedef std::vector<LogLevelToStringMethodRec> LogLevelToStringMethodList;
  158. LogLevelToStringMethodList toStringMethods;
  159. typedef std::vector<StringToLogLevelMethod> StringToLogLevelMethodList;
  160. StringToLogLevelMethodList fromStringMethods;
  161. // Disable Copy
  162. LogLevelManager(const LogLevelManager&);
  163. LogLevelManager& operator=(const LogLevelManager&);
  164. };
  165. /**
  166. * Returns the singleton LogLevelManager.
  167. */
  168. LOG4CPLUS_EXPORT LogLevelManager& getLogLevelManager();
  169. }
  170. #endif // LOG4CPLUS_LOGLEVEL_HEADER_