FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
fastmm::Logger Class Reference

Process-wide registry of per-thread rings + the sink thread. Implemented in log_sink.cpp. More...

#include <fastmm/core/log.hpp>

Public Member Functions

void set_level (LogLevel lvl) noexcept
 
LogLevel level () const noexcept
 
LogRing * attach_current_thread ()
 Gives the calling thread a ring: a drained ring released by an exited thread if one exists, otherwise a new one (heap).
 
void detach_current_thread () noexcept
 Returns the calling thread's ring to the pool.
 
std::size_t thread_slots () const noexcept
 Number of rings allocated so far; reused rings are not counted again.
 
void start (std::FILE *out=stderr, LogLevel mirror_level=LogLevel::Warn)
 Sink control.
 
bool start_file (const std::string &path, LogLevel mirror_level=LogLevel::Warn)
 Opens path for appending and starts the sink on it.
 
void stop ()
 drains everything then joins the sink thread (and closes a start_file() file)
 
void request_reopen () noexcept
 Log rotation: the sink drains what was logged so far into the file it has open, then opens the path again, so a file renamed away (logrotate without copytruncate) is closed and the rest goes to a new one.
 
bool reopen ()
 
void flush ()
 Drains all rings and fflush()es. Works with or without a running sink thread.
 
bool running () const noexcept
 
bool to_file () const noexcept
 The sink writes to a stream other than stderr (a log file).
 
std::uint64_t dropped () const noexcept
 
void note_drop () noexcept
 
std::uint64_t written () const noexcept
 

Static Public Member Functions

static Logger & instance () noexcept
 
static void format_record (const LogRecord &r, std::string &out)
 Formats one record into out (appends). Public for tests/tools.
 

Detailed Description

Process-wide registry of per-thread rings + the sink thread. Implemented in log_sink.cpp.

Member Function Documentation

◆ instance()

static Logger & fastmm::Logger::instance ( )
staticnoexcept

◆ set_level()

void fastmm::Logger::set_level ( LogLevel  lvl)
inlinenoexcept

◆ level()

LogLevel fastmm::Logger::level ( ) const
inlinenoexcept

◆ attach_current_thread()

LogRing * fastmm::Logger::attach_current_thread ( )

Gives the calling thread a ring: a drained ring released by an exited thread if one exists, otherwise a new one (heap).

Call during warm-up on every thread that logs; it is otherwise done lazily on the first log statement. Returns nullptr once kLogMaxThreads rings are in use at the same time.

◆ detach_current_thread()

void fastmm::Logger::detach_current_thread ( )
noexcept

Returns the calling thread's ring to the pool.

Runs automatically when a thread that logged exits, so short-lived threads (sweep workers, Python callers) never exhaust the slots.

◆ thread_slots()

std::size_t fastmm::Logger::thread_slots ( ) const
noexcept

Number of rings allocated so far; reused rings are not counted again.

◆ start()

void fastmm::Logger::start ( std::FILE *  out = stderr,
LogLevel  mirror_level = LogLevel::Warn 
)

Sink control.

out defaults to stderr. Records at >= mirror_level are also copied to stderr when out is a different stream.

◆ start_file()

bool fastmm::Logger::start_file ( const std::string &  path,
LogLevel  mirror_level = LogLevel::Warn 
)

Opens path for appending and starts the sink on it.

The logger owns this file: stop() closes it, and reopen() can swap it for a new one at the same path. False, and nothing started, when the file cannot be opened.

◆ stop()

void fastmm::Logger::stop ( )

drains everything then joins the sink thread (and closes a start_file() file)

◆ request_reopen()

void fastmm::Logger::request_reopen ( )
noexcept

Log rotation: the sink drains what was logged so far into the file it has open, then opens the path again, so a file renamed away (logrotate without copytruncate) is closed and the rest goes to a new one.

request_reopen() only asks, and is async-signal-safe; reopen() waits for the sink. False: no start_file() file, or the path cannot be opened (the old file stays in use).

◆ reopen()

bool fastmm::Logger::reopen ( )

◆ flush()

void fastmm::Logger::flush ( )

Drains all rings and fflush()es. Works with or without a running sink thread.

◆ running()

bool fastmm::Logger::running ( ) const
inlinenoexcept

◆ to_file()

bool fastmm::Logger::to_file ( ) const
inlinenoexcept

The sink writes to a stream other than stderr (a log file).

◆ dropped()

std::uint64_t fastmm::Logger::dropped ( ) const
inlinenoexcept

◆ note_drop()

void fastmm::Logger::note_drop ( )
inlinenoexcept

◆ written()

std::uint64_t fastmm::Logger::written ( ) const
inlinenoexcept

◆ format_record()

static void fastmm::Logger::format_record ( const LogRecord &  r,
std::string &  out 
)
static

Formats one record into out (appends). Public for tests/tools.