loggingevent.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: loggingevent.h
  4. // Created: 6/2001
  5. // Author: Tad E. Smith
  6. //
  7. //
  8. // Copyright 2001-2015 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. * @param function Name of function that is logging this event.
  59. */
  60. InternalLoggingEvent(const log4cplus::tstring& logger,
  61. LogLevel loglevel, const log4cplus::tstring& message,
  62. const char* filename, int line, const char * function = NULL);
  63. InternalLoggingEvent(const log4cplus::tstring& logger,
  64. LogLevel loglevel, const log4cplus::tstring& ndc,
  65. MappedDiagnosticContextMap const & mdc,
  66. const log4cplus::tstring& message,
  67. const log4cplus::tstring& thread,
  68. log4cplus::helpers::Time time, const log4cplus::tstring& file,
  69. int line, const log4cplus::tstring & function
  70. = log4cplus::tstring ()) LOG4CPLUS_ATTRIBUTE_DEPRECATED;
  71. InternalLoggingEvent(const log4cplus::tstring& logger,
  72. LogLevel loglevel, const log4cplus::tstring& ndc,
  73. MappedDiagnosticContextMap const & mdc,
  74. const log4cplus::tstring& message,
  75. const log4cplus::tstring& thread,
  76. const log4cplus::tstring& thread2,
  77. log4cplus::helpers::Time time, const log4cplus::tstring& file,
  78. int line, const log4cplus::tstring & function
  79. = log4cplus::tstring ());
  80. InternalLoggingEvent ();
  81. InternalLoggingEvent(
  82. const log4cplus::spi::InternalLoggingEvent& rhs);
  83. virtual ~InternalLoggingEvent();
  84. void setLoggingEvent (const log4cplus::tstring & logger,
  85. LogLevel ll, const log4cplus::tstring & message,
  86. const char * filename, int line,
  87. const char * function = NULL);
  88. void setFunction (char const * func);
  89. void setFunction (log4cplus::tstring const &);
  90. // public virtual methods
  91. /** The application supplied message of logging event. */
  92. virtual const log4cplus::tstring& getMessage() const;
  93. /** Returns the 'type' of InternalLoggingEvent. Derived classes
  94. * should override this method. (NOTE: Values <= 1000 are
  95. * reserved for log4cplus and should not be used.)
  96. */
  97. virtual unsigned int getType() const;
  98. /** Returns a copy of this object. Derived classes
  99. * should override this method.
  100. */
  101. virtual std::auto_ptr<InternalLoggingEvent> clone() const;
  102. // public methods
  103. /** The logger of the logging event. It is set by
  104. * the LoggingEvent constructor.
  105. */
  106. const log4cplus::tstring& getLoggerName() const
  107. {
  108. return loggerName;
  109. }
  110. /** LogLevel of logging event. */
  111. LogLevel getLogLevel() const
  112. {
  113. return ll;
  114. }
  115. /** The nested diagnostic context (NDC) of logging event. */
  116. const log4cplus::tstring& getNDC() const
  117. {
  118. if (!ndcCached)
  119. {
  120. ndc = log4cplus::getNDC().get();
  121. ndcCached = true;
  122. }
  123. return ndc;
  124. }
  125. MappedDiagnosticContextMap const & getMDCCopy () const
  126. {
  127. if (!mdcCached)
  128. {
  129. mdc = log4cplus::getMDC().getContext ();
  130. mdcCached = true;
  131. }
  132. return mdc;
  133. }
  134. tstring const & getMDC (tstring const & key) const;
  135. /** The name of thread in which this logging event was generated. */
  136. const log4cplus::tstring& getThread() const
  137. {
  138. if (! threadCached)
  139. {
  140. thread = thread::getCurrentThreadName ();
  141. threadCached = true;
  142. }
  143. return thread;
  144. }
  145. //! The alternative name of thread in which this logging event
  146. //! was generated.
  147. const log4cplus::tstring& getThread2() const
  148. {
  149. if (! thread2Cached)
  150. {
  151. thread2 = thread::getCurrentThreadName2 ();
  152. thread2Cached = true;
  153. }
  154. return thread2;
  155. }
  156. /** The number of milliseconds elapsed from 1/1/1970 until
  157. * logging event was created. */
  158. const log4cplus::helpers::Time& getTimestamp() const
  159. {
  160. return timestamp;
  161. }
  162. /** The is the file where this log statement was written */
  163. const log4cplus::tstring& getFile() const
  164. {
  165. return file;
  166. }
  167. /** The is the line where this log statement was written */
  168. int getLine() const { return line; }
  169. log4cplus::tstring const & getFunction () const
  170. {
  171. return function;
  172. }
  173. void gatherThreadSpecificData () const;
  174. void swap (InternalLoggingEvent &);
  175. // public operators
  176. log4cplus::spi::InternalLoggingEvent&
  177. operator=(const log4cplus::spi::InternalLoggingEvent& rhs);
  178. // static methods
  179. static unsigned int getDefaultType();
  180. protected:
  181. // Data
  182. log4cplus::tstring message;
  183. log4cplus::tstring loggerName;
  184. LogLevel ll;
  185. mutable log4cplus::tstring ndc;
  186. mutable MappedDiagnosticContextMap mdc;
  187. mutable log4cplus::tstring thread;
  188. mutable log4cplus::tstring thread2;
  189. log4cplus::helpers::Time timestamp;
  190. log4cplus::tstring file;
  191. log4cplus::tstring function;
  192. int line;
  193. /** Indicates whether or not the Threadname has been retrieved. */
  194. mutable bool threadCached;
  195. mutable bool thread2Cached;
  196. /** Indicates whether or not the NDC has been retrieved. */
  197. mutable bool ndcCached;
  198. /** Indicates whether or not the MDC has been retrieved. */
  199. mutable bool mdcCached;
  200. };
  201. } // end namespace spi
  202. } // end namespace log4cplus
  203. #endif // LOG4CPLUS_SPI_INTERNAL_LOGGING_EVENT_HEADER_