Macro-Free Mode - Quill v11.0.2 (original) (raw)

The library provides a macro-free mode that allows logging using functions instead of macros. While the macro mode remains the main and recommended approach for logging, the macro-free mode offers an alternative with cleaner code in some scenarios, at the cost of additional overhead.

The macro-free mode is implemented using compiler built-ins (__builtin_FILE(), __builtin_FUNCTION(), __builtin_LINE()) which may vary by compiler.

Performance Trade-offs

The macro-free approach comes with performance implications:

  1. Higher Latency: Format metadata isn’t available at compile time, requiring additional runtime copying of metadata to the backend thread
  2. Always-Evaluated Arguments: Unlike macros which skip evaluation for disabled log levels, arguments to these functions are always evaluated
  3. No Compile-Time Removal: Cannot be completely compiled out with QUILL_COMPILE_ACTIVE_LOG_LEVEL_<LEVEL> as macros can
  4. Backend Thread Impact: Reduced throughput in the backend due to runtime metadata storage and processing

For performance-critical logging paths, the macro-based logging interface is recommended.

Available Functions

The following logging functions are available in the macro-free mode:

Each function also accepts a quill::Tags object as an optional parameter after the logger.

Usage

1#include "quill/Backend.h" 2#include "quill/Frontend.h" 3#include "quill/LogFunctions.h" 4#include "quill/Logger.h" 5#include "quill/sinks/ConsoleSink.h" 6 7#include 8#include 9 10/** 11 * Trivial logging example to console 12 * Note: You can also pass STL types by including the relevant header files from quill/std/ 13 / 14 15int main() 16{ 17 quill::BackendOptions backend_options; 18 quill::Backend::start(backend_options); 19 20 // Frontend 21 auto console_sink = quill::Frontend::create_or_get_sinkquill::ConsoleSink("sink_id_1"); 22 23 // [ %(tags)] is for demonstration purposes only, you might remove it if you don't want it 24 quill::Logger logger = quill::Frontend::create_or_get_logger( 25 "root", std::move(console_sink), 26 quill::PatternFormatterOptions{"%(time) [%(thread_id)] %(short_source_location:<28) " 27 "LOG_%(log_level:<9) %(logger:<12) [ %(tags)] %(message)"}); 28 29 // Change the LogLevel to print everything 30 logger->set_log_level(quill::LogLevel::TraceL3); 31 32 int a = 123; 33 std::string l = "log"; 34 35 quill::tracel3(logger, "A {} message with number {}", l, a); 36 quill::tracel3(logger, quill::Tags{"TAG"}, "A {} message with number {}", l, a); 37 38 quill::tracel2(logger, "A {} message with number {}", l, a); 39 quill::tracel2(logger, quill::Tags{"TAG"}, "A {} message with number {}", l, a); 40 41 quill::tracel1(logger, "A {} message with number {}", l, a); 42 quill::tracel1(logger, quill::Tags{"TAG"}, "A {} message with number {}", l, a); 43 44 quill::debug(logger, "A {} message with number {}", l, a); 45 quill::debug(logger, quill::Tags{"TAG"}, "A {} message with number {}", l, a); 46 47 quill::info(logger, "A {} message with number {}", l, a); 48 quill::info(logger, quill::Tags{"TAG"}, "A {} message with number {}", l, a); 49 50 quill::notice(logger, "A {} message with number {}", l, a); 51 quill::notice(logger, quill::Tags{"TAG"}, "A {} message with number {}", l, a); 52 53 quill::warning(logger, "A {} message with number {}", l, a); 54 quill::warning(logger, quill::Tags{"TAG"}, "A {} message with number {}", l, a); 55 56 quill::error(logger, "A {} message with number {}", l, a); 57 quill::error(logger, quill::Tags{"TAG"}, "A {} message with number {}", l, a); 58 59 quill::critical(logger, "A {} message with number {}", l, a); 60 quill::critical(logger, quill::Tags{"TAG"}, "A {} message with number {}", l, a); 61}