ifw-fnd 2.0.1
 
Loading...
Searching...
No Matches
logger.hpp File Reference

ifw-fnd logging abstraction. More...

#include <chrono>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include <fmt/format.h>

Go to the source code of this file.

Classes

class  ifw::fnd::Logger
 Abstract logger; downstream supplies an implementation. More...
 
class  ifw::fnd::ScopedTracer
 RAII tracer used by FNDTRACE. More...
 

Namespaces

namespace  ifw
 
namespace  ifw::fnd
 This source file contains definitions of the types and constants to handle data types.
 

Macros

Logging macros

Usage:

FNDINFO("connected to {} on port {}", host, port);
FNDERROR("call failed: {}", status);
FNDTHROW("invalid argument: {}", arg); // logs ERROR then throws
#define FNDTHROW(fmt_str,...)
Definition logger.hpp:314
#define FNDINFO(fmt_str,...)
Definition logger.hpp:305
#define FNDERROR(fmt_str,...)
Definition logger.hpp:311

Each macro performs a single early-out level test before formatting, so disabled-level calls are essentially free. FNDTHROW does not early-out — a throw must happen unconditionally.

#define FND_LOG_IMPL_(level_, fmt_str_, ...)
 
#define FND_TRACE_NAME_(line)
 Scoped trace macro.
 
#define FND_TRACE_NAME(line)
 
#define FNDTRACE(...)
 
#define FNDDEBUG(fmt_str, ...)
 
#define FNDINFO(fmt_str, ...)
 
#define FNDWARNING(fmt_str, ...)
 
#define FNDERROR(fmt_str, ...)
 
#define FNDTHROW(fmt_str, ...)
 

Enumerations

enum class  ifw::fnd::LogLevel : int {
  ifw::fnd::TRACE = 0 , ifw::fnd::DEBUG = 10 , ifw::fnd::INFO = 20 , ifw::fnd::WARNING = 30 ,
  ifw::fnd::ERROR = 40 , ifw::fnd::OFF = 100
}
 Log severity, lowest to highest. More...
 

Functions

void ifw::fnd::InstallLogger (std::unique_ptr< Logger > logger) noexcept
 Install the process-wide logger.
 
Loggerifw::fnd::Log ()
 Access the installed logger.
 
bool ifw::fnd::HasLogger () noexcept
 true iff a logger has been installed.
 
std::unique_ptr< Loggerifw::fnd::MakeStdoutLogger ()
 Factory: a logger that prints to stdout.
 
std::unique_ptr< Loggerifw::fnd::MakeNullLogger ()
 Factory: a logger that discards everything.
 
std::string_view ifw::fnd::LogLevelName (LogLevel level) noexcept
 Render a LogLevel as a short uppercase string.
 
std::string ifw::fnd::TraceExtra ()
 Helper used by FNDTRACE.
 
template<typename... Args>
std::string ifw::fnd::TraceExtra (fmt::string_view fmt_str, Args &&... args)
 

Detailed Description

ifw-fnd logging abstraction.

ifw-fnd is meant to build on both ELT and VLTSW environments and must not impose a specific logging backend. This header defines an abstract Logger interface. Downstream applications subclass it, instantiate one logger object, and hand ownership to ifw-fnd at startup via InstallLogger(). Until a logger has been installed, calls to Log() throw — ifw-fnd refuses to silently swallow log output. Two ready-made implementations are provided as convenience: a stdout logger and a null (silent) logger.

Call sites use the FNDTRACE / FNDDEBUG / FNDINFO / FNDWARNING / FNDERROR / FNDTHROW macros. Each macro is fmt-style and includes an early-out test on the configured log level, so disabled-level calls cost a single integer compare.

Note: this header is dependency-free (stdlib + fmt only). It does NOT pull in log4cplus, CII, or any other ESO-only library. The point of the abstraction is that consumers of ifw-fnd inherit that freedom.

Macro Definition Documentation

◆ FND_LOG_IMPL_

#define FND_LOG_IMPL_ ( level_,
fmt_str_,
... )
Value:
do { \
auto& _fnd_log = ::ifw::fnd::Log(); \
if (_fnd_log.GetLogLevel() <= (level_)) { \
_fnd_log.Emit((level_), \
FNDLOC + ": " + \
::fmt::format( \
::fmt::runtime(std::string(fmt_str_)) \
__VA_OPT__(,) __VA_ARGS__)); \
} \
} while (0)
#define FNDLOC
Macro generating a location identifier: "<iso-time>:<file>:<line>:<function>:<thread>".
Definition base.hpp:34
Logger & Log()
Access the installed logger.
Definition defs.cpp:120

