summaryrefslogtreecommitdiff
path: root/src/logger_file.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
committerJoel Klinghed <the_jk@spawned.biz>2021-11-17 22:34:57 +0100
commit6232d13f5321b87ddf12a1aa36b4545da45f173d (patch)
tree23f3316470a14136debd9d02f9e920ca2b06f4cc /src/logger_file.cc
Travel3: Simple image and video display site
Reads the images and videos from filesystem and builds a site in memroy.
Diffstat (limited to 'src/logger_file.cc')
-rw-r--r--src/logger_file.cc52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/logger_file.cc b/src/logger_file.cc
new file mode 100644
index 0000000..ad11398
--- /dev/null
+++ b/src/logger_file.cc
@@ -0,0 +1,52 @@
+#include "common.hh"
+
+#include "logger_base.hh"
+
+#include <fstream>
+#include <mutex>
+
+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<std::mutex> 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> Logger::create_file(std::filesystem::path const& path,
+ Logger* fallback) {
+ auto logger = std::make_unique<LoggerFile>(path);
+ if (logger->good())
+ return logger;
+ fallback->warn("Unable to open %s for appending.", path.c_str());
+ return nullptr;
+}