Type: fcf::NTest::Logger::HandlerDataMap
Class: fcf::NTest::Logger::OutputTarget
Package: fcfTest
File: test.hpp
Available from version: 1.1.13
A map containing persistent data associated with the prefixes of this target.
The prefixData property is a fcf::NTest::Logger::HandlerDataMap (a map of fcf::NTest::SharedPtrAny). It allows you to store and persist data that is specific to the prefixes used by this target. This is particularly useful when using functional prefixes that need to maintain state (like a counter or a timestamp) across multiple log calls for the same target.
Example: Stateful Functional Prefix
Demonstrating how to use prefixData to maintain a counter within a custom functional prefix.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
FCF_TEST_DEFINE("PrefixDemo", "Logger", "CounterTest") {
fcf::NTest::log() << "Message 1" << std::endl;
fcf::NTest::log() << "Message 2" << std::endl;
FCF_TEST(true);
}
int main(int a_argc, char* a_argv[]) {
fcf::NTest::Logger& logger = fcf::NTest::logger();
fcf::NTest::Logger::OutputTarget target;
target.name = "myprefix_target";
target.stream = &std::cout;
// Set user data for the prefix named myprefix
target.prefixData["myprefix"] = fcf::NTest::SharedPtrAny::make<std::string>("from target");;
// Register the target
logger.targets({target});
// In this implementation, we manually populate the prefixData for the target
// to simulate a factory-created object.
fcf::NTest::Logger::Prefix prefix;
prefix.name = "myprefix";
prefix.category = fcf::NTest::LMC_USER_GROUP;
prefix.dataFactory = []( fcf::NTest::Logger&, fcf::NTest::Logger::OutputTarget&) {
// This factory will not be called because user data is already specified in the target field
return fcf::NTest::SharedPtrAny::make<std::string>("from factory");
};
prefix.handler = []( fcf::NTest::Logger&, fcf::NTest::Logger::MessageContext& a_context) -> std::string {
// This is a conceptual demonstration of how prefixData is accessed
std::string* userData = a_context.data->cast<std::string>();
return "[Prefix (" + *userData + ")] ";
};
// Register the prefix
logger.appendPrefix(prefix);
bool error = false;
fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error);
return error ? 1 : 0;
}
Output:
Performing the test: "PrefixDemo" -> "Logger" -> "CounterTest" ...
> [Prefix (from target)] Message 1
> [Prefix (from target)] Message 2
[SUCCESS] Test completed successfully (0.000`007`974 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.000`007`974 sec