diff options
Diffstat (limited to 'yage/core/logsink.cpp')
-rw-r--r-- | yage/core/logsink.cpp | 134 |
1 files changed, 0 insertions, 134 deletions
diff --git a/yage/core/logsink.cpp b/yage/core/logsink.cpp deleted file mode 100644 index 1f026059..00000000 --- a/yage/core/logsink.cpp +++ /dev/null @@ -1,134 +0,0 @@ -/** --------------------------------------------------------------------------- - * @file: logsink.cpp - * - * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> - * MIT License, see LICENSE file for more details. - * ---------------------------------------------------------------------------- - */ - -#include "logsink.h" - -#include <chrono> -#include <ctime> -#include <fstream> -#include <iomanip> -#include <iostream> - -namespace yage -{ - -LogSink::LogSink(const LogSink &sink) : wrapper_(sink.wrapper_->clone()) {} - -LogSink::LogSink(LogSink &&sink) : wrapper_(std::move(sink.wrapper_)) {} - -LogSink &LogSink::operator=(const LogSink &sink) -{ - wrapper_.reset(sink.wrapper_->clone()); - return *this; -} - -LogSink &LogSink::operator=(LogSink &&sink) -{ - wrapper_ = std::move(sink.wrapper_); - return *this; -} - -bool LogSink::operator==(const LogSink &sink) -{ - return (wrapper_.get() == sink.wrapper_.get()); -} - -void LogSink::write(const LogMessage::Meta &meta, const std::string &msg) const -{ - wrapper_->write(meta, msg); -} - -LogSink makeConsoleSink() -{ - return [](const LogMessage::Meta &meta, const std::string &msg) { - std::cout << msg << "\n"; - }; -} - -namespace -{ - -class FileSink -{ -public: - FileSink(std::string &&filename) - : fileHandle_(std::make_shared<std::ofstream>(filename)) - { - if (!fileHandle_->good()) { - throw std::runtime_error("Could not open file: " + filename); - } - } - - FileSink(const std::string filename) - : fileHandle_(std::make_shared<std::ofstream>(filename)) - { - if (!fileHandle_->good()) { - throw std::runtime_error("Could not open file: " + filename); - } - } - - ~FileSink() = default; - - void operator()(const LogMessage::Meta &meta, const std::string &msg) const - { - using namespace std::chrono; - - auto now = system_clock::now(); - auto time_t = system_clock::to_time_t(now); - auto local_time = std::localtime(&time_t); - - std::string level; - - switch (meta.level) { - case LogLevel::DEBUG: - level = "DEBUG"; - break; - case LogLevel::INFO: - level = "INFO"; - break; - case LogLevel::WARNING: - level = "WARNING"; - break; - case LogLevel::ERROR: - level = "ERROR"; - break; - case LogLevel::FATAL: - level = "FATAL"; - break; - } - - (*fileHandle_) << std::put_time(local_time, "[%H:%M:%S] [") << level - << "] " << msg << "\n"; - if (meta.fileName != "") { - (*fileHandle_) << "(" << meta.fileName; - if (meta.line != -1) { - (*fileHandle_) << ":" << meta.line << ")"; - } else { - (*fileHandle_) << ")"; - } - } - (*fileHandle_) << "\n\n"; - } - -private: - std::shared_ptr<std::ofstream> fileHandle_; -}; - -} // namespace - -LogSink makeFileSink(const std::string &filename) -{ - return FileSink(filename); -} - -LogSink makeFileSink(std::string &&filename) -{ - return FileSink(filename); -} - -} // namespace yage |