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

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

void addedPrefixFunc(const fcf::NTest::Loger::PrefixFunctionType& a_prefix)

Class: fcf::NTest::Logger

Package: fcfTest

File: test.hpp

Available from version: 1.0.1

Adds a dynamic functional prefix to all subsequent log messages, allowing for real-time data injection such as timestamps or thread IDs

The addedPrefixFunc method allows you to attach a callback function that generates a prefix for every log entry. Unlike static strings, this functional prefix is evaluated at the moment the log message is written, making it ideal for dynamic metadata.

The callback receives a reference to the fcf::NTest::Logger instance and the current fcf::NTest::ELogLevel, allowing the prefix to change based on the severity of the message.

Arguments

const fcf::NTest::Loger::PrefixFunctionType& a_prefix
- A std::function with the signature std::string(fcf::NTest::Logger&, fcf::NTest::ELogLevel)

Example: Adding dynamic timestamps

The most common use case is prefixing logs with a human-readable timestamp

#include <ctime> #include <iomanip> #include <sstream> #define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> int main(int a_argc, char* a_argv[]) { fcf::NTest::logger().addedPrefixFunc([](fcf::NTest::Logger& a_logger, fcf::NTest::ELogLevel a_level) { auto time = std::time(nullptr); return (std::stringstream() << "[" << std::put_time(std::localtime(&time), "%H:%M:%S") << "] " << "[" << a_logger.toLevelStr(a_level) << "] " ).str(); }); fcf::NTest::log() << "System started." << std::endl; return 0; }

Output:

[14:30:05] [inf] System started.