#include "common.hh" #include "logger_base.hh" #include #include namespace { class LoggerFile : public LoggerBase { public: explicit LoggerFile(std::filesystem::path const& path) : out_(path, std::ios::out | std::ios::app) {} bool good() const { return out_.good(); } protected: void msg(Level lvl, std::string_view msg) override { std::lock_guard lock(mutex_); switch (lvl) { case Level::ERR: out_ << "Error: " << msg << std::endl; break; case Level::WARN: out_ << "Warning: " << msg << std::endl; break; case Level::INFO: out_ << msg << std::endl; break; case Level::DBG: out_ << "Debug: " << msg << std::endl; break; } } private: std::mutex mutex_; std::fstream out_; }; } // namespace std::unique_ptr Logger::create_file(std::filesystem::path const& path, Logger* fallback) { auto logger = std::make_unique(path); if (logger->good()) return logger; fallback->warn("Unable to open %s for appending.", path.c_str()); return nullptr; }