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

handler from class fcf::NTest::Logger::Format

Property: handler

Type: fcf::NTest::Logger::FormatFunction

Class: fcf::NTest::Logger::Format

Package: fcfTest

File: test.hpp

Available from version: 1.2.1

A callback function responsible for formatting the log message content.

The func property is a fcf::NTest::Logger::FormatFunction (a std::function wrapper). It defines the logic for transforming a raw log message into its final string representation.

The function is invoked by the fcf::NTest::Logger during the output phase. It receives a reference to the logger instance and a fcf::NTest::Logger::MessageContext object, which contains all the necessary metadata (category, level, origin, etc.) and the message itself. The return value of this function becomes the actual content written to the output target.

Example: Custom Message Formatter

Demonstrating how to implement a custom handler to wrap log messages in brackets and convert them to uppercase.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <iostream> #include <algorithm> #include <string> #include <regex> FCF_TEST_DEFINE("FormatterDemo", "Logger", "CustomFuncTest") { fcf::NTest::log() << "hello world" << std::endl; FCF_TEST(true); } // Message formatting std::string formatMessage(std::string a_string) { // Remove terminal control characters std::regex ansi_regex("\x1B\\[[0-?]*[ -/]*[@-~]"); std::string result = std::regex_replace(a_string, ansi_regex, ""); // Remove line breaks result.erase(result.find_last_not_of(" \t\n\r\f\v") + 1); // Convert to uppercase std::transform(result.begin(), result.end(), result.begin(), ::toupper); return result; } int main(int a_argc, char* a_argv[]) { fcf::NTest::Logger& logger = fcf::NTest::logger(); // Prepare Format fcf::NTest::Logger::Format format; // Replace the default format format.name = "default"; // Custom formatting function format.handler = []( fcf::NTest::Logger&, fcf::NTest::Logger::MessageContext& a_context) { // Transform message to uppercase std::string msg = formatMessage(a_context.message); // Return the formatted message wrapped in brackets a_context.message = "[" + msg + "]\n"; }; // Register the format logger.appendFormat(format); bool error = false; fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error); return error ? 1 : 0; }

Output:

[PERFORMING THE TEST: "FORMATTERDEMO" -> "LOGGER" -> "CUSTOMFUNCTEST" ...] [ > HELLO WORLD] [ [SUCCESS] TEST COMPLETED SUCCESSFULLY (0.000`117`832 SEC)] [] [[SUCCESS] ALL TESTS WERE COMPLETED.] [TESTS: 1 PASSED, 0 FAILED, 0 SKIPPED, 1 TOTAL] [DURATION: 0.000`117`832 SEC]