CategoryStream.hh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * CategoryStream.hh
  3. *
  4. * Copyright 2001, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
  5. * Copyright 2001, Bastiaan Bakker. All rights reserved.
  6. *
  7. * See the COPYING file for the terms of usage and distribution.
  8. */
  9. #ifndef _LOG4CPP_CATEGORYSTREAM_HH
  10. #define _LOG4CPP_CATEGORYSTREAM_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <log4cpp/Priority.hh>
  13. #include <ios>
  14. #ifdef LOG4CPP_HAVE_SSTREAM
  15. #include <sstream>
  16. #endif
  17. #include <log4cpp/Manipulator.hh>
  18. namespace log4cpp {
  19. class LOG4CPP_EXPORT Category;
  20. class LOG4CPP_EXPORT CategoryStream;
  21. /**
  22. * eol manipulator
  23. **/
  24. LOG4CPP_EXPORT CategoryStream& eol (CategoryStream& os);
  25. /**
  26. * left manipulator
  27. **/
  28. LOG4CPP_EXPORT CategoryStream& left (CategoryStream& os);
  29. /**
  30. * This class enables streaming simple types and objects to a category.
  31. * Use category.errorStream(), etc. to obtain a CategoryStream class.
  32. **/
  33. class LOG4CPP_EXPORT CategoryStream {
  34. public:
  35. /**
  36. * Construct a CategoryStream for given Category with given priority.
  37. * @param category The category this stream will send log messages to.
  38. * @param priority The priority the log messages will get or
  39. * Priority::NOTSET to silently discard any streamed in messages.
  40. **/
  41. CategoryStream(Category& category, Priority::Value priority);
  42. /**
  43. * Destructor for CategoryStream
  44. **/
  45. ~CategoryStream();
  46. /**
  47. * Returns the destination Category for this stream.
  48. * @returns The Category.
  49. **/
  50. inline Category& getCategory() const { return _category; };
  51. /**
  52. * Returns the priority for this stream.
  53. * @returns The priority.
  54. **/
  55. inline Priority::Value getPriority() const LOG4CPP_NOTHROW {
  56. return _priority;
  57. };
  58. /**
  59. * Flush the contents of the stream buffer to the Category and
  60. * empties the buffer.
  61. **/
  62. void flush();
  63. /**
  64. * Stream in arbitrary types and objects.
  65. * @param t The value or object to stream in.
  66. * @returns A reference to itself.
  67. **/
  68. template<typename T>
  69. CategoryStream& operator<<(const T& t) {
  70. if (getPriority() != Priority::NOTSET) {
  71. if (!_buffer) {
  72. if (!(_buffer = new std::ostringstream)) {
  73. // XXX help help help
  74. }
  75. }
  76. (*_buffer) << t;
  77. }
  78. return *this;
  79. }
  80. CategoryStream& operator<<(const char* t);
  81. template<typename T>
  82. CategoryStream& operator<<(const std::string& t) {
  83. if (getPriority() != Priority::NOTSET) {
  84. if (!_buffer) {
  85. if (!(_buffer = new std::ostringstream)) {
  86. // XXX help help help
  87. }
  88. }
  89. (*_buffer) << t;
  90. }
  91. return *this;
  92. }
  93. #if LOG4CPP_HAS_WCHAR_T != 0
  94. template<typename T>
  95. CategoryStream& operator<<(const std::wstring& t) {
  96. if (getPriority() != Priority::NOTSET) {
  97. if (!_wbuffer) {
  98. if (!(_wbuffer = new std::wostringstream)) {
  99. // XXX help help help
  100. }
  101. }
  102. (*_wbuffer) << t;
  103. }
  104. return *this;
  105. }
  106. #endif
  107. /**
  108. * Set the width output on CategoryStream
  109. **/
  110. std::streamsize width(std::streamsize wide );
  111. private:
  112. Category& _category;
  113. Priority::Value _priority;
  114. union {
  115. std::ostringstream* _buffer;
  116. #if LOG4CPP_HAS_WCHAR_T != 0
  117. std::wostringstream* _wbuffer;
  118. #endif
  119. };
  120. public:
  121. typedef CategoryStream& (*cspf) (CategoryStream&);
  122. CategoryStream& operator << (cspf);
  123. LOG4CPP_EXPORT friend CategoryStream& eol (CategoryStream& os);
  124. LOG4CPP_EXPORT friend CategoryStream& left (CategoryStream& os);
  125. };
  126. }
  127. #endif // _LOG4CPP_CATEGORYSTREAM_HH