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.

113 lines
2.8 KiB

  1. // Module: Log4CPLUS
  2. // File: consoleappender.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/layout.h>
  21. #include <log4cplus/consoleappender.h>
  22. #include <log4cplus/streams.h>
  23. #include <log4cplus/helpers/loglog.h>
  24. #include <log4cplus/helpers/stringhelper.h>
  25. #include <log4cplus/helpers/property.h>
  26. #include <log4cplus/spi/loggingevent.h>
  27. #include <log4cplus/thread/syncprims-pub-impl.h>
  28. #include <ostream>
  29. namespace log4cplus
  30. {
  31. namespace helpers
  32. {
  33. extern log4cplus::thread::Mutex const & getConsoleOutputMutex ();
  34. } // namespace helpers
  35. log4cplus::thread::Mutex const &
  36. ConsoleAppender::getOutputMutex ()
  37. {
  38. return helpers::getConsoleOutputMutex ();
  39. }
  40. //////////////////////////////////////////////////////////////////////////////
  41. // ConsoleAppender ctors and dtor
  42. //////////////////////////////////////////////////////////////////////////////
  43. ConsoleAppender::ConsoleAppender(bool logToStdErr_,
  44. bool immediateFlush_)
  45. : logToStdErr(logToStdErr_),
  46. immediateFlush(immediateFlush_)
  47. {
  48. }
  49. ConsoleAppender::ConsoleAppender(const helpers::Properties & properties)
  50. : Appender(properties),
  51. logToStdErr(false),
  52. immediateFlush(false)
  53. {
  54. properties.getBool (logToStdErr, LOG4CPLUS_TEXT("logToStdErr"));
  55. properties.getBool (immediateFlush, LOG4CPLUS_TEXT("ImmediateFlush"));
  56. }
  57. ConsoleAppender::~ConsoleAppender()
  58. {
  59. destructorImpl();
  60. }
  61. //////////////////////////////////////////////////////////////////////////////
  62. // ConsoleAppender public methods
  63. //////////////////////////////////////////////////////////////////////////////
  64. void
  65. ConsoleAppender::close()
  66. {
  67. helpers::getLogLog().debug(
  68. LOG4CPLUS_TEXT("Entering ConsoleAppender::close().."));
  69. closed = true;
  70. }
  71. //////////////////////////////////////////////////////////////////////////////
  72. // ConsoleAppender protected methods
  73. //////////////////////////////////////////////////////////////////////////////
  74. void
  75. ConsoleAppender::append(const spi::InternalLoggingEvent& event)
  76. {
  77. thread::MutexGuard guard (getOutputMutex ());
  78. tostream& output = (logToStdErr ? tcerr : tcout);
  79. layout->formatAndAppend(output, event);
  80. if(immediateFlush) {
  81. output.flush();
  82. }
  83. }
  84. } // namespace log4cplus