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

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

fcf::NTest::Logger::OutputTargets targets()
void targets(fcf::NTest::Logger::OutputTargets a_targets)

Class: fcf::NTest::Logger

Package: fcfTest

File: test.hpp

Available from version: 1.2.1

Provides access to the current collection of output targets.

The targets method provides access to the current set of fcf::NTest::Logger::OutputTarget objects configured in the logger.

This method has two distinct behaviors depending on whether it is used for reading or writing:

  • Read Mode: When called without arguments, it returns a copy of the current list of targets (fcf::NTest::Logger::OutputTargets). This is useful for inspecting where the logger is currently sending its output.
  • Write Mode: When called with a fcf::NTest::Logger::OutputTargets object as an argument, it replaces the existing collection of targets with the provided one.

Arguments

fcf::NTest::Logger::OutputTargets a_targets
- The new collection of output targets to be set in the logger.
Result
fcf::NTest::Logger::OutputTargets
- A copy of the current list of output targets.

Example: Reading and Replacing Output Targets

Demonstrating how to retrieve the current targets and then replace them with a new custom configuration.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <iostream> #include <fstream> FCF_TEST_DECLARE("TargetDemo", "Logger", "TargetsAccessTest") { fcf::NTest::Logger& logger = fcf::NTest::logger(); // 1. Retrieve the current targets (Read Mode) fcf::NTest::Logger::OutputTargets currentTargets = logger.targets(); fcf::NTest::log() << "Current number of targets: " << currentTargets.size() << std::endl; // 2. Create a new set of targets std::ofstream fileStream("test_output.log"); fcf::NTest::Logger::OutputTarget newTarget; newTarget.name = "file-target"; newTarget.stream = &fileStream; newTarget.format = "default"; fcf::NTest::Logger::OutputTargets newTargets(currentTargets); newTargets.push_back(newTarget); // 3. Replace the existing targets (Write Mode) logger.targets(newTargets); // 4. Verify by logging a message fcf::NTest::log() << "This message goes to the file." << std::endl; // 5. Restore targets logger.targets(currentTargets); 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" -> "TargetsAccessTest" ... > Current number of targets: 1 > This message goes to the file. [SUCCESS] Test completed successfully (0.000`075`002 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`075`002 sec

test_output.log file:

> This message goes to the file.

The targets method effectively allows both inspection and complete reconfiguration of the logger's output destinations.