Type: std::string
Class: fcf::NTest::Logger::MessageContext
Package: fcfTest
File: test.hpp
Available from version: 1.1.14
The actual text content of the log message.
The message property holds the primary text content of the log entry. During the formatting process, this string can be modified by fcf::NTest::Logger::Prefix or fcf::NTest::Logger::Format handlers, allowing for advanced transformations such as adding indentation, wrapping text, or injecting metadata directly into the message body.
Example: Modifying Message Content via Formatter
Demonstrating how a custom fcf::NTest::Logger::Format can intercept and modify the message property (e.g., converting it to uppercase).
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <iostream>
#include <algorithm>
FCF_TEST_DEFINE("MessageDemo", "Logger", "MessageTransform") {
// Log a message
fcf::NTest::log() << "hello world" << std::endl;
}
void uppercaseTextOnly(std::string &a_string) {
bool inAnsiCode = false;
for (size_t i = 0; i < a_string.length(); ++i) {
// Hit the Escape character (\x1B or \033) — the control sequence starts
if (a_string[i] == '\x1B') {
inAnsiCode = true;
continue;
}
if (inAnsiCode) {
// ANSI escape sequences always end with a letter (ASCII 64-126)
// e.g., 'm' in \x1B[0m or 'H' in cursor positioning commands
if ((a_string[i] >= 'a' && a_string[i] <= 'z') || (a_string[i] >= 'A' && a_string[i] <= 'Z')) {
inAnsiCode = false;
}
continue; // Skip modification for this character
}
// Outside of the control sequence — safe to convert to uppercase
a_string[i] = static_cast<char>(std::toupper(static_cast<unsigned char>(a_string[i])));
}
}
int main(int a_argc, char* a_argv[]) {
fcf::NTest::Logger& logger = fcf::NTest::logger();
// 1. Define a custom format function
fcf::NTest::Logger::FormatFunction upperCaseFormat = []( fcf::NTest::Logger& logger, fcf::NTest::Logger::MessageContext& context) {
std::string upperMsg = context.message;
uppercaseTextOnly(upperMsg);
context.message = upperMsg;
};
// 2. Apply the format to the logger
fcf::NTest::Logger::Format format;
format.name = "uppercase";
format.handler = upperCaseFormat;
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: "MESSAGEDEMO" -> "LOGGER" -> "MESSAGETRANSFORM" ...
> HELLO WORLD
[SUCCESS] TEST COMPLETED SUCCESSFULLY (0.000`009`274 SEC)
[SUCCESS] ALL TESTS WERE COMPLETED.
TESTS: 1 PASSED, 0 FAILED, 0 SKIPPED, 1 TOTAL
DURATION: 0.000`009`274 SEC
The application must be launched with the format specified via the command line:
app --test-format "uppercase"