FCF 2.0 development in progress...
> > > > > > > >
[News] [C++ Libraries API] [C++ Downloads] [Donate to the project] [Contacts]

stream from class fcf::NTest::Logger::MessageContext

Property: stream

Type: std::ostream*

Class: fcf::NTest::Logger::MessageContext

Package: fcfTest

File: test.hpp

Available from version: 1.1.14

Pointer to the output stream target for the current log message.

The stream property is a pointer to the std::ostream currently being written to. Since the fcf::NTest::Logger can have multiple OutputTargets (e.g., std::cout, a file stream, etc.), this property allows formatters and prefixes to know exactly which stream is being processed.

Example: Target Stream Awareness

Demonstrating how a formatter can use the stream property to perform stream-specific operations (though in practice, formatters usually modify the message string, the stream pointer is available for direct output if needed).

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <iostream> FCF_TEST_DEFINE("StreamDemo", "Logger", "StreamTest") { fcf::NTest::Logger& logger = fcf::NTest::logger(); // Register a format that checks the stream type fcf::NTest::Logger::Format format; format.name = "stream-checker"; format.handler = []( fcf::NTest::Logger& logger, fcf::NTest::Logger::MessageContext& context) { if (context.stream == &std::cout) { context.message = "[CONSOLE] " + context.message; } else { context.message = "[FILE] " + context.message; } }; logger.appendFormat(format); // 2. Log to console (default) logger.log() << "Hello Console" << std::endl; // 3. Log to a file std::ofstream file("test_output.log"); if (file.is_open()) { fcf::NTest::Logger::OutputTarget ot; ot.name = "file-target"; ot.stream = &file; // Set an empty format so that this stream will use // the globally configured format instead. ot.format = ""; logger.appendTarget(ot); logger.log() << "Hello File" << std::endl; // Restoring the logger's state logger.clearTargets(true); } // Restoring the logger's state logger.clearFormats(true); } int main(int a_argc, char* a_argv[]) { bool error = false; fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error); return error ? 1 : 0; }

Output:

Performing the test: "StreamDemo" -> "Logger" -> "StreamTest" ... [CONSOLE] > Hello Console [CONSOLE] > Hello File [SUCCESS] Test completed successfully (0.000`124`952 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`124`952 sec

test_output.log file:

[FILE] > Hello File

The application must be run by specifying the format via a command-line parameter:

app --test-format stream-checker