Type:
class fcf::NTest::Logger
Package: fcfTest
File: test.hpp
Available from version: 1.0.1
A configurable logging utility providing multiple severity levels and customizable output formatting via static prefixes or functional callbacks
The fcf::NTest::Logger class manages diagnostic output through various severity levels. It supports both global access via singleton-like functions and independent logger instances. The logger can be customized with static string prefixes or dynamic functional prefixes (e.g., for timestamps).
The logger filters messages based on the current fcf::NTest::ELogLevel. Only messages with a level equal to or higher than the set threshold are processed.
- fcf::NTest::LL_FTL (Fatal), fcf::NTest::LL_ERR (Error), fcf::NTest::LL_WRN (Warning), fcf::NTest::LL_ATT (Attention), fcf::NTest::LL_LOG (Log), fcf::NTest::LL_INF (Info), fcf::NTest::LL_DBG (Debug), fcf::NTest::LL_TRC (Trace).
Usage Examples
1. Basic Logging
Using global shortcut functions for immediate output.
// Default level is LL_LOG
fcf::NTest::inf() << "System initialized." << std::endl;
fcf::NTest::err() << "Critical error detected!" << std::endl;
// Debug messages won't appear unless level is changed
fcf::NTest::dbg() << "This is hidden by default." << std::endl;
2. Customizing Prefixes
Adding static strings or dynamic functions (e.g., timestamps) to every log entry.
fcf::NTest::Logger::Prefix prefix;
prefix.name = "static-prefix";
prefix.prefix = "[MY_APP] ";
fcf::NTest::logger().appendPrefix("prefix");
// Dynamic functional prefix (e.g., for timestamps)
prefix.name = "dynamic-prefix";
prefix.handler = []( fcf::NTest::Logger&, fcf::NTest::Logger::MessageContext& a_context) {
return "[" + fcf::NTest::Logger::toLevelStr(a_context.level) + "] ";
};
fcf::NTest::log() << "Message with custom prefix." << std::endl;
// Output: [MY_APP] [log] Message with custom prefix.
3. Changing Log Level
Adjusting verbosity at runtime.
fcf::NTest::logger().levelStr("dbg");
fcf::NTest::dbg() << "Now debug messages are visible." << std::endl;
// Or using enum
fcf::NTest::logger().levelStr(fcf::NTest::LL_ALL);
4. Custom Output Formatting
If the built-in output formats do not meet your requirements, you can easily define and register your own custom formatting handler.
// Create a new format
fcf::NTest::Logger::Format customFormat;
customFormat.name = "my-custom-format";
customFormat.handler = [](fcf::NTest::Logger&, fcf::NTest::Logger::MessageContext& a_context) {
a_context.message = "[message: " + (std::stringstream() << std::hex << a_context.category).str() + "]\n";
};
// Add the format to the logger
logger.appendFormat(customFormat);
Typedef
typedef Prefixes - A collection of prefix configurations for the logger.
typedef Formats - A collection of formatting configurations for the logger.
typedef OutputTargets - A collection of output targets for the logger.
typedef DataFactory - A factory function for creating type-erased user data for loggers.
typedef HandlerDataMap - A map used to store user-defined metadata for logger handlers.
Nested classes
MessageContext
- A context structure used by the logger to encapsulate all metadata and content of a single log message during the formatting process.
OutputTarget
- A structure representing a specific output destination for log messages, including the stream, name, and associated formatting settings.
Prefix
- A configuration structure used to define how prefixes (static strings or dynamic functions) are applied to log messages.
Format
- A configuration structure used to define the name of a specific output format for the logger.
Writer
- An RAII-based stream wrapper used for buffering log messages before they are dispatched to the central Logger.
Static methods
fcf::NTest::ELogLevel
toLevel(
std::string a_level,
fcf::NTest::ELogLevel a_default
= fcf::NTest::LL_LOG)
- Converts a string representation of a log level to its enum value.
- Converts an fcf::NTest::ELogLevel enumeration value into its corresponding human-readable string representation.
Methods
- Default constructor that initializes a new logger instance with the default log level set to LL_LOG.
fcf::NTest::ELogLevel
level()
const
void
level(
fcf::NTest::ELogLevel a_level)
- Receive or set the current log level (fcf::NTest::ELogLevel) value depending on the parameters given.
- Returns or sets a new log level value in string format
fcf::NTest::Logger::Prefixes
prefixes()
const
void
prefixes(
fcf::NTest::Logger::Prefixes a_prefixes)
- Provides access to the current collection of prefix configurations.
- Clears the current prefixes from the logger, optionally restoring default ones.
void
appendPrefix(
const fcf::NTest::Logger::Prefix& a_prefix)
- Adds a new prefix configuration to the logger.
fcf::NTest::Logger::Formats
formats()
const
void
formats(
fcf::NTest::Logger::Formats a_formats)
- Provides access to the current collection of formatting configurations.
- Clears all current formatting configurations from the logger.
void
appendFormat(
const fcf::NTest::Logger::Format& a_format)
- Adds a new formatting configuration to the logger.
fcf::NTest::Logger::OutputTargets
targets()
const
void
targets(
fcf::NTest::Logger::OutputTargets a_targets)
- Provides access to the current collection of output targets.
- Clears all current output targets from the logger, optionally restoring default ones.
void
appendTarget(
const fcf::NTest::Logger::OutputTarget& a_stream)
- Adds a new output target to the logger.
fcf::NTest::Logger::Writer
ftl(
unsigned int a_messageCategory
= fcf::NTest::LMC_USER_GROUP)
- Returns an fcf::NTest::Logger::Writer object for fatal-level log messages. This level is used for critical failures that require immediate attention.
fcf::NTest::Logger::Writer
err(
unsigned int a_messageCategory
= fcf::NTest::LMC_USER_GROUP)
- Returns an fcf::NTest::Logger::Writer object for error-level log messages. This level is used to report significant issues that do not necessarily terminate the application
fcf::NTest::Logger::Writer
wrn(
unsigned int a_messageCategory
= fcf::NTest::LMC_USER_GROUP)
- Returns an fcf::NTest::Logger::Writer object for warning-level log messages. This level is used for warnings that require immediate attention.
fcf::NTest::Logger::Writer
att(
unsigned int a_messageCategory
= fcf::NTest::LMC_USER_GROUP)
- Returns an fcf::NTest::Logger::Writer object for attention-level log messages. This level is used for important message that require attention.
fcf::NTest::Logger::Writer
log(
unsigned int a_messageCategory
= fcf::NTest::LMC_USER_GROUP)
- Returns an fcf::NTest::Logger::Writer object for default level log messages. This level is used for simple message that require attention.
fcf::NTest::Logger::Writer
inf(
unsigned int a_messageCategory
= fcf::NTest::LMC_USER_GROUP)
- Returns an fcf::NTest::Logger::Writer object for info level log messages. This level is used for message with a secondary importance
fcf::NTest::Logger::Writer
dbg(
unsigned int a_messageCategory
= fcf::NTest::LMC_USER_GROUP)
- Returns an fcf::NTest::Logger::Writer object for debug log messages. This logging level is used to output the debugging mesasge.
fcf::NTest::Logger::Writer
trc(
unsigned int a_messageCategory
= fcf::NTest::LMC_USER_GROUP)
- Returns an fcf::NTest::Logger::Writer object for trace log messages. This logging level is used to extract tracing data.