log_buffer_impl.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <log/log_buffer_impl.h>
  15. #include <log4cplus/loglevel.h>
  16. #include <boost/scoped_ptr.hpp>
  17. #include <cstdio>
  18. namespace isc {
  19. namespace log {
  20. namespace internal {
  21. LogBuffer::~LogBuffer() {
  22. // If there is anything left in the buffer,
  23. // it means no reconfig has been done, and
  24. // we can assume the logging system was either
  25. // never setup, or broke while doing so.
  26. // So dump all that is left to stdout
  27. try {
  28. flushStdout();
  29. } catch (...) {
  30. // Ok if we can't even seem to dump to stdout, never mind.
  31. }
  32. }
  33. void
  34. LogBuffer::add(const log4cplus::spi::InternalLoggingEvent& event) {
  35. if (flushed_) {
  36. isc_throw(LogBufferAddAfterFlush,
  37. "Internal log buffer has been flushed already");
  38. }
  39. // get a clone, and put the pointer in a shared_pt
  40. std::auto_ptr<log4cplus::spi::InternalLoggingEvent> event_aptr =
  41. event.clone();
  42. boost::shared_ptr<log4cplus::spi::InternalLoggingEvent> event_sptr(
  43. event_aptr.release());
  44. stored_.push_back(event_sptr);
  45. }
  46. void
  47. LogBuffer::flushStdout() {
  48. // This does not show a bit of information normal log messages
  49. // do, so perhaps we should try and setup a new logger here
  50. // However, as this is called from a destructor, it may not
  51. // be a good idea; as we can't reliably know whether in what
  52. // state the logger instance is now (or what the specific logger's
  53. // settings were).
  54. // So we print a raw format (it excludes the time and the pid, and
  55. // it prints severity as a number)
  56. LoggerEventPtrList::const_iterator it;
  57. for (it = stored_.begin(); it != stored_.end(); ++it) {
  58. std::printf("Severity=%d [%s]: %s\n", (*it)->getLogLevel(),
  59. (*it)->getLoggerName().c_str(),
  60. (*it)->getMessage().c_str());
  61. }
  62. stored_.clear();
  63. }
  64. void
  65. LogBuffer::flush() {
  66. LoggerEventPtrList stored_copy;
  67. stored_.swap(stored_copy);
  68. LoggerEventPtrList::const_iterator it;
  69. for (it = stored_copy.begin(); it != stored_copy.end(); ++it) {
  70. log4cplus::Logger logger =
  71. log4cplus::Logger::getInstance((*it)->getLoggerName());
  72. logger.log((*it)->getLogLevel(), (*it)->getMessage());
  73. }
  74. stored_.clear();
  75. flushed_ = true;
  76. }
  77. size_t
  78. LogBuffer::getBufferSize() const {
  79. return (stored_.size());
  80. }
  81. void
  82. BufferAppender::append(const log4cplus::spi::InternalLoggingEvent& event) {
  83. buffer_.add(event);
  84. }
  85. } // end namespace internal
  86. } // end namespace log
  87. } // end namespace isc