quill
FuzzerHelper.h
1 #pragma once
2 
3 #include "quill/Backend.h"
4 #include "quill/Frontend.h"
5 #include "quill/Logger.h"
6 #include "quill/sinks/FileSink.h"
7 
8 #include <string>
9 
10 #ifndef FUZZER_LOG_FILENAME
11  #error "FUZZER_LOG_FILENAME must be defined before including FuzzerHelper.h"
12 #endif
13 
14 struct CustomFrontendOptions : quill::FrontendOptions
15 {
16  static constexpr size_t initial_queue_capacity = 4096; // start low for extra testing
17 };
18 
19 // Global logger instance
20 static quill::Logger* g_logger = nullptr;
21 static bool g_initialized = false;
22 
23 extern "C" int LLVMFuzzerInitialize(int* /*argc*/, char*** /*argv*/)
24 {
25  if (!g_initialized)
26  {
27  // Low the limits for additional testing
28  quill::BackendOptions backend_options;
29  backend_options.transit_event_buffer_initial_capacity = 2;
30 
31 #ifdef FUZZER_USE_BINARY_MODE
32  // Binary mode: disable printable char check
33  backend_options.check_printable_char = {};
34 #endif
35 
36  quill::Backend::start(backend_options);
37 
38 #ifdef FUZZER_USE_BINARY_MODE
39  // Binary pattern formatter - no suffix for raw binary data
40  quill::PatternFormatterOptions pattern_options;
41  pattern_options.format_pattern = "%(message)";
42  pattern_options.add_metadata_to_multi_line_logs = false;
43  pattern_options.pattern_suffix = quill::PatternFormatterOptions::NO_SUFFIX;
44 #endif
45 
46  auto file_sink = quill::Frontend::create_or_get_sink<quill::FileSink>(
47  FUZZER_LOG_FILENAME,
48  []()
49  {
50  quill::FileSinkConfig cfg;
51 #ifdef FUZZER_USE_BINARY_MODE
52  cfg.set_open_mode("wb"); // Binary mode
53 #else
54  cfg.set_open_mode('w'); // Text mode
55 #endif
56  cfg.set_filename_append_option(quill::FilenameAppendOption::None);
57  return cfg;
58  }(),
59  quill::FileEventNotifier{});
60 
61 #ifdef FUZZER_USE_BINARY_MODE
62  g_logger = quill::Frontend::create_or_get_logger("root", std::move(file_sink), pattern_options);
63 #else
64  g_logger = quill::Frontend::create_or_get_logger("root", std::move(file_sink));
65 #endif
66 
67  g_logger->set_log_level(quill::LogLevel::TraceL3);
68 
69  // Allow customization of flush limit via preprocessor
70  // Higher limits allow more queue stress but take longer to run
71 #ifndef FUZZER_IMMEDIATE_FLUSH_LIMIT
72  g_logger->set_immediate_flush(2048); // default: we need a limit otherwise the fuzzer produces too much input
73 #else
74  g_logger->set_immediate_flush(FUZZER_IMMEDIATE_FLUSH_LIMIT);
75 #endif
76 
77  g_initialized = true;
78  }
79  return 0;
80 }
This example demonstrates how to change the type of the Single Producer Single Consumer (SPSC) queue ...
Definition: bounded_dropping_queue_frontend.cpp:24