◆ FND_TRACE_NAME

#define FND_TRACE_NAME ( line)
Value:
#define FND_TRACE_NAME_(line)
Scoped trace macro.
Definition logger.hpp:287

◆ FND_TRACE_NAME_

#define FND_TRACE_NAME_ ( line)
Value:
_fnd_trace_##line

Scoped trace macro.

Emits ENTERING on the line and LEAVING + elapsed time when the enclosing scope ends. Both lines log at LogLevel::TRACE and include the calling function (via __PRETTY_FUNCTION__). Optional format string + args append an extra note that appears on BOTH lines so a reader can correlate them in interleaved multi-thread output.

FNDTRACE(); // -> "ENTERING: <loc>: <fn>"
// "LEAVING: <loc>: <fn> Time: 0.012s"
FNDTRACE("got {}", n); // -> "ENTERING: <loc>: <fn>: got 42"
// "LEAVING: <loc>: <fn>: got 42 Time: 0.012s"
#define FNDTRACE(...)
Definition logger.hpp:290

Place at the top of any function whose enter/leave timing you want captured. The macro expands to an RAII object held by the enclosing scope. Nested traces in the same thread indent visually.

COST CONTRACT: when TRACE is disabled, the cost is one atomic load (HasLogger) plus one integer compare (level). The FNDLOC expansion (which calls IsoTimeNow + ThreadName, both syscalls) and the formatting of any extra args are SKIPPED entirely. Without this short-circuit, leaving FNDTRACE in hot paths stalled the data plane at the millisecond scale.

◆ FNDDEBUG

#define FNDDEBUG ( fmt_str,
... )
Value:
FND_LOG_IMPL_(::ifw::fnd::LogLevel::DEBUG, fmt_str __VA_OPT__(,) __VA_ARGS__)
#define FND_LOG_IMPL_(level_, fmt_str_,...)
Definition logger.hpp:250
@ DEBUG
Definition logger.hpp:57

◆ FNDERROR

#define FNDERROR ( fmt_str,
... )
Value:
FND_LOG_IMPL_(::ifw::fnd::LogLevel::ERROR, fmt_str __VA_OPT__(,) __VA_ARGS__)
@ ERROR
Definition logger.hpp:60

◆ FNDINFO

#define FNDINFO ( fmt_str,
... )
Value:
FND_LOG_IMPL_(::ifw::fnd::LogLevel::INFO, fmt_str __VA_OPT__(,) __VA_ARGS__)
@ INFO
Definition logger.hpp:58

◆ FNDTHROW

#define FNDTHROW ( fmt_str,
... )
Value:
do { \
::ifw::fnd::Log().Throw( \
FNDLOC + ": " + \
::fmt::format( \
::fmt::runtime(std::string(fmt_str)) \
__VA_OPT__(,) __VA_ARGS__)); \
} while (0)

◆ FNDTRACE

#define FNDTRACE ( ...)
Value:
[[maybe_unused]] ::ifw::fnd::ScopedTracer FND_TRACE_NAME(__LINE__) { \
::ifw::fnd::Log().GetLogLevel() <= ::ifw::fnd::LogLevel::TRACE) \
? (FNDLOC + ": " + std::string(__PRETTY_FUNCTION__)) \
: std::string{}, \
::ifw::fnd::Log().GetLogLevel() <= ::ifw::fnd::LogLevel::TRACE) \
? ::ifw::fnd::TraceExtra(__VA_ARGS__) \
: std::string{} \
}
RAII tracer used by FNDTRACE.
Definition logger.hpp:213
#define FND_TRACE_NAME(line)
Definition logger.hpp:288
@ TRACE
Definition logger.hpp:56
std::string TraceExtra()
Helper used by FNDTRACE.
Definition logger.hpp:188
bool HasLogger() noexcept
true iff a logger has been installed.
Definition defs.cpp:131

◆ FNDWARNING

#define FNDWARNING ( fmt_str,
... )
Value:
FND_LOG_IMPL_(::ifw::fnd::LogLevel::WARNING, fmt_str __VA_OPT__(,) __VA_ARGS__)
@ WARNING
Definition logger.hpp:59