Design an Asynchronous C++ Logger Using a Lock-Free Queue
Context
You are asked to design and implement an asynchronous logger suitable for a technical screen in a systems/engineering interview. The logger must:
-
Accept variadic arguments using fmt-style format strings.
-
Use a background thread to perform formatting and writing.
-
Pass log events through a provided lock-free queue.
-
Not optimize for heap usage or memory footprint (allocations are acceptable).
Assume a generic lock-free MPMC queue is provided with a minimal try_enqueue/try_dequeue API. You should:
-
Specify the public API.
-
Define the log event structure.
-
Describe the producer–consumer flow.
-
Demonstrate shutdown/flush behavior.
-
Describe error handling and overflow policy.
Requirements
-
Public API that supports:
-
Variadic, fmt-style logging (compile-time format checking preferred).
-
Flush and shutdown semantics.
-
Configurable sink (e.g., file or stderr) and error handling hooks.
-
Log events must be enqueued by producers and formatted/written by a background consumer thread.
-
Messages should be passed as events through the lock-free queue; formatting happens only on the consumer side.
-
Show robust shutdown/flush that drains the queue and avoids message loss.
-
Explain error handling (enqueue overflow, formatting errors, I/O failures).