| 1 |
#pragma once |
| 2 |
|
| 3 |
/// \file |
| 4 |
/// \brief Text-stream interfaces. |
| 5 |
|
| 6 |
#include <cstddef> |
| 7 |
#include <string> |
| 8 |
#include <stdio.h> |
| 9 |
#include <streambuf> |
| 10 |
#include <istream> |
| 11 |
#include <cassert> |
| 12 |
#include <sstream> |
| 13 |
#include <iostream> |
| 14 |
#include <mutex> |
| 15 |
|
| 16 |
/// \brief A read-only character-stream. |
| 17 |
// OrbWeaver: merged functionality from TextStreambufAdaptor onto this class |
| 18 |
// directly. |
| 19 |
class TextInputStream |
| 20 |
: public std::streambuf |
| 21 |
{ |
| 22 |
protected: |
| 23 |
// Buffer to use for reading |
| 24 |
static const std::size_t BUFFER_SIZE = 8192; |
| 25 |
char _buffer[BUFFER_SIZE]; |
| 26 |
|
| 27 |
protected: |
| 28 |
|
| 29 |
/* Implementations of stream-specific virtual functions on std::streambuf */ |
| 30 |
|
| 31 |
// Replenish the controlled buffer with characters from the underlying |
| 32 |
// input sequence. |
| 33 |
virtual int underflow() |
| 34 |
{ |
| 35 |
// Read next block of BUFFER_SIZE characters into the buffer from |
| 36 |
// the underlying TextInputStream. |
| 37 |
std::size_t charsRead = this->read(_buffer, BUFFER_SIZE); |
| 38 |
|
| 39 |
// Set up the internal pointers correctly |
| 40 |
assert(charsRead <= BUFFER_SIZE); |
| 41 |
std::streambuf::setg(_buffer, _buffer, _buffer + charsRead); |
| 42 |
|
| 43 |
// Return the next character, or EOF if there were no more characters |
| 44 |
if (charsRead > 0) |
| 45 |
return static_cast<int>(_buffer[0]); |
| 46 |
else |
| 47 |
return EOF; |
| 48 |
} |
| 49 |
|
| 50 |
public: |
| 51 |
|
| 52 |
/// \brief Attempts to read the next \p length characters from the stream to \p buffer. |
| 53 |
/// Returns the number of characters actually stored in \p buffer. |
| 54 |
virtual std::size_t read(char* buffer, std::size_t length) = 0; |
| 55 |
|
| 56 |
}; |
| 57 |
|
| 58 |
/** |
| 59 |
* greebo: This is a simple container holding a single output stream. |
| 60 |
* Use the getStream() method to acquire a reference to the stream. |
| 61 |
*/ |
| 62 |
class OutputStreamHolder |
| 63 |
{ |
| 64 |
std::ostringstream _tempOutputStream; |
| 65 |
std::mutex _nullLock; |
| 66 |
std::ostream* _outputStream; |
| 67 |
std::mutex* _streamLock; |
| 68 |
|
| 69 |
public: |
| 70 |
OutputStreamHolder() : |
| 71 |
_outputStream(&_tempOutputStream), |
| 72 |
_streamLock(&_nullLock) |
| 73 |
{} |
| 74 |
|
| 75 |
void setStream(std::ostream& outputStream) |
| 76 |
{ |
| 77 |
_outputStream = &outputStream; |
| 78 |
|
| 79 |
// Copy temporary data to new buffer |
| 80 |
(*_outputStream) << _tempOutputStream.str(); |
| 81 |
_tempOutputStream.clear(); |
| 82 |
} |
| 83 |
|
| 84 |
std::ostream& getStream() { |
| 85 |
return *_outputStream; |
| 86 |
} |
| 87 |
|
| 88 |
void setLock(std::mutex& streamLock) |
| 89 |
{ |
| 90 |
_streamLock = &streamLock; |
| 91 |
} |
| 92 |
|
| 93 |
std::mutex& getStreamLock() |
| 94 |
{ |
| 95 |
return *_streamLock; |
| 96 |
} |
| 97 |
|
| 98 |
void reset() |
| 99 |
{ |
| 100 |
_outputStream = &_tempOutputStream; |
| 101 |
_streamLock = &_nullLock; |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
// With multiple threads writing against a single thread-unsafe std::ostream |
| 106 |
// we need to buffer the stream and write it to the underlying stream |
| 107 |
// in a thread-safe manner. The TemporaryThreadsafeStream passes its data |
| 108 |
// in the destructor - since std::ostringstream doesn't define a virtual |
| 109 |
// destructor client code should not cast the stream reference to its base |
| 110 |
// std::stringstream otherwise the destructor might not be called. |
| 111 |
class TemporaryThreadsafeStream : |
| 112 |
public std::ostringstream |
| 113 |
{ |
| 114 |
private: |
| 115 |
std::ostream& _actualStream; |
| 116 |
std::mutex& _streamLock; |
| 117 |
|
| 118 |
public: |
| 119 |
TemporaryThreadsafeStream(std::ostream& actualStream, std::mutex& streamLock) : |
| 120 |
_actualStream(actualStream), |
| 121 |
_streamLock(streamLock) |
| 122 |
{ |
| 123 |
_actualStream.copyfmt(*this); |
| 124 |
setstate(actualStream.rdstate()); |
| 125 |
} |
| 126 |
|
| 127 |
// Copy constructor must be defined explicitly because newer GCC won't |
| 128 |
// define it implicitly for reference members resulting in a "use of |
| 129 |
// deleted function" error when returning by value. |
| 130 |
TemporaryThreadsafeStream(const TemporaryThreadsafeStream& other) |
| 131 |
: _actualStream(other._actualStream), |
| 132 |
_streamLock(other._streamLock) |
| 133 |
{ } |
| 134 |
|
| 135 |
// On destruction, we flush our buffer to the main stream |
| 136 |
// in a thread-safe manner |
| 137 |
~TemporaryThreadsafeStream() |
| 138 |
{ |
| 139 |
std::lock_guard<std::mutex> lock(_streamLock); |
| 140 |
|
| 141 |
// Flush buffer on destruction |
| 142 |
_actualStream << str(); |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
// The static stream holder containers, these are instantiated by each |
| 148 |
// module (DLL/so) at the time of the first call. |
| 149 |
inline OutputStreamHolder& GlobalOutputStream() |
| 150 |
{ |
| 151 |
static OutputStreamHolder _holder; |
| 152 |
return _holder; |
| 153 |
} |
| 154 |
|
| 155 |
inline OutputStreamHolder& GlobalErrorStream() |
| 156 |
{ |
| 157 |
static OutputStreamHolder _holder; |
| 158 |
return _holder; |
| 159 |
} |
| 160 |
|
| 161 |
inline OutputStreamHolder& GlobalWarningStream() |
| 162 |
{ |
| 163 |
static OutputStreamHolder _holder; |
| 164 |
return _holder; |
| 165 |
} |
| 166 |
|
| 167 |
inline OutputStreamHolder& GlobalDebugStream() |
| 168 |
{ |
| 169 |
static OutputStreamHolder _holder; |
| 170 |
return _holder; |
| 171 |
} |
| 172 |
|
| 173 |
// The stream accessors: use these to write to the application's various streams. |
| 174 |
// Note that the TemporaryThreadsafeStream is using the SAME std::mutex (on purpose) |
| 175 |
// to avoid error and debug streams from concurrently writing to the same log device. |
| 176 |
inline TemporaryThreadsafeStream rMessage() |
| 177 |
{ |
| 178 |
return TemporaryThreadsafeStream( |
| 179 |
GlobalOutputStream().getStream(), |
| 180 |
GlobalOutputStream().getStreamLock() |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
inline TemporaryThreadsafeStream rError() |
| 185 |
{ |
| 186 |
return TemporaryThreadsafeStream( |
| 187 |
GlobalErrorStream().getStream(), |
| 188 |
GlobalErrorStream().getStreamLock() |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
inline TemporaryThreadsafeStream rWarning() |
| 193 |
{ |
| 194 |
return TemporaryThreadsafeStream( |
| 195 |
GlobalWarningStream().getStream(), |
| 196 |
GlobalWarningStream().getStreamLock() |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* \brief |
| 202 |
* Get the debug output stream. |
| 203 |
* |
| 204 |
* In debug builds the debug stream is the same as the output stream. In release |
| 205 |
* builds it is a null stream. |
| 206 |
*/ |
| 207 |
inline TemporaryThreadsafeStream rDebug() |
| 208 |
{ |
| 209 |
return TemporaryThreadsafeStream( |
| 210 |
GlobalDebugStream().getStream(), |
| 211 |
GlobalDebugStream().getStreamLock() |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Thread-safe wrapper around the std::cout output stream. |
| 217 |
* |
| 218 |
* Since the Windows build is running without any console window |
| 219 |
* the output to std::cout is redirected to the appliation's LogStream buffer. |
| 220 |
* std::cout is not thread-safe itself, that's why it's advisable to use |
| 221 |
* this accessor whenever client code needs to write to std::cout. |
| 222 |
* |
| 223 |
* see also: rConsoleError() which wraps std::cerr |
| 224 |
*/ |
| 225 |
inline TemporaryThreadsafeStream rConsole() |
| 226 |
{ |
| 227 |
return TemporaryThreadsafeStream( |
| 228 |
std::cout, |
| 229 |
GlobalOutputStream().getStreamLock() |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Thread-safe wrapper around the std::cerr output stream. |
| 235 |
* |
| 236 |
* Use this instead of directly writing to std::cerr which |
| 237 |
* is not thread-safe. |
| 238 |
* |
| 239 |
* see also: rConsole() which wraps std::cout |
| 240 |
*/ |
| 241 |
inline TemporaryThreadsafeStream rConsoleError() |
| 242 |
{ |
| 243 |
return TemporaryThreadsafeStream( |
| 244 |
std::cerr, |
| 245 |
GlobalErrorStream().getStreamLock() |
| 246 |
); |
| 247 |
} |