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.

252 lines
6.0 KiB

  1. // Module: Log4CPLUS
  2. // File: loggingevent.cxx
  3. // Created: 6/2003
  4. // Author: Tad E. Smith
  5. //
  6. //
  7. // Copyright 2003-2013 Tad E. Smith
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. #include <log4cplus/spi/loggingevent.h>
  21. #include <log4cplus/internal/internal.h>
  22. #include <algorithm>
  23. namespace log4cplus { namespace spi {
  24. static const int LOG4CPLUS_DEFAULT_TYPE = 1;
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // InternalLoggingEvent ctors and dtor
  27. ///////////////////////////////////////////////////////////////////////////////
  28. InternalLoggingEvent::InternalLoggingEvent(const log4cplus::tstring& logger,
  29. LogLevel loglevel, const log4cplus::tstring& message_, const char* filename,
  30. int line_)
  31. : message(message_)
  32. , loggerName(logger)
  33. , ll(loglevel)
  34. , ndc()
  35. , mdc()
  36. , thread()
  37. , timestamp(log4cplus::helpers::Time::gettimeofday())
  38. , file(filename
  39. ? LOG4CPLUS_C_STR_TO_TSTRING(filename)
  40. : log4cplus::tstring())
  41. , function ()
  42. , line(line_)
  43. , threadCached(false)
  44. , thread2Cached(false)
  45. , ndcCached(false)
  46. , mdcCached(false)
  47. {
  48. }
  49. InternalLoggingEvent::InternalLoggingEvent(const log4cplus::tstring& logger,
  50. LogLevel loglevel, const log4cplus::tstring& ndc_,
  51. MappedDiagnosticContextMap const & mdc_, const log4cplus::tstring& message_,
  52. const log4cplus::tstring& thread_, log4cplus::helpers::Time time,
  53. const log4cplus::tstring& file_, int line_)
  54. : message(message_)
  55. , loggerName(logger)
  56. , ll(loglevel)
  57. , ndc(ndc_)
  58. , mdc(mdc_)
  59. , thread(thread_)
  60. , timestamp(time)
  61. , file(file_)
  62. , function ()
  63. , line(line_)
  64. , threadCached(true)
  65. , thread2Cached(true)
  66. , ndcCached(true)
  67. , mdcCached(true)
  68. {
  69. }
  70. InternalLoggingEvent::InternalLoggingEvent ()
  71. : ll (NOT_SET_LOG_LEVEL)
  72. , function ()
  73. , line (0)
  74. , threadCached(false)
  75. , thread2Cached(false)
  76. , ndcCached(false)
  77. , mdcCached(false)
  78. { }
  79. InternalLoggingEvent::InternalLoggingEvent(
  80. const log4cplus::spi::InternalLoggingEvent& rhs)
  81. : message(rhs.getMessage())
  82. , loggerName(rhs.getLoggerName())
  83. , ll(rhs.getLogLevel())
  84. , ndc(rhs.getNDC())
  85. , mdc(rhs.getMDCCopy())
  86. , thread(rhs.getThread())
  87. , timestamp(rhs.getTimestamp())
  88. , file(rhs.getFile())
  89. , function(rhs.getFunction())
  90. , line(rhs.getLine())
  91. , threadCached(true)
  92. , thread2Cached(true)
  93. , ndcCached(true)
  94. , mdcCached(true)
  95. {
  96. }
  97. InternalLoggingEvent::~InternalLoggingEvent()
  98. {
  99. }
  100. ///////////////////////////////////////////////////////////////////////////////
  101. // InternalLoggingEvent static methods
  102. ///////////////////////////////////////////////////////////////////////////////
  103. unsigned int
  104. InternalLoggingEvent::getDefaultType()
  105. {
  106. return LOG4CPLUS_DEFAULT_TYPE;
  107. }
  108. ///////////////////////////////////////////////////////////////////////////////
  109. // InternalLoggingEvent implementation
  110. ///////////////////////////////////////////////////////////////////////////////
  111. void
  112. InternalLoggingEvent::setLoggingEvent (const log4cplus::tstring & logger,
  113. LogLevel loglevel, const log4cplus::tstring & msg, const char * filename,
  114. int fline)
  115. {
  116. // This could be imlemented using the swap idiom:
  117. //
  118. // InternalLoggingEvent (logger, loglevel, msg, filename, fline).swap (*this);
  119. //
  120. // But that defeats the optimization of using thread local instance
  121. // of InternalLoggingEvent to avoid memory allocation.
  122. loggerName = logger;
  123. ll = loglevel;
  124. message = msg;
  125. timestamp = helpers::Time::gettimeofday();
  126. if (filename)
  127. file = LOG4CPLUS_C_STR_TO_TSTRING (filename);
  128. else
  129. file.clear ();
  130. line = fline;
  131. threadCached = false;
  132. thread2Cached = false;
  133. ndcCached = false;
  134. mdcCached = false;
  135. }
  136. void
  137. InternalLoggingEvent::setFunction (char const * func)
  138. {
  139. function = LOG4CPLUS_C_STR_TO_TSTRING (func);
  140. }
  141. void
  142. InternalLoggingEvent::setFunction (log4cplus::tstring const & func)
  143. {
  144. function = func;
  145. }
  146. const log4cplus::tstring&
  147. InternalLoggingEvent::getMessage() const
  148. {
  149. return message;
  150. }
  151. unsigned int
  152. InternalLoggingEvent::getType() const
  153. {
  154. return LOG4CPLUS_DEFAULT_TYPE;
  155. }
  156. std::auto_ptr<InternalLoggingEvent>
  157. InternalLoggingEvent::clone() const
  158. {
  159. std::auto_ptr<InternalLoggingEvent> tmp(new InternalLoggingEvent(*this));
  160. return tmp;
  161. }
  162. tstring const &
  163. InternalLoggingEvent::getMDC (tstring const & key) const
  164. {
  165. MappedDiagnosticContextMap const & mdc_ = getMDCCopy ();
  166. MappedDiagnosticContextMap::const_iterator it = mdc_.find (key);
  167. if (it != mdc_.end ())
  168. return it->second;
  169. else
  170. return internal::empty_str;
  171. }
  172. InternalLoggingEvent &
  173. InternalLoggingEvent::operator = (const InternalLoggingEvent& rhs)
  174. {
  175. InternalLoggingEvent (rhs).swap (*this);
  176. return *this;
  177. }
  178. void
  179. InternalLoggingEvent::gatherThreadSpecificData () const
  180. {
  181. getNDC ();
  182. getMDCCopy ();
  183. getThread ();
  184. getThread2 ();
  185. }
  186. void
  187. InternalLoggingEvent::swap (InternalLoggingEvent & other)
  188. {
  189. using std::swap;
  190. swap (message, other.message);
  191. swap (loggerName, other.loggerName);
  192. swap (ll, other.ll);
  193. swap (ndc, other.ndc);
  194. swap (mdc, other.mdc);
  195. swap (thread, other.thread);
  196. swap (thread2, other.thread2);
  197. swap (timestamp, other.timestamp);
  198. swap (file, other.file);
  199. swap (function, other.function);
  200. swap (line, other.line);
  201. swap (threadCached, other.threadCached);
  202. swap (ndcCached, other.ndcCached);
  203. }
  204. } } // namespace log4cplus { namespace spi {