LogStream.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // LogStream.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/LogStream.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: LogStream
  9. //
  10. // Definition of the LogStream class.
  11. //
  12. // Copyright (c) 2006-2007, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_LogStream_INCLUDED
  18. #define Foundation_LogStream_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Logger.h"
  21. #include "Poco/UnbufferedStreamBuf.h"
  22. #include <istream>
  23. namespace Poco {
  24. class Foundation_API LogStreamBuf: public UnbufferedStreamBuf
  25. /// This class implements a streambuf interface
  26. /// to a Logger.
  27. ///
  28. /// The streambuf appends all characters written to it
  29. /// to a string. As soon as a CR or LF (std::endl) is written,
  30. /// the string is sent to the Logger, with the set
  31. /// priority.
  32. {
  33. public:
  34. LogStreamBuf(Logger& logger, Message::Priority priority);
  35. /// Creates the LogStream.
  36. ~LogStreamBuf();
  37. /// Destroys the LogStream.
  38. void setPriority(Message::Priority priority);
  39. /// Sets the priority for log messages.
  40. Message::Priority getPriority() const;
  41. /// Returns the priority for log messages.
  42. Logger& logger() const;
  43. /// Returns a reference to the Logger.
  44. private:
  45. int writeToDevice(char c);
  46. private:
  47. Logger& _logger;
  48. Message::Priority _priority;
  49. std::string _message;
  50. };
  51. class Foundation_API LogIOS: public virtual std::ios
  52. /// The base class for LogStream.
  53. ///
  54. /// This class is needed to ensure the correct initialization
  55. /// order of the stream buffer and base classes.
  56. {
  57. public:
  58. LogIOS(Logger& logger, Message::Priority priority);
  59. ~LogIOS();
  60. LogStreamBuf* rdbuf();
  61. protected:
  62. LogStreamBuf _buf;
  63. };
  64. class Foundation_API LogStream: public LogIOS, public std::ostream
  65. /// This class implements an ostream interface
  66. /// to a Logger.
  67. ///
  68. /// The stream's buffer appends all characters written to it
  69. /// to a string. As soon as a CR or LF (std::endl) is written,
  70. /// the string is sent to the Logger, with the current
  71. /// priority.
  72. ///
  73. /// Usage example:
  74. /// LogStream ls(someLogger);
  75. /// ls << "Some informational message" << std::endl;
  76. /// ls.error() << "Some error message" << std::endl;
  77. {
  78. public:
  79. LogStream(Logger& logger, Message::Priority priority = Message::PRIO_INFORMATION);
  80. /// Creates the LogStream, using the given logger and priority.
  81. LogStream(const std::string& loggerName, Message::Priority priority = Message::PRIO_INFORMATION);
  82. /// Creates the LogStream, using the logger identified
  83. /// by loggerName, and sets the priority.
  84. ~LogStream();
  85. /// Destroys the LogStream.
  86. LogStream& fatal();
  87. /// Sets the priority for log messages to Message::PRIO_FATAL.
  88. LogStream& fatal(const std::string& message);
  89. /// Sets the priority for log messages to Message::PRIO_FATAL
  90. /// and writes the given message.
  91. LogStream& critical();
  92. /// Sets the priority for log messages to Message::PRIO_CRITICAL.
  93. LogStream& critical(const std::string& message);
  94. /// Sets the priority for log messages to Message::PRIO_CRITICAL
  95. /// and writes the given message.
  96. LogStream& error();
  97. /// Sets the priority for log messages to Message::PRIO_ERROR.
  98. LogStream& error(const std::string& message);
  99. /// Sets the priority for log messages to Message::PRIO_ERROR
  100. /// and writes the given message.
  101. LogStream& warning();
  102. /// Sets the priority for log messages to Message::PRIO_WARNING.
  103. LogStream& warning(const std::string& message);
  104. /// Sets the priority for log messages to Message::PRIO_WARNING
  105. /// and writes the given message.
  106. LogStream& notice();
  107. /// Sets the priority for log messages to Message::PRIO_NOTICE.
  108. LogStream& notice(const std::string& message);
  109. /// Sets the priority for log messages to Message::PRIO_NOTICE
  110. /// and writes the given message.
  111. LogStream& information();
  112. /// Sets the priority for log messages to Message::PRIO_INFORMATION.
  113. LogStream& information(const std::string& message);
  114. /// Sets the priority for log messages to Message::PRIO_INFORMATION
  115. /// and writes the given message.
  116. LogStream& debug();
  117. /// Sets the priority for log messages to Message::PRIO_DEBUG.
  118. LogStream& debug(const std::string& message);
  119. /// Sets the priority for log messages to Message::PRIO_DEBUG
  120. /// and writes the given message.
  121. LogStream& trace();
  122. /// Sets the priority for log messages to Message::PRIO_TRACE.
  123. LogStream& trace(const std::string& message);
  124. /// Sets the priority for log messages to Message::PRIO_TRACE
  125. /// and writes the given message.
  126. LogStream& priority(Message::Priority priority);
  127. /// Sets the priority for log messages.
  128. };
  129. //
  130. // inlines
  131. //
  132. inline Message::Priority LogStreamBuf::getPriority() const
  133. {
  134. return _priority;
  135. }
  136. inline Logger& LogStreamBuf::logger() const
  137. {
  138. return _logger;
  139. }
  140. } // namespace Poco
  141. #endif // Foundation_LogStream_INCLUDED