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.

228 lines
7.6 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: loggingevent.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2010 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. #ifndef LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_
  23. #define LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_
  24. #include <log4cplus/config.hxx>
  25. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  26. #pragma once
  27. #endif
  28. #include <memory>
  29. #include <log4cplus/loglevel.h>
  30. #include <log4cplus/ndc.h>
  31. #include <log4cplus/mdc.h>
  32. #include <log4cplus/tstring.h>
  33. #include <log4cplus/helpers/timehelper.h>
  34. #include <log4cplus/thread/threads.h>
  35. namespace log4cplus {
  36. namespace spi {
  37. /**
  38. * The internal representation of logging events. When an affirmative
  39. * decision is made to log then a <code>InternalLoggingEvent</code>
  40. * instance is created. This instance is passed around to the
  41. * different log4cplus components.
  42. *
  43. * This class is of concern to those wishing to extend log4cplus.
  44. */
  45. class LOG4CPLUS_EXPORT InternalLoggingEvent {
  46. public:
  47. // Ctors
  48. /**
  49. * Instantiate a LoggingEvent from the supplied parameters.
  50. *
  51. * @param logger The logger of this event.
  52. * @param loglevel The LogLevel of this event.
  53. * @param message The message of this event.
  54. * @param filename Name of file where this event has occurred,
  55. * can be NULL.
  56. * @param line Line number in file specified by
  57. * the <code>filename</code> parameter.
  58. */
  59. InternalLoggingEvent(const log4cplus::tstring& logger,
  60. LogLevel loglevel, const log4cplus::tstring& message,
  61. const char* filename, int line);
  62. InternalLoggingEvent(const log4cplus::tstring& logger,
  63. LogLevel loglevel, const log4cplus::tstring& ndc,
  64. MappedDiagnosticContextMap const & mdc,
  65. const log4cplus::tstring& message,
  66. const log4cplus::tstring& thread,
  67. log4cplus::helpers::Time time, const log4cplus::tstring& file,
  68. int line);
  69. InternalLoggingEvent ();
  70. InternalLoggingEvent(
  71. const log4cplus::spi::InternalLoggingEvent& rhs);
  72. virtual ~InternalLoggingEvent();
  73. void setLoggingEvent (const log4cplus::tstring & logger,
  74. LogLevel ll, const log4cplus::tstring & message,
  75. const char * filename, int line);
  76. void setFunction (char const * func);
  77. void setFunction (log4cplus::tstring const &);
  78. // public virtual methods
  79. /** The application supplied message of logging event. */
  80. virtual const log4cplus::tstring& getMessage() const;
  81. /** Returns the 'type' of InternalLoggingEvent. Derived classes
  82. * should override this method. (NOTE: Values <= 1000 are
  83. * reserved for log4cplus and should not be used.)
  84. */
  85. virtual unsigned int getType() const;
  86. /** Returns a copy of this object. Derived classes
  87. * should override this method.
  88. */
  89. virtual std::auto_ptr<InternalLoggingEvent> clone() const;
  90. // public methods
  91. /** The logger of the logging event. It is set by
  92. * the LoggingEvent constructor.
  93. */
  94. const log4cplus::tstring& getLoggerName() const
  95. {
  96. return loggerName;
  97. }
  98. /** LogLevel of logging event. */
  99. LogLevel getLogLevel() const
  100. {
  101. return ll;
  102. }
  103. /** The nested diagnostic context (NDC) of logging event. */
  104. const log4cplus::tstring& getNDC() const
  105. {
  106. if (!ndcCached)
  107. {
  108. ndc = log4cplus::getNDC().get();
  109. ndcCached = true;
  110. }
  111. return ndc;
  112. }
  113. MappedDiagnosticContextMap const & getMDCCopy () const
  114. {
  115. if (!mdcCached)
  116. {
  117. mdc = log4cplus::getMDC().getContext ();
  118. mdcCached = true;
  119. }
  120. return mdc;
  121. }
  122. tstring const & getMDC (tstring const & key) const;
  123. /** The name of thread in which this logging event was generated. */
  124. const log4cplus::tstring& getThread() const
  125. {
  126. if (! threadCached)
  127. {
  128. thread = thread::getCurrentThreadName ();
  129. threadCached = true;
  130. }
  131. return thread;
  132. }
  133. //! The alternative name of thread in which this logging event
  134. //! was generated.
  135. const log4cplus::tstring& getThread2() const
  136. {
  137. if (! thread2Cached)
  138. {
  139. thread2 = thread::getCurrentThreadName2 ();
  140. thread2Cached = true;
  141. }
  142. return thread2;
  143. }
  144. /** The number of milliseconds elapsed from 1/1/1970 until
  145. * logging event was created. */
  146. const log4cplus::helpers::Time& getTimestamp() const
  147. {
  148. return timestamp;
  149. }
  150. /** The is the file where this log statement was written */
  151. const log4cplus::tstring& getFile() const
  152. {
  153. return file;
  154. }
  155. /** The is the line where this log statement was written */
  156. int getLine() const { return line; }
  157. log4cplus::tstring const & getFunction () const
  158. {
  159. return function;
  160. }
  161. void gatherThreadSpecificData () const;
  162. void swap (InternalLoggingEvent &);
  163. // public operators
  164. log4cplus::spi::InternalLoggingEvent&
  165. operator=(const log4cplus::spi::InternalLoggingEvent& rhs);
  166. // static methods
  167. static unsigned int getDefaultType();
  168. protected:
  169. // Data
  170. log4cplus::tstring message;
  171. log4cplus::tstring loggerName;
  172. LogLevel ll;
  173. mutable log4cplus::tstring ndc;
  174. mutable MappedDiagnosticContextMap mdc;
  175. mutable log4cplus::tstring thread;
  176. mutable log4cplus::tstring thread2;
  177. log4cplus::helpers::Time timestamp;
  178. log4cplus::tstring file;
  179. log4cplus::tstring function;
  180. int line;
  181. /** Indicates whether or not the Threadname has been retrieved. */
  182. mutable bool threadCached;
  183. mutable bool thread2Cached;
  184. /** Indicates whether or not the NDC has been retrieved. */
  185. mutable bool ndcCached;
  186. /** Indicates whether or not the MDC has been retrieved. */
  187. mutable bool mdcCached;
  188. };
  189. } // end namespace spi
  190. } // end namespace log4cplus
  191. #endif // LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_