LogStream.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // LogStream.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/LogStream.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: LogStream
  9. //
  10. // Copyright (c) 2006-2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/LogStream.h"
  16. namespace Poco {
  17. //
  18. // LogStreamBuf
  19. //
  20. LogStreamBuf::LogStreamBuf(Logger& logger, Message::Priority priority):
  21. _logger(logger),
  22. _priority(priority)
  23. {
  24. }
  25. LogStreamBuf::~LogStreamBuf()
  26. {
  27. }
  28. void LogStreamBuf::setPriority(Message::Priority priority)
  29. {
  30. _priority = priority;
  31. }
  32. int LogStreamBuf::writeToDevice(char c)
  33. {
  34. if (c == '\n' || c == '\r')
  35. {
  36. if (_message.find_first_not_of("\r\n") != std::string::npos)
  37. {
  38. Message msg(_logger.name(), _message, _priority);
  39. _message.clear();
  40. _logger.log(msg);
  41. }
  42. }
  43. else _message += c;
  44. return c;
  45. }
  46. //
  47. // LogIOS
  48. //
  49. LogIOS::LogIOS(Logger& logger, Message::Priority priority):
  50. _buf(logger, priority)
  51. {
  52. poco_ios_init(&_buf);
  53. }
  54. LogIOS::~LogIOS()
  55. {
  56. }
  57. LogStreamBuf* LogIOS::rdbuf()
  58. {
  59. return &_buf;
  60. }
  61. //
  62. // LogStream
  63. //
  64. LogStream::LogStream(Logger& logger, Message::Priority priority):
  65. LogIOS(logger, priority),
  66. std::ostream(&_buf)
  67. {
  68. }
  69. LogStream::LogStream(const std::string& loggerName, Message::Priority priority):
  70. LogIOS(Logger::get(loggerName), priority),
  71. std::ostream(&_buf)
  72. {
  73. }
  74. LogStream::~LogStream()
  75. {
  76. }
  77. LogStream& LogStream::fatal()
  78. {
  79. return priority(Message::PRIO_FATAL);
  80. }
  81. LogStream& LogStream::fatal(const std::string& message)
  82. {
  83. _buf.logger().fatal(message);
  84. return priority(Message::PRIO_FATAL);
  85. }
  86. LogStream& LogStream::critical()
  87. {
  88. return priority(Message::PRIO_CRITICAL);
  89. }
  90. LogStream& LogStream::critical(const std::string& message)
  91. {
  92. _buf.logger().critical(message);
  93. return priority(Message::PRIO_CRITICAL);
  94. }
  95. LogStream& LogStream::error()
  96. {
  97. return priority(Message::PRIO_ERROR);
  98. }
  99. LogStream& LogStream::error(const std::string& message)
  100. {
  101. _buf.logger().error(message);
  102. return priority(Message::PRIO_ERROR);
  103. }
  104. LogStream& LogStream::warning()
  105. {
  106. return priority(Message::PRIO_WARNING);
  107. }
  108. LogStream& LogStream::warning(const std::string& message)
  109. {
  110. _buf.logger().warning(message);
  111. return priority(Message::PRIO_WARNING);
  112. }
  113. LogStream& LogStream::notice()
  114. {
  115. return priority(Message::PRIO_NOTICE);
  116. }
  117. LogStream& LogStream::notice(const std::string& message)
  118. {
  119. _buf.logger().notice(message);
  120. return priority(Message::PRIO_NOTICE);
  121. }
  122. LogStream& LogStream::information()
  123. {
  124. return priority(Message::PRIO_INFORMATION);
  125. }
  126. LogStream& LogStream::information(const std::string& message)
  127. {
  128. _buf.logger().information(message);
  129. return priority(Message::PRIO_INFORMATION);
  130. }
  131. LogStream& LogStream::debug()
  132. {
  133. return priority(Message::PRIO_DEBUG);
  134. }
  135. LogStream& LogStream::debug(const std::string& message)
  136. {
  137. _buf.logger().debug(message);
  138. return priority(Message::PRIO_DEBUG);
  139. }
  140. LogStream& LogStream::trace()
  141. {
  142. return priority(Message::PRIO_TRACE);
  143. }
  144. LogStream& LogStream::trace(const std::string& message)
  145. {
  146. _buf.logger().trace(message);
  147. return priority(Message::PRIO_TRACE);
  148. }
  149. LogStream& LogStream::priority(Message::Priority priority)
  150. {
  151. _buf.setPriority(priority);
  152. return *this;
  153. }
  154. } // namespace Poco