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

MessageContext class
From fcf::NTest::Logger class

Type:
class fcf::NTest::Logger::MessageContext

Owner class: fcf::NTest::Logger

Package: fcfTest

File: test.hpp

Available from version: 1.1.14

A context structure used by the logger to encapsulate all metadata and content of a single log message during the formatting process.

The fcf::NTest::Logger::MessageContext structure is an internal data carrier used by the fcf::NTest::Logger during the message processing pipeline. When a log entry is created, a context object is populated with essential information such as the message category, the origin string, the actual message content, the line number, the log level, and the target output stream. This allows various fcf::NTest::Logger::Format and fcf::NTest::Logger::Prefix handlers to access and modify the message content or metadata uniformly before it is finally written to the output targets.

Properties
unsigned int category
- The bitmask category of the log message.
bool system
- Flag indicating whether the log message should be treated as system message. System messages are not written to the logging stream.
std::string origin
- Contains the original message string
std::string message
- The actual text content of the log message.
size_t line
- The line number within the message where the current prefix is being applied.
fcf::NTest::ELogLevel level
- The severity level of the log message.
std::ostream* stream
- Pointer to the output stream target for the current log message.
fcf::NTest::SharedPtrAny* data
- A pointer to user-defined metadata associated with the current stream and formatter.

Example: Internal Context Usage

Since MessageContext is an internal structure managed by the Logger, it is not typically instantiated manually by users. However, this example demonstrates how the Logger internally populates and uses this context to process a message with a custom prefix.

#define FCF_TEST_IMPLEMENTATION #include <fcfTest/test.hpp> #include <iostream> FCF_TEST_DEFINE("LoggerDemo", "Context", "InternalUsage"){ fcf::NTest::Logger& a_logger = fcf::NTest::logger(); // Register a functional prefix fcf::NTest::Logger::Prefix prefix; prefix.name = "context-inspector"; prefix.multiLine = false; prefix.category = fcf::NTest::LMC_USER_GROUP; // Note: Users do not manually create MessageContext. // It is created and passed to prefixes/formats by the Logger. prefix.handler = []( fcf::NTest::Logger& logger, fcf::NTest::Logger::MessageContext& a_context) { // Inside this lambda, we have access to the MessageContext object // We can read properties like context.level or context.origin return std::string() + "[Level: " + fcf::NTest::Logger::toLevelStr(a_context.level) + "] "; }; a_logger.appendPrefix(prefix); // 3. Trigger a log event // The Logger will internally create a MessageContext and pass it to our lambda fcf::NTest::log() << "This message is processed via MessageContext!" << std::endl; } int main(int a_argc, char* a_argv[]) { bool error = false; fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error); return error ? 1 : 0; }

Output:

Performing the test: "LoggerDemo" -> "Context" -> "InternalUsage" ... > [Level: log] This message is processed via MessageContext! [SUCCESS] Test completed successfully (0.000`013`504 sec) [SUCCESS] All tests were completed. Tests: 1 passed, 0 failed, 0 skipped, 1 total Duration: 0.000`013`504 sec