Class: fcf::NTest::Logger
Package: fcfTest
File: test.hpp
Available from version: 1.2.1
Clears all current output targets from the logger, optionally restoring default ones.
The clearTargets method is used to remove all registered fcf::NTest::Logger::OutputTarget objects from the logger.
This method provides two modes of operation via the a_defaultState parameter:
- Full Clear: If a_defaultState is false (default), all targets are removed, leaving the logger with no output destinations.
- Reset to Defaults: If a_defaultState is true, all existing targets are cleared, and the standard default target (typically
std::cout) is automatically re-added.
Arguments
bool a_defaultState = false
- If true, the logger will restore its default target configuration after clearing the current ones. If false, the logger will have no targets.
Example: Clearing and Resetting Output Targets
Demonstrating how to completely remove all targets and how to restore the default logger state using a_defaultState.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
#include <fstream>
FCF_TEST_DECLARE("TargetDemo", "Logger", "ClearTargetsTest") {
fcf::NTest::Logger& logger = fcf::NTest::logger();
// 1. Add a custom file target
static std::ofstream fileStream("test_output.log");
fcf::NTest::Logger::OutputTarget fileTarget;
fileTarget.name = "file-target";
fileTarget.stream = &fileStream;
fileTarget.format = "default";
logger.appendTarget(fileTarget);
// 2. Verify the custom target is present
fcf::NTest::log() << "Targets before clearing: " << logger.targets().size() << std::endl;
// 3. Completely clear all targets
logger.clearTargets(false);
fcf::NTest::log() << "This message will not be seen anywhere." << std::endl;
// 4. Reset to default targets (std::cout)
logger.clearTargets(true);
fcf::NTest::log() << "This message should appear in the console." << std::endl;
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" -> "ClearTargetsTest" ...
> Targets before clearing: 2
> This message should appear in the console.
[SUCCESS] Test completed successfully (0.000`047`056 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`047`056 sec
test_output.log file:
> Targets before clearing: 2
The clearTargets method allows for easy management of the logger's output destinations, providing both a 'blank slate' option and a 'factory reset' option.