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

fcf::NTest::Logger class

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

Detailed description

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().addedPrefixStr("[MY_APP] "); // Dynamic functional prefix (e.g., for timestamps) fcf::NTest::logger().addedPrefixFunc([](fcf::NTest::Logger& a_logger, fcf::NTest::ELogLevel a_level) { return "[" + fcf::NTest::Logger::toLevelStr(a_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().setLevelStr("dbg"); fcf::NTest::dbg() << "Now debug messages are visible." << std::endl; // Or using enum fcf::NTest::logger().setLevelStr(fcf::NTest::LL_ALL);

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.
const char* toLevelStr()
- Converts an fcf::NTest::ELogLevel enumeration value into its corresponding human-readable string representation.
Methods
[CONSTRUCTOR] Logger()
- Default constructor that initializes a new logger instance with the default log level set to LL_LOG.

fcf::NTest::ELogLevel getLevel()
- Returns the current logging threshold level as an fcf::NTest::ELogLevel enumeration value.
void setLevel(fcf::NTest::ELogLevel a_level)
- Sets the current logging threshold level, determining the minimum severity required for a message to be processed
const char* getLevelStr()
- Returns a pointer to a static string representing the current log level name
void setLevelStr(const char* a_level)
- Sets the logger's verbosity threshold using a string identifier (e.g., "dbg", "err")
void addedPrefixStr(const std::string& a_prefix)
- Appends a static string as a prefix to all subsequent log messages produced by the logger.
void addedPrefixFunc(const fcf::NTest::Loger::PrefixFunctionType& a_prefix)
- Adds a dynamic functional prefix to all subsequent log messages, allowing for real-time data injection such as timestamps or thread IDs
std::ostream& ftl()
- Returns a reference to the output stream for fatal-level log messages. This level is used for critical failures that require immediate attention.
std::ostream& err()
- Returns a reference to the output stream for error-level log messages. This level is used to report significant issues that do not necessarily terminate the application
std::stream& wrn()
- Returns a reference to the output stream for warning-level log messages. This level is used for warnings that require immediate attention.
std::stream& att()
- Returns a reference to the output stream for attention-level log messages. This level is used for important message that require attention.
std::stream& log()
- Returns a reference to the output stream for default level log messages. This level is used for simple message that require attention.
std::stream& inf()
- Returns a reference to the output stream for info level log messages. This level is used for message with a secondary importance
std::stream& dbg()
- Returns a reference to the output stream for debug log messages. This logging level is used to output the debugging mesasge.
std::stream& trc()
- Returns a reference to the output stream for trace log messages. This logging level is used to extract tracing data.