quill
Public Types | Public Member Functions | Static Public Attributes | Friends | List of all members
LoggerImpl< TFrontendOptions > Class Template Reference

Thread safe logger. More...

#include <Logger.h>

Inheritance diagram for LoggerImpl< TFrontendOptions >:
Inheritance graph
[legend]
Collaboration diagram for LoggerImpl< TFrontendOptions >:
Collaboration graph
[legend]

Public Types

using frontend_options_t = TFrontendOptions
 

Public Member Functions

 LoggerImpl (LoggerImpl const &)=delete
 
LoggerImploperator= (LoggerImpl const &)=delete
 
template<bool enable_immediate_flush, typename... Args>
QUILL_ATTRIBUTE_HOT bool log_statement (MacroMetadata const *macro_metadata, Args &&... fmt_args)
 Push a log message to the spsc queue to be logged by the backend thread. More...
 
template<bool enable_immediate_flush, typename... Args>
QUILL_ATTRIBUTE_HOT bool log_statement_runtime_metadata (MacroMetadata const *macro_metadata, char const *fmt, char const *file_path, char const *function_name, char const *tags, uint32_t line_number, LogLevel log_level, Args &&... fmt_args)
 Push a log message with runtime metadata to the spsc queue to be logged by the backend thread. More...
 
void init_backtrace (uint32_t max_capacity, LogLevel flush_level=LogLevel::None)
 Init a backtrace for this logger. More...
 
void flush_backtrace ()
 Dump any stored backtrace messages.
 
void flush_log (uint32_t sleep_duration_ns=100)
 Blocks the calling thread until all log messages up to the current timestamp are flushed. More...
 
- Public Member Functions inherited from detail::LoggerBase
 LoggerBase (std::string logger_name, std::vector< std::shared_ptr< Sink >> sinks, PatternFormatterOptions pattern_formatter_options, ClockSourceType clock_source, UserClockSource *user_clock)
 
 LoggerBase (LoggerBase const &)=delete
 
LoggerBaseoperator= (LoggerBase const &)=delete
 
QUILL_NODISCARD std::string const & get_logger_name () const noexcept
 Returns the name of the logger. More...
 
QUILL_NODISCARD UserClockSourceget_user_clock_source () const noexcept
 Returns the user-defined clock source. More...
 
QUILL_NODISCARD ClockSourceType get_clock_source_type () const noexcept
 Returns the type of clock source being used. More...
 
QUILL_NODISCARD PatternFormatterOptions const & get_pattern_formatter_options () const noexcept
 Returns the pattern formatter options. More...
 
QUILL_NODISCARD std::vector< std::shared_ptr< Sink > > const & get_sinks () const noexcept
 Returns a const reference to the sinks vector. More...
 
void mark_invalid ()
 This function sets the logger's validity flag to false, indicating that the logger is no longer valid.
 
QUILL_NODISCARD bool is_valid_logger () const noexcept
 Checks if the logger is valid. More...
 
QUILL_NODISCARD LogLevel get_log_level () const noexcept
 
void set_log_level (LogLevel new_log_level)
 Set the log level of the logger. More...
 
void set_immediate_flush (uint32_t flush_every_n_messages=1)
 Sets the flush interval for logging based on message count. More...
 
template<LogLevel log_statement_level>
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT bool should_log_statement () const noexcept
 Checks if the given log_statement_level can be logged by this logger. More...
 
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT bool should_log_statement (LogLevel log_statement_level) const noexcept
 Checks if the given log_statement_level can be logged by this logger. More...
 

Static Public Attributes

static constexpr bool using_unbounded_queue
 

Friends

class detail::LoggerManager
 
class detail::BackendWorker
 

Additional Inherited Members

- Protected Attributes inherited from detail::LoggerBase
std::string _logger_name
 
UserClockSource_user_clock {nullptr}
 
std::atomic< uint32_t > _message_flush_threshold {0}
 
std::atomic< uint32_t > _messages_since_last_flush {0}
 
ClockSourceType _clock_source
 
std::atomic< LogLevel > _log_level {LogLevel::Info}
 
std::atomic< LogLevel > _backtrace_flush_level {LogLevel::None}
 
std::atomic< bool > _valid {true}
 Updated by the frontend at any time, accessed by the backend.
 
PatternFormatterOptions _pattern_formatter_options
 
