FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
log.hpp File Reference

Asynchronous logging (5.10). More...

#include "fastmm/core/config_macros.hpp"
#include "fastmm/core/enums.hpp"
#include "fastmm/core/fixed_point.hpp"
#include "fastmm/core/fixed_string.hpp"
#include "fastmm/core/spsc_ring.hpp"
#include "fastmm/core/strong_id.hpp"
#include "fastmm/core/time.hpp"
#include <fmt/base.h>
#include <atomic>
#include <concepts>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <string_view>
#include <type_traits>

Go to the source code of this file.

Classes

struct  fastmm::LogDescriptor
 
struct  fastmm::LogRecord
 
struct  fastmm::Secret< T >
 Wrap a value to have it rendered as "***". More...
 
class  fastmm::Logger
 Process-wide registry of per-thread rings + the sink thread. Implemented in log_sink.cpp. More...
 

Namespaces

namespace  fastmm
 

Macros

#define FASTMM_LOG_IMPL(lvl, fmt_str, ...)
 fmt_str must be a string literal.
 
#define FASTMM_LOG_TRACE(fmt_str, ...)    FASTMM_LOG_IMPL(::fastmm::LogLevel::Trace, fmt_str __VA_OPT__(, ) __VA_ARGS__)
 
#define FASTMM_LOG_DEBUG(fmt_str, ...)    FASTMM_LOG_IMPL(::fastmm::LogLevel::Debug, fmt_str __VA_OPT__(, ) __VA_ARGS__)
 
#define FASTMM_LOG_INFO(fmt_str, ...)    FASTMM_LOG_IMPL(::fastmm::LogLevel::Info, fmt_str __VA_OPT__(, ) __VA_ARGS__)
 
#define FASTMM_LOG_WARN(fmt_str, ...)    FASTMM_LOG_IMPL(::fastmm::LogLevel::Warn, fmt_str __VA_OPT__(, ) __VA_ARGS__)
 
#define FASTMM_LOG_ERROR(fmt_str, ...)    FASTMM_LOG_IMPL(::fastmm::LogLevel::Error, fmt_str __VA_OPT__(, ) __VA_ARGS__)
 

Typedefs

using fastmm::LogRing = SpscRing< LogRecord, kLogRingSize >
 

Enumerations

enum class  fastmm::LogArgType : std::uint8_t {
  fastmm::Int = 1 , fastmm::Uint = 2 , fastmm::Double = 3 , fastmm::Bool = 4 ,
  fastmm::Char = 5 , fastmm::PriceFx = 6 , fastmm::QtyFx = 7 , fastmm::NotionalFx = 8 ,
  fastmm::Str = 9 , fastmm::Secret = 10
}
 

Functions

template<class T >
Secret< std::decay_t< T > > fastmm::secret (T &&v)
 
FASTMM_FORCE_INLINE bool fastmm::log_enabled (LogLevel lvl) noexcept
 

Variables

constexpr std::size_t fastmm::kLogRecordBytes = 256
 
constexpr std::size_t fastmm::kLogMaxStrBytes = 64
 
constexpr std::size_t fastmm::kLogRingSize = 8192
 records per thread (2 MiB)
 
constexpr std::size_t fastmm::kLogMaxThreads = 64
 
std::atomic< std::uint8_t > fastmm::g_log_level {static_cast<std::uint8_t>(LogLevel::Info)}
 Global level (runtime); FASTMM_MIN_LOG_LEVEL is the compile-time floor.
 

Detailed Description

Asynchronous logging (5.10).

FASTMM_LOG_INFO("filled {} \@ {} on {}", qty, px, side);

The call site captures a pointer to a static LogDescriptor (format string, file, line, level) plus the packed argument values into a 256-byte LogRecord in the calling thread's SpscRing. Nothing is formatted on the caller's thread; the LogSink thread pops records, formats them with fmt and writes them to a FILE*. When a ring is full the record is dropped and counted - the hot path never blocks.

The format string is validated at compile time against the argument types (fmt), so a mismatch is a compile error rather than a runtime surprise on the sink thread.

Supported argument types: integers, floating point, bool, char, Price/Qty/Notional (formatted as exact decimals), enums (via to_string() when available, else the underlying integer), string_view / const char* / std::string / FixedString (copied, truncated to kLogMaxStrBytes), Timestamp/Duration (ns as integers), Secret<T> ("***").

Macro Definition Documentation

◆ FASTMM_LOG_IMPL

#define FASTMM_LOG_IMPL (   lvl,
  fmt_str,
  ... 
)
Value:
do { \
if constexpr (::fastmm::detail::log_compiled(lvl)) { \
if (::fastmm::log_enabled(lvl)) { \
static constexpr ::fastmm::LogDescriptor fastmm_log_desc_{ \
fmt_str, __FILE__, __LINE__, lvl}; \
[&](const auto&... fastmm_a_) { \
static_cast<void>( \
::fmt::format_string<::fastmm::detail::LogFmtType<decltype(fastmm_a_)>...>( \
fmt_str)); \
::fastmm::detail::log_emit(fastmm_log_desc_, fastmm_a_...); \
}(__VA_ARGS__); \
} \
} \
} while (0)
FASTMM_FORCE_INLINE bool log_enabled(LogLevel lvl) noexcept
Definition log.hpp:92

fmt_str must be a string literal.

The generic lambda lets us spell the packed argument types for the compile-time format check without touching the arguments' values.

◆ FASTMM_LOG_TRACE

#define FASTMM_LOG_TRACE (   fmt_str,
  ... 
)     FASTMM_LOG_IMPL(::fastmm::LogLevel::Trace, fmt_str __VA_OPT__(, ) __VA_ARGS__)

◆ FASTMM_LOG_DEBUG

#define FASTMM_LOG_DEBUG (   fmt_str,
  ... 
)     FASTMM_LOG_IMPL(::fastmm::LogLevel::Debug, fmt_str __VA_OPT__(, ) __VA_ARGS__)

◆ FASTMM_LOG_INFO

#define FASTMM_LOG_INFO (   fmt_str,
  ... 
)     FASTMM_LOG_IMPL(::fastmm::LogLevel::Info, fmt_str __VA_OPT__(, ) __VA_ARGS__)

◆ FASTMM_LOG_WARN

#define FASTMM_LOG_WARN (   fmt_str,
  ... 
)     FASTMM_LOG_IMPL(::fastmm::LogLevel::Warn, fmt_str __VA_OPT__(, ) __VA_ARGS__)

◆ FASTMM_LOG_ERROR

#define FASTMM_LOG_ERROR (   fmt_str,
  ... 
)     FASTMM_LOG_IMPL(::fastmm::LogLevel::Error, fmt_str __VA_OPT__(, ) __VA_ARGS__)