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

appendTarget() method from fcf::NTest::Logger class

void appendTarget(const fcf::NTest::Logger::OutputTarget& a_stream)

Class: fcf::NTest::Logger

Package: fcfTest

File: test.hpp

Available from version: 1.2.1

Adds a new output target to the logger.

The appendTarget method allows you to add a new fcf::NTest::Logger::OutputTarget to the logger's current collection of output destinations.

This method is used to direct log messages to multiple destinations simultaneously, such as both the standard console and a specific log file, potentially with different formatting rules for each target.

Arguments

const fcf::NTest::Logger::OutputTarget& a_stream
- The output target configuration to be added to the logger.

Example: Adding a File Output Target

Demonstrating how to add a new file-based output target to the logger alongside the default console output.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <iostream> #include <fstream> FCF_TEST_DECLARE("TargetDemo", "Logger", "AppendTargetTest") { fcf::NTest::Logger& logger = fcf::NTest::logger(); // 1. Create a file stream that will persist for the duration of the test std::ofstream fileStream("test_results.log", std::ios::binary); // 2. Configure a new output target for the file fcf::NTest::Logger::OutputTarget fileTarget; fileTarget.name = "file-log"; fileTarget.stream = &fileStream; fileTarget.format = "default"; // 3. Add the target to the logger logger.appendTarget(fileTarget); // 4. Log a message // This message will now be sent to BOTH the console and the file fcf::NTest::log() << "This message is sent to multiple targets!" << std::endl; // 5. Reset to default targets logger.clearTargets(true); FCF_TEST(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: "TargetDemo" -> "Logger" -> "AppendTargetTest" ... > This message is sent to multiple targets! [SUCCESS] Test completed successfully (0.000`099`141 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`099`141 sec

test_results.log file:

> This message is sent to multiple targets!

By using appendTarget, you can easily expand the logger's capabilities to support multi-destination logging without affecting existing targets.