std::vector< std::shared_ptr< Sink > > _sinks
 
std::shared_ptr< PatternFormatter_pattern_formatter
 
std::shared_ptr< BacktraceStorage_backtrace_storage
 
- Static Protected Attributes inherited from detail::LoggerBase
static QUILL_THREAD_LOCAL ThreadContext_thread_context = nullptr
 

Detailed Description

template<typename TFrontendOptions>
class LoggerImpl< TFrontendOptions >

Thread safe logger.

Logger must be obtained from create_or_get_logger(), therefore, constructors are private

Member Function Documentation

◆ flush_log()

template<typename TFrontendOptions>
void LoggerImpl< TFrontendOptions >::flush_log ( uint32_t  sleep_duration_ns = 100)
inline

Blocks the calling thread until all log messages up to the current timestamp are flushed.

The backend thread will invoke the write operation on all sinks for all loggers up to the point (timestamp) when this function is invoked.

Parameters
sleep_duration_nsThe duration in nanoseconds to sleep between retries when the blocking queue is full, and between checks for the flush completion. Default is 100 nanoseconds.
Warning
Do not call this function from the destructor of a static object. This may lead to application crashes if the thread-local ThreadContext is destroyed before the static object invoking flush_log.
Note
This function should only be called when the backend worker is running after Backend::start(...)
This function will block the calling thread until the flush message is processed by the backend thread. The calling thread can block for up to backend_options.sleep_duration. If you configure a custom long sleep duration on the backend thread, e.g., backend_options.sleep_duration = std::chrono::minutes{1}, then you should ideally avoid calling this function as you can block for long period of times unless you use another thread that calls Backend::notify()

◆ init_backtrace()

template<typename TFrontendOptions>
void LoggerImpl< TFrontendOptions >::init_backtrace ( uint32_t  max_capacity,
LogLevel  flush_level = LogLevel::None 
)
inline

Init a backtrace for this logger.

Stores messages logged with LOG_BACKTRACE in a ring buffer and displays them later on demand.

Parameters
max_capacityThe max number of messages to store in the backtrace
flush_levelIf this logger logs any message higher or equal to this severity level, the backtrace will also get flushed. Default level is None meaning the user has to call flush_backtrace explicitly

◆ log_statement()

template<typename TFrontendOptions>
template<bool enable_immediate_flush, typename... Args>
QUILL_ATTRIBUTE_HOT bool LoggerImpl< TFrontendOptions >::log_statement ( MacroMetadata const *  macro_metadata,
Args &&...  fmt_args 
)
inline

Push a log message to the spsc queue to be logged by the backend thread.

One spsc queue per caller thread. This function is enabled only when all arguments are fundamental types. This is the fastest way possible to log

Note
This function is thread-safe.
Parameters
macro_metadatametadata of the log message
fmt_argsarguments
Returns
true if the message is written to the queue, false if it is dropped (when a dropping queue is used)

◆ log_statement_runtime_metadata()

template<typename TFrontendOptions>
template<bool enable_immediate_flush, typename... Args>
QUILL_ATTRIBUTE_HOT bool LoggerImpl< TFrontendOptions >::log_statement_runtime_metadata ( MacroMetadata const *  macro_metadata,
char const *  fmt,
char const *  file_path,
char const *  function_name,
char const *  tags,
uint32_t  line_number,
LogLevel  log_level,
Args &&...  fmt_args 
)
inline

Push a log message with runtime metadata to the spsc queue to be logged by the backend thread.

Similar to log_statement but allows passing metadata that is only available at runtime.

Note
This function is thread-safe.
Parameters
macro_metadataMetadata of the log message
fmtFormat string for the log message
file_pathSource file path where the log statement was called
function_nameFunction name where the log statement was called
tagsOptional tags associated with the log message
line_numberLine number in the source file
log_levelSeverity level of the log message
fmt_argsFormat arguments for the log message
Returns
true if the message is written to the queue, false if it is dropped (when a dropping queue is used)

Member Data Documentation

◆ using_unbounded_queue

template<typename TFrontendOptions>
constexpr bool LoggerImpl< TFrontendOptions >::using_unbounded_queue
static
Initial value:
=
(frontend_options_t::queue_type == QueueType::UnboundedBlocking) ||
(frontend_options_t::queue_type == QueueType::UnboundedDropping)

The documentation for this class was generated from the following file: