ACF $AcfVersion:0$
Public Types | Public Member Functions | Protected Member Functions | List of all members
ilog::CStandardStreamBridge Class Reference

Redirects C++ standard output streams (cout/cerr) to the logging system. More...

#include <CStandardStreamBridge.h>

Inheritance diagram for ilog::CStandardStreamBridge:

Public Types

typedef std::basic_streambuf< char > BaseClass
 Base class typedef.
 

Public Member Functions

 CStandardStreamBridge (std::ostream &stream, ilog::IMessageConsumer &messageConsumer, istd::IInformationProvider::InformationCategory informationSeverity)
 Construct a stream bridge and activate redirection.
 
virtual ~CStandardStreamBridge ()
 Destructor - restores original stream buffer.
 

Protected Member Functions

virtual std::streamsize xsputn (const char *data, std::streamsize bufferSize) override
 Handle bulk character writes.
 
virtual int_type overflow (int_type v) override
 Handle single character writes.
 

Detailed Description

Redirects C++ standard output streams (cout/cerr) to the logging system.

CStandardStreamBridge intercepts output sent to std::cout or std::cerr and converts it into log messages. This is useful for capturing output from legacy code, third-party libraries, or console-based diagnostics.

The class inherits from std::basic_streambuf and replaces the stream's buffer, capturing all output that would normally go to the console and instead sending it as log messages.

Features
  • Captures std::cout and std::cerr output
  • Converts stream output to log messages
  • Configurable severity per stream (e.g., cout=info, cerr=error)
  • Thread-safe with mutex protection
  • Automatic restoration of original stream buffer on destruction
  • Line-based message generation (splits on newline)
Thread Safety
Uses QMutex to protect internal buffer during multi-threaded output. Safe to use with multiple threads writing to the same stream.
Usage Example
// Create log consumer
// Create bridges for cout (as info) and cerr (as error)
std::cout,
*logger,
std::cerr,
*logger,
// Now std::cout and std::cerr are redirected
std::cout << "This becomes an info message" << std::endl;
std::cerr << "This becomes an error message" << std::endl;
// Bridges are destroyed when going out of scope,
// restoring original stream buffers
Console logging component with colored output.
Redirects C++ standard output streams (cout/cerr) to the logging system.
@ IC_ERROR
Information about error, processing could not be done correctly.
@ IC_INFO
Normal information level.
Shared ownership smart pointer for interface types.
Note
When the bridge is destroyed, the original stream buffer is restored. Make sure the bridge lifetime covers all code that needs redirection.
Warning
Don't delete the stream or consumer while the bridge is active. The bridge holds raw pointers to both.
See also
ilog::CStandardStreamBridgeComp, ilog::IMessageConsumer

Definition at line 80 of file CStandardStreamBridge.h.

Member Typedef Documentation

◆ BaseClass

typedef std::basic_streambuf<char> ilog::CStandardStreamBridge::BaseClass

Base class typedef.

Definition at line 84 of file CStandardStreamBridge.h.

Constructor & Destructor Documentation

◆ CStandardStreamBridge()

ilog::CStandardStreamBridge::CStandardStreamBridge ( std::ostream &  stream,
ilog::IMessageConsumer messageConsumer,
istd::IInformationProvider::InformationCategory  informationSeverity 
)

Construct a stream bridge and activate redirection.

Replaces the stream's buffer with this bridge, capturing all output.

Parameters
streamReference to stream to bridge (std::cout, std::cerr, etc.)
messageConsumerConsumer to receive converted messages
informationSeveritySeverity level for generated messages
Example
std::cout,
*myLogger,
);
// std::cout is now redirected to myLogger

◆ ~CStandardStreamBridge()

virtual ilog::CStandardStreamBridge::~CStandardStreamBridge ( )
virtual

Destructor - restores original stream buffer.

When the bridge is destroyed, the original stream buffer is restored, returning the stream to normal console output.

Member Function Documentation

◆ overflow()

virtual int_type ilog::CStandardStreamBridge::overflow ( int_type  v)
overrideprotectedvirtual

Handle single character writes.

Called when a single character is written. Accumulates in buffer until newline, then sends as message.

Parameters
vCharacter to write
Returns
The character written, or EOF on error

◆ xsputn()

virtual std::streamsize ilog::CStandardStreamBridge::xsputn ( const char *  data,
std::streamsize  bufferSize 
)
overrideprotectedvirtual

Handle bulk character writes.

Called when multiple characters are written at once. Accumulates characters in internal buffer until newline, then sends as message.

Parameters
dataPointer to character data
bufferSizeNumber of characters to write
Returns
Number of characters written

The documentation for this class was generated from the following file: