Thread-Safe Singleton Logger

Medium C++ Concurrency Asked at Cisco
Solve this problem

Implement a logger singleton that is safe to initialise from multiple threads and cannot be destroyed while another thread is still writing.

Constraints

  • Use the Meyers singleton idiom for initialisation.
  • Writes must be serialised with a mutex.
  • Copy and move deleted.
  • No manual double-checked locking.
  • Must not allocate on every log call.
  • Log level filtering happens before taking the lock.
Starter codeC++
1#include <mutex>2#include <string>3 4class Logger {5public:6    enum Level { kDebug, kInfo, kError };7    static Logger& instance();8    void log(Level level, const char* message);9    void set_level(Level level);10};