Type:
class fcf::NTest::Logger::Prefix
Owner class: fcf::NTest::Logger
Package: fcfTest
File: test.hpp
Available from version: 1.1.13
A configuration structure used to define how prefixes (static strings or dynamic functions) are applied to log messages.
The fcf::NTest::Logger::Prefix structure allows fine-grained control over log prefixes. It can be used to attach static strings or dynamic functional callbacks to log entries. Each setting can be scoped to specific message categories using a bitmask, ensuring that prefixes are only applied to relevant logs (e.g., only to test results or only to user-defined messages).
Properties
- The name of the prefix setting.
- Determines if the prefix should be applied to every line of a multi-line message.
unsigned int category
=
fcf::NTest::LMC_ALL
- Bitmask of message categories to which this prefix applies.
- A factory function used to initialize and provide a unique
fcf::NTest::SharedPtrAny data object for a specific
fcf::NTest::Logger::OutputTarget and
fcf::NTest::Logger::Prefix combination.
- A static string that is prepended to every log message matching the specified category.
fcf::NTest::Logger::PrefixFunction handler
- A callback function used to dynamically generate a prefix string for log messages.
Example: Customizing Log Prefixes
Demonstrates how to use Prefix to add both a static indentation and a dynamic timestamp prefix to log messages, filtered by message categories.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <ctime>
#include <iomanip>
#include <sstream>
FCF_TEST_DEFINE("LoggerDemo", "Prefixes", "CustomPrefixTest") {
// Test the output
fcf::NTest::inf() << "This message has both prefixes!" << std::endl;
FCF_TEST(true);
}
int main(int a_argc, char* a_argv[]) {
// 1. Create a Prefix object for a static string prefix
fcf::NTest::Logger::Prefix staticPrefix;
staticPrefix.name = "static";
staticPrefix.multiLine = false;
staticPrefix.category = fcf::NTest::LMC_ALL;
staticPrefix.prefix = " >> ";
// 2. Create a Prefix object for a dynamic functional prefix (timestamp)
fcf::NTest::Logger::Prefix dynamicPrefix;
dynamicPrefix.name = "timestamp";
dynamicPrefix.multiLine = true;
dynamicPrefix.category = fcf::NTest::LMC_ALL;
dynamicPrefix.handler = []( fcf::NTest::Logger& a_logger, fcf::NTest::Logger::MessageContext& a_context) {
auto time = std::time(nullptr);
std::stringstream ss;
ss << "[" << std::put_time(std::localtime(&time), "%H:%M:%S") << "] ";
return ss.str();
};
// 3. Apply the settings to the global logger
fcf::NTest::logger().appendPrefix(staticPrefix);
fcf::NTest::logger().appendPrefix(dynamicPrefix);
bool error = false;
fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error);
return error ? 1 : 0;
}
Output:
>> [17:38:53] Performing the test: "LoggerDemo" -> "Prefixes" -> "CustomPrefixTest" ...
>> [17:38:53] [SUCCESS] Test completed successfully (0.000`002`669 sec)
>> [17:38:53]
>> [17:38:53] [SUCCESS] All tests were completed.
>> [17:38:53] Tests: 1 passed, 0 failed, 0 skipped, 1 total
>> [17:38:53] Duration: 0.000`002`669 sec