From 12d2080865958cc07d37dddd28240f40423fb292 Mon Sep 17 00:00:00 2001 From: DaMichel Date: Fri, 29 Jul 2016 16:32:54 +0200 Subject: logger: it is definitely nicer to not have things all over the place --- opentrack-logic/state.hpp | 2 -- opentrack-logic/tracklogger.hpp | 7 +------ opentrack-logic/work.cpp | 45 +++++++++++++++++++++++++++++++++++++++-- opentrack-logic/work.hpp | 10 ++++++--- 4 files changed, 51 insertions(+), 13 deletions(-) (limited to 'opentrack-logic') diff --git a/opentrack-logic/state.hpp b/opentrack-logic/state.hpp index bdab9afe..1c608f7a 100644 --- a/opentrack-logic/state.hpp +++ b/opentrack-logic/state.hpp @@ -14,7 +14,6 @@ using namespace options; #include "main-settings.hpp" #include "mappings.hpp" #include "selected-libraries.hpp" -#include "tracklogger.hpp" #include "work.hpp" #include #include @@ -30,5 +29,4 @@ struct State main_settings s; Mappings pose; mem work; - mem logger; }; diff --git a/opentrack-logic/tracklogger.hpp b/opentrack-logic/tracklogger.hpp index 99bc71b0..65128d48 100644 --- a/opentrack-logic/tracklogger.hpp +++ b/opentrack-logic/tracklogger.hpp @@ -14,8 +14,6 @@ public: { } - static mem make() { return std::make_shared(); } - virtual void write(const char *) { } @@ -45,12 +43,9 @@ public: first_col(true) { out.open(filename.toStdString()); - if (!out.is_open()) - throw std::ios_base::failure("unable to open file"); } - static mem make(const main_settings &s) { return std::static_pointer_cast(std::make_shared(s.tracklogging_filename)); } - + bool is_open() const { return out.is_open(); } virtual void write(const char *s); virtual void write(const double *p, int n); virtual void next_line(); diff --git a/opentrack-logic/work.cpp b/opentrack-logic/work.cpp index e5b08c18..8d00270b 100644 --- a/opentrack-logic/work.cpp +++ b/opentrack-logic/work.cpp @@ -1,9 +1,50 @@ #include "work.hpp" +#include -Work::Work(Mappings& m, SelectedLibraries& libs, TrackLogger &logger, WId handle) : + +std::shared_ptr Work::make_logger(const main_settings &s) +{ + if (s.tracklogging_enabled) + { + if (static_cast(s.tracklogging_filename).isEmpty()) + { + QMessageBox::warning(nullptr, "Logging Error", + "No filename given for track logging. Proceeding without logging.", + QMessageBox::Ok, + QMessageBox::NoButton); + } + else + { + auto logger = std::make_shared(s.tracklogging_filename); + if (!logger->is_open()) + { + logger = nullptr; + QMessageBox::warning(nullptr, "Logging Error", + "Unable to open file: " + s.tracklogging_filename + ". Proceeding without logging.", + QMessageBox::Ok, + QMessageBox::NoButton); + } + else + { + /* As this function has the potential to fill up the hard drive + of the unwary with junk data, a warning is in order. */ + QMessageBox::warning(nullptr, "Logging Active", + "Just a heads up. You are recoding pose data to " + s.tracklogging_filename + "!", + QMessageBox::Ok, + QMessageBox::NoButton); + return logger; + } + } + } + return std::make_shared(); +} + + +Work::Work(Mappings& m, SelectedLibraries& libs, WId handle) : libs(libs), - tracker(std::make_shared(m, libs, logger)), + logger(make_logger(s)), + tracker(std::make_shared(m, libs, *logger)), sc(std::make_shared()), handle(handle), keys { diff --git a/opentrack-logic/work.hpp b/opentrack-logic/work.hpp index 6425b11c..4afb1da4 100644 --- a/opentrack-logic/work.hpp +++ b/opentrack-logic/work.hpp @@ -13,6 +13,7 @@ #include "tracker.h" #include "shortcuts.h" #include "export.hpp" +#include "tracklogger.hpp" #include #include @@ -25,15 +26,18 @@ struct OPENTRACK_LOGIC_EXPORT Work { using fn_t = std::function; using key_tuple = std::tuple; - + main_settings s; // tracker needs settings, so settings must come before it SelectedLibraries& libs; + std::shared_ptr logger; // must come before tracker, since tracker depends on it std::shared_ptr tracker; std::shared_ptr sc; WId handle; std::vector keys; - main_settings s; - Work(Mappings& m, SelectedLibraries& libs, TrackLogger &logger, WId handle); + Work(Mappings& m, SelectedLibraries& libs, WId handle); ~Work(); void reload_shortcuts(); + +private: + std::shared_ptr make_logger(const main_settings &s); }; -- cgit v1.2.3