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.

332 lines
5.5 KiB

  1. // Module: Log4CPLUS
  2. // File: logger.cxx
  3. // Created: 6/2001
  4. // Author: Tad E. Smith
  5. //
  6. //
  7. // Copyright 2001-2010 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/logger.h>
  21. #include <log4cplus/appender.h>
  22. #include <log4cplus/hierarchy.h>
  23. #include <log4cplus/helpers/loglog.h>
  24. #include <log4cplus/spi/loggerimpl.h>
  25. #include <utility>
  26. namespace log4cplus
  27. {
  28. Logger
  29. DefaultLoggerFactory::makeNewLoggerInstance (const log4cplus::tstring & name,
  30. Hierarchy& h)
  31. {
  32. return Logger (new spi::LoggerImpl (name, h));
  33. }
  34. //////////////////////////////////////////////////////////////////////////////
  35. // static Logger Methods
  36. //////////////////////////////////////////////////////////////////////////////
  37. //
  38. Hierarchy &
  39. Logger::getDefaultHierarchy ()
  40. {
  41. return log4cplus::getDefaultHierarchy ();
  42. }
  43. bool
  44. Logger::exists (const log4cplus::tstring & name)
  45. {
  46. return getDefaultHierarchy().exists(name);
  47. }
  48. LoggerList
  49. Logger::getCurrentLoggers ()
  50. {
  51. return getDefaultHierarchy ().getCurrentLoggers ();
  52. }
  53. Logger
  54. Logger::getInstance (const log4cplus::tstring& name)
  55. {
  56. return getDefaultHierarchy().getInstance(name);
  57. }
  58. Logger
  59. Logger::getInstance (const log4cplus::tstring& name,
  60. spi::LoggerFactory& factory)
  61. {
  62. return getDefaultHierarchy().getInstance(name, factory);
  63. }
  64. Logger
  65. Logger::getRoot ()
  66. {
  67. return getDefaultHierarchy ().getRoot ();
  68. }
  69. void
  70. Logger::shutdown ()
  71. {
  72. getDefaultHierarchy ().shutdown ();
  73. }
  74. //////////////////////////////////////////////////////////////////////////////
  75. // Logger ctors and dtor
  76. //////////////////////////////////////////////////////////////////////////////
  77. Logger::Logger ()
  78. : value (0)
  79. { }
  80. Logger::Logger (spi::LoggerImpl * ptr)
  81. : value (ptr)
  82. {
  83. if (value)
  84. value->addReference ();
  85. }
  86. Logger::Logger (const Logger& rhs)
  87. : spi::AppenderAttachable (rhs)
  88. , value (rhs.value)
  89. {
  90. if (value)
  91. value->addReference ();
  92. }
  93. Logger &
  94. Logger::operator = (const Logger& rhs)
  95. {
  96. Logger (rhs).swap (*this);
  97. return *this;
  98. }
  99. #if defined (LOG4CPLUS_HAVE_RVALUE_REFS)
  100. Logger::Logger (Logger && rhs)
  101. : spi::AppenderAttachable (std::move (rhs))
  102. , value (std::move (rhs.value))
  103. {
  104. rhs.value = 0;
  105. }
  106. Logger &
  107. Logger::operator = (Logger && rhs)
  108. {
  109. Logger (std::move (rhs)).swap (*this);
  110. return *this;
  111. }
  112. #endif
  113. Logger::~Logger ()
  114. {
  115. if (value)
  116. value->removeReference ();
  117. }
  118. //////////////////////////////////////////////////////////////////////////////
  119. // Logger Methods
  120. //////////////////////////////////////////////////////////////////////////////
  121. void
  122. Logger::swap (Logger & other)
  123. {
  124. std::swap (value, other.value);
  125. }
  126. Logger
  127. Logger::getParent () const
  128. {
  129. if (value->parent)
  130. return Logger (value->parent.get ());
  131. else
  132. {
  133. helpers::getLogLog().error(
  134. LOG4CPLUS_TEXT("********* This logger has no parent: "
  135. + getName()));
  136. return *this;
  137. }
  138. }
  139. void
  140. Logger::addAppender (SharedAppenderPtr newAppender)
  141. {
  142. value->addAppender(newAppender);
  143. }
  144. SharedAppenderPtrList
  145. Logger::getAllAppenders ()
  146. {
  147. return value->getAllAppenders();
  148. }
  149. SharedAppenderPtr
  150. Logger::getAppender (const log4cplus::tstring& name)
  151. {
  152. return value->getAppender (name);
  153. }
  154. void
  155. Logger::removeAllAppenders ()
  156. {
  157. value->removeAllAppenders ();
  158. }
  159. void
  160. Logger::removeAppender (SharedAppenderPtr appender)
  161. {
  162. value->removeAppender(appender);
  163. }
  164. void
  165. Logger::removeAppender (const log4cplus::tstring& name)
  166. {
  167. value->removeAppender (name);
  168. }
  169. void
  170. Logger::assertion (bool assertionVal, const log4cplus::tstring& msg) const
  171. {
  172. if (! assertionVal)
  173. log (FATAL_LOG_LEVEL, msg);
  174. }
  175. void
  176. Logger::closeNestedAppenders () const
  177. {
  178. value->closeNestedAppenders ();
  179. }
  180. bool
  181. Logger::isEnabledFor (LogLevel ll) const
  182. {
  183. return value->isEnabledFor (ll);
  184. }
  185. void
  186. Logger::log (LogLevel ll, const log4cplus::tstring& message, const char* file,
  187. int line) const
  188. {
  189. value->log (ll, message, file, line);
  190. }
  191. void
  192. Logger::log (spi::InternalLoggingEvent const & ev) const
  193. {
  194. value->log (ev);
  195. }
  196. void
  197. Logger::forcedLog (LogLevel ll, const log4cplus::tstring& message,
  198. const char* file, int line) const
  199. {
  200. value->forcedLog (ll, message, file, line);
  201. }
  202. void
  203. Logger::forcedLog (spi::InternalLoggingEvent const & ev) const
  204. {
  205. value->forcedLog (ev);
  206. }
  207. void
  208. Logger::callAppenders (const spi::InternalLoggingEvent& event) const
  209. {
  210. value->callAppenders (event);
  211. }
  212. LogLevel
  213. Logger::getChainedLogLevel () const
  214. {
  215. return value->getChainedLogLevel ();
  216. }
  217. LogLevel
  218. Logger::getLogLevel() const
  219. {
  220. return value->getLogLevel ();
  221. }
  222. void
  223. Logger::setLogLevel (LogLevel ll)
  224. {
  225. value->setLogLevel (ll);
  226. }
  227. Hierarchy &
  228. Logger::getHierarchy () const
  229. {
  230. return value->getHierarchy ();
  231. }
  232. log4cplus::tstring const &
  233. Logger::getName () const
  234. {
  235. return value->getName ();
  236. }
  237. bool
  238. Logger::getAdditivity () const
  239. {
  240. return value->getAdditivity ();
  241. }
  242. void
  243. Logger::setAdditivity (bool additive)
  244. {
  245. value->setAdditivity (additive);
  246. }
  247. } // namespace log4cplus