#include "logger.h" #include #include #include #include #include "global.h" #include "log_dialog.h" namespace { class Logger { public: Logger() : logfile_(GetAbsolutePath("debug.log")) {} void LogLine(const std::string& text) { std::lock_guard guard(file_mutex_); std::ostringstream line; line << "[" << std::chrono::system_clock::now() << "] " << text; logfile_ << line.str() << std::endl; logfile_.flush(); if (log_dialog_ != nullptr) { log_dialog_->LogMessage(line.str()); } } std::string GetContents() { std::lock_guard guard(file_mutex_); std::ifstream file_in(GetAbsolutePath("debug.log")); std::ostringstream buffer; buffer << file_in.rdbuf(); return buffer.str(); } void SetLogDialog(LogDialog* log_dialog) { std::lock_guard guard(file_mutex_); log_dialog_ = log_dialog; } private: std::ofstream logfile_; std::mutex file_mutex_; LogDialog* log_dialog_ = nullptr; }; Logger& GetLogger() { static Logger* instance = new Logger(); return *instance; } } // namespace void TrackerLog(std::string text) { GetLogger().LogLine(text); } std::string TrackerReadPastLog() { return GetLogger().GetContents(); } void TrackerSetLogDialog(LogDialog* log_dialog) { GetLogger().SetLogDialog(log_dialog); }