diff options
| author | Star Rauchenberger <fefferburbia@gmail.com> | 2025-03-09 13:07:35 -0400 |
|---|---|---|
| committer | Star Rauchenberger <fefferburbia@gmail.com> | 2025-03-09 13:07:35 -0400 |
| commit | d3457700075fab2dac25bcff2775b7ae5a436a28 (patch) | |
| tree | cfa18a1875e171e53234b2487fd9ba25b31146fa /src/logger.cpp | |
| parent | 7f907a8985b1e2193e210ecc7e18f60c7eea2094 (diff) | |
| download | lingo-ap-tracker-d3457700075fab2dac25bcff2775b7ae5a436a28.tar.gz lingo-ap-tracker-d3457700075fab2dac25bcff2775b7ae5a436a28.tar.bz2 lingo-ap-tracker-d3457700075fab2dac25bcff2775b7ae5a436a28.zip | |
Created log window
Diffstat (limited to 'src/logger.cpp')
| -rw-r--r-- | src/logger.cpp | 42 |
1 files changed, 37 insertions, 5 deletions
| diff --git a/src/logger.cpp b/src/logger.cpp index 09fc331..8a08b58 100644 --- a/src/logger.cpp +++ b/src/logger.cpp | |||
| @@ -3,8 +3,10 @@ | |||
| 3 | #include <chrono> | 3 | #include <chrono> |
| 4 | #include <fstream> | 4 | #include <fstream> |
| 5 | #include <mutex> | 5 | #include <mutex> |
| 6 | #include <sstream> | ||
| 6 | 7 | ||
| 7 | #include "global.h" | 8 | #include "global.h" |
| 9 | #include "log_dialog.h" | ||
| 8 | 10 | ||
| 9 | namespace { | 11 | namespace { |
| 10 | 12 | ||
| @@ -14,19 +16,49 @@ class Logger { | |||
| 14 | 16 | ||
| 15 | void LogLine(const std::string& text) { | 17 | void LogLine(const std::string& text) { |
| 16 | std::lock_guard guard(file_mutex_); | 18 | std::lock_guard guard(file_mutex_); |
| 17 | logfile_ << "[" << std::chrono::system_clock::now() << "] " << text | 19 | std::ostringstream line; |
| 18 | << std::endl; | 20 | line << "[" << std::chrono::system_clock::now() << "] " << text; |
| 21 | |||
| 22 | logfile_ << line.str() << std::endl; | ||
| 19 | logfile_.flush(); | 23 | logfile_.flush(); |
| 24 | |||
| 25 | if (log_dialog_ != nullptr) { | ||
| 26 | log_dialog_->LogMessage(line.str()); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | std::string GetContents() { | ||
| 31 | std::lock_guard guard(file_mutex_); | ||
| 32 | |||
| 33 | std::ifstream file_in(GetAbsolutePath("debug.log")); | ||
| 34 | std::ostringstream buffer; | ||
| 35 | buffer << file_in.rdbuf(); | ||
| 36 | |||
| 37 | return buffer.str(); | ||
| 38 | } | ||
| 39 | |||
| 40 | void SetLogDialog(LogDialog* log_dialog) { | ||
| 41 | std::lock_guard guard(file_mutex_); | ||
| 42 | log_dialog_ = log_dialog; | ||
| 20 | } | 43 | } |
| 21 | 44 | ||
| 22 | private: | 45 | private: |
| 23 | std::ofstream logfile_; | 46 | std::ofstream logfile_; |
| 24 | std::mutex file_mutex_; | 47 | std::mutex file_mutex_; |
| 48 | LogDialog* log_dialog_ = nullptr; | ||
| 25 | }; | 49 | }; |
| 26 | 50 | ||
| 51 | Logger& GetLogger() { | ||
| 52 | static Logger* instance = new Logger(); | ||
| 53 | return *instance; | ||
| 54 | } | ||
| 55 | |||
| 27 | } // namespace | 56 | } // namespace |
| 28 | 57 | ||
| 29 | void TrackerLog(std::string text) { | 58 | void TrackerLog(std::string text) { GetLogger().LogLine(text); } |
| 30 | static Logger* instance = new Logger(); | 59 | |
| 31 | instance->LogLine(text); | 60 | std::string TrackerReadPastLog() { return GetLogger().GetContents(); } |
| 61 | |||
| 62 | void TrackerSetLogDialog(LogDialog* log_dialog) { | ||
| 63 | GetLogger().SetLogDialog(log_dialog); | ||
| 32 | } | 64 | } |
