appender.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Module: Log4CPLUS
  2. // File: appender.cxx
  3. // Created: 6/2001
  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/appender.h>
  21. #include <log4cplus/layout.h>
  22. #include <log4cplus/helpers/loglog.h>
  23. #include <log4cplus/helpers/pointer.h>
  24. #include <log4cplus/helpers/stringhelper.h>
  25. #include <log4cplus/helpers/property.h>
  26. #include <log4cplus/spi/factory.h>
  27. #include <log4cplus/spi/loggingevent.h>
  28. #include <log4cplus/internal/internal.h>
  29. #include <log4cplus/thread/syncprims-pub-impl.h>
  30. #include <stdexcept>
  31. namespace log4cplus
  32. {
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // log4cplus::ErrorHandler dtor
  35. ///////////////////////////////////////////////////////////////////////////////
  36. ErrorHandler::ErrorHandler ()
  37. { }
  38. ErrorHandler::~ErrorHandler()
  39. { }
  40. ///////////////////////////////////////////////////////////////////////////////
  41. // log4cplus::OnlyOnceErrorHandler
  42. ///////////////////////////////////////////////////////////////////////////////
  43. OnlyOnceErrorHandler::OnlyOnceErrorHandler()
  44. : firstTime(true)
  45. { }
  46. OnlyOnceErrorHandler::~OnlyOnceErrorHandler ()
  47. { }
  48. void
  49. OnlyOnceErrorHandler::error(const log4cplus::tstring& err)
  50. {
  51. if(firstTime) {
  52. helpers::getLogLog().error(err);
  53. firstTime = false;
  54. }
  55. }
  56. void
  57. OnlyOnceErrorHandler::reset()
  58. {
  59. firstTime = true;
  60. }
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // log4cplus::Appender ctors
  63. ///////////////////////////////////////////////////////////////////////////////
  64. Appender::Appender()
  65. : layout(new SimpleLayout()),
  66. name( LOG4CPLUS_TEXT("") ),
  67. threshold(NOT_SET_LOG_LEVEL),
  68. errorHandler(new OnlyOnceErrorHandler),
  69. useLockFile(false),
  70. closed(false)
  71. {
  72. }
  73. Appender::Appender(const log4cplus::helpers::Properties & properties)
  74. : layout(new SimpleLayout())
  75. , name()
  76. , threshold(NOT_SET_LOG_LEVEL)
  77. , errorHandler(new OnlyOnceErrorHandler)
  78. , useLockFile(false)
  79. , closed(false)
  80. {
  81. if(properties.exists( LOG4CPLUS_TEXT("layout") ))
  82. {
  83. log4cplus::tstring const & factoryName
  84. = properties.getProperty( LOG4CPLUS_TEXT("layout") );
  85. spi::LayoutFactory* factory
  86. = spi::getLayoutFactoryRegistry().get(factoryName);
  87. if(factory == 0) {
  88. helpers::getLogLog().error(
  89. LOG4CPLUS_TEXT("Cannot find LayoutFactory: \"")
  90. + factoryName
  91. + LOG4CPLUS_TEXT("\"") );
  92. return;
  93. }
  94. helpers::Properties layoutProperties =
  95. properties.getPropertySubset( LOG4CPLUS_TEXT("layout.") );
  96. try {
  97. std::auto_ptr<Layout> newLayout(factory->createObject(layoutProperties));
  98. if(newLayout.get() == 0) {
  99. helpers::getLogLog().error(
  100. LOG4CPLUS_TEXT("Failed to create appender: ")
  101. + factoryName);
  102. }
  103. else {
  104. layout = newLayout;
  105. }
  106. }
  107. catch(std::exception const & e) {
  108. helpers::getLogLog().error(
  109. LOG4CPLUS_TEXT("Error while creating Layout: ")
  110. + LOG4CPLUS_C_STR_TO_TSTRING(e.what()));
  111. return;
  112. }
  113. }
  114. // Support for appender.Threshold in properties configuration file
  115. if(properties.exists(LOG4CPLUS_TEXT("Threshold"))) {
  116. tstring tmp = properties.getProperty(LOG4CPLUS_TEXT("Threshold"));
  117. tmp = log4cplus::helpers::toUpper(tmp);
  118. threshold = log4cplus::getLogLevelManager().fromString(tmp);
  119. }
  120. // Configure the filters
  121. helpers::Properties filterProps
  122. = properties.getPropertySubset( LOG4CPLUS_TEXT("filters.") );
  123. unsigned filterCount = 0;
  124. spi::FilterPtr filterChain;
  125. tstring filterName;
  126. while (filterProps.exists(
  127. filterName = helpers::convertIntegerToString (++filterCount)))
  128. {
  129. tstring const & factoryName = filterProps.getProperty(filterName);
  130. spi::FilterFactory* factory
  131. = spi::getFilterFactoryRegistry().get(factoryName);
  132. if(! factory)
  133. {
  134. tstring err = LOG4CPLUS_TEXT("Appender::ctor()- Cannot find FilterFactory: ");
  135. helpers::getLogLog().error(err + factoryName);
  136. continue;
  137. }
  138. spi::FilterPtr tmpFilter = factory->createObject (
  139. filterProps.getPropertySubset(filterName + LOG4CPLUS_TEXT(".")));
  140. if (! tmpFilter)
  141. {
  142. tstring err = LOG4CPLUS_TEXT("Appender::ctor()- Failed to create filter: ");
  143. helpers::getLogLog().error(err + filterName);
  144. }
  145. if (! filterChain)
  146. filterChain = tmpFilter;
  147. else
  148. filterChain->appendFilter(tmpFilter);
  149. }
  150. setFilter(filterChain);
  151. properties.getBool (useLockFile, LOG4CPLUS_TEXT("UseLockFile"));
  152. if (useLockFile)
  153. {
  154. tstring const & lockFileName
  155. = properties.getProperty (LOG4CPLUS_TEXT ("LockFile"));
  156. if (! lockFileName.empty ())
  157. {
  158. try
  159. {
  160. lockFile.reset (new helpers::LockFile (lockFileName));
  161. }
  162. catch (std::runtime_error const &)
  163. {
  164. return;
  165. }
  166. }
  167. else
  168. {
  169. helpers::getLogLog ().debug (
  170. LOG4CPLUS_TEXT (
  171. "UseLockFile is true but LockFile is not specified"));
  172. }
  173. }
  174. }
  175. Appender::~Appender()
  176. {
  177. helpers::LogLog & loglog = helpers::getLogLog ();
  178. loglog.debug(LOG4CPLUS_TEXT("Destroying appender named [") + name
  179. + LOG4CPLUS_TEXT("]."));
  180. if (! closed)
  181. loglog.error (
  182. LOG4CPLUS_TEXT ("Derived Appender did not call destructorImpl()."));
  183. }
  184. ///////////////////////////////////////////////////////////////////////////////
  185. // log4cplus::Appender public methods
  186. ///////////////////////////////////////////////////////////////////////////////
  187. void
  188. Appender::destructorImpl()
  189. {
  190. // An appender might be closed then destroyed. There is no point
  191. // in closing twice. It can actually be a wrong thing to do, e.g.,
  192. // files get rolled more than once.
  193. if (closed)
  194. return;
  195. close();
  196. closed = true;
  197. }
  198. bool Appender::isClosed() const
  199. {
  200. return closed;
  201. }
  202. void
  203. Appender::doAppend(const log4cplus::spi::InternalLoggingEvent& event)
  204. {
  205. thread::MutexGuard guard (access_mutex);
  206. if(closed) {
  207. helpers::getLogLog().error(
  208. LOG4CPLUS_TEXT("Attempted to append to closed appender named [")
  209. + name
  210. + LOG4CPLUS_TEXT("]."));
  211. return;
  212. }
  213. // Check appender's threshold logging level.
  214. if (! isAsSevereAsThreshold(event.getLogLevel()))
  215. return;
  216. // Evaluate filters attached to this appender.
  217. if (checkFilter(filter.get(), event) == spi::DENY)
  218. return;
  219. // Lock system wide lock.
  220. helpers::LockFileGuard lfguard;
  221. if (useLockFile && lockFile.get ())
  222. {
  223. try
  224. {
  225. lfguard.attach_and_lock (*lockFile);
  226. }
  227. catch (std::runtime_error const &)
  228. {
  229. return;
  230. }
  231. }
  232. // Finally append given event.
  233. append(event);
  234. }
  235. tstring &
  236. Appender::formatEvent (const spi::InternalLoggingEvent& event) const
  237. {
  238. internal::appender_sratch_pad & appender_sp = internal::get_appender_sp ();
  239. detail::clear_tostringstream (appender_sp.oss);
  240. layout->formatAndAppend(appender_sp.oss, event);
  241. appender_sp.oss.str().swap (appender_sp.str);
  242. return appender_sp.str;
  243. }
  244. log4cplus::tstring
  245. Appender::getName()
  246. {
  247. return name;
  248. }
  249. void
  250. Appender::setName(const log4cplus::tstring& n)
  251. {
  252. this->name = n;
  253. }
  254. ErrorHandler*
  255. Appender::getErrorHandler()
  256. {
  257. return errorHandler.get();
  258. }
  259. void
  260. Appender::setErrorHandler(std::auto_ptr<ErrorHandler> eh)
  261. {
  262. if (! eh.get())
  263. {
  264. // We do not throw exception here since the cause is probably a
  265. // bad config file.
  266. helpers::getLogLog().warn(
  267. LOG4CPLUS_TEXT("You have tried to set a null error-handler."));
  268. return;
  269. }
  270. thread::MutexGuard guard (access_mutex);
  271. this->errorHandler = eh;
  272. }
  273. void
  274. Appender::setLayout(std::auto_ptr<Layout> lo)
  275. {
  276. thread::MutexGuard guard (access_mutex);
  277. this->layout = lo;
  278. }
  279. Layout*
  280. Appender::getLayout()
  281. {
  282. return layout.get();
  283. }
  284. } // namespace log4cplus