Skip to content

Commit

Permalink
Improve log to debug restore/dump application state
Browse files Browse the repository at this point in the history
Refs: #75
  • Loading branch information
orontee committed Nov 16, 2023
1 parent fad0d10 commit f865f25
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
13 changes: 12 additions & 1 deletion src/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "experimental/optional"
#include "inkview.h"
#include "model.h"
#include "state.h"
#include "units.h"
#include "util.h"

Expand Down Expand Up @@ -86,13 +87,21 @@ void App::setup() {

this->ui = std::make_unique<Ui>(this->model);

this->application_state = std::make_unique<ApplicationState>(this->model);
this->application_state->restore();

auto &current_location = this->model->location;
if (current_location.name.empty()) {
BOOST_LOG_TRIVIAL(debug) << "Setting current location to default value "
<< "since not restored from application state";
current_location.longitude = 2.3200410217200766;
current_location.latitude = 48.858889699999999;
current_location.name = "Paris";
current_location.country = "FR";
current_location.state = "Ile-de-France";
} else {
BOOST_LOG_TRIVIAL(debug)
<< "Current location restored from application state";
}
this->load_config();
}
Expand All @@ -107,9 +116,11 @@ void App::show() {
void App::exit() {
BOOST_LOG_TRIVIAL(info) << "Exiting application";

this->application_state->dump();

this->ui.reset();
this->history.reset();
this->application_state.reset();
this->history.reset();
this->service.reset();
this->model.reset();

Expand Down
3 changes: 1 addition & 2 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class App {
App()
: model{std::make_shared<Model>()}, l10n{std::make_unique<L10n>()},
service{std::make_unique<Service>()},
application_state{std::make_unique<ApplicationState>(this->model)},
history{std::make_unique<LocationHistoryProxy>(this->model)} {}

int process_event(int event_type, int param_one, int param_two);
Expand All @@ -46,8 +45,8 @@ class App {
std::shared_ptr<Model> model;
std::unique_ptr<L10n> l10n;
std::unique_ptr<Service> service;
std::unique_ptr<ApplicationState> application_state;
std::unique_ptr<LocationHistoryProxy> history;
std::unique_ptr<ApplicationState> application_state;
std::unique_ptr<Ui> ui;

bool config_already_loaded{false};
Expand Down
1 change: 1 addition & 0 deletions src/locationbox.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "locationbox.h"

#include <array>
#include <boost/log/trivial.hpp>
#include <cmath>
#include <cstring>
#include <inkview.h>
Expand Down
6 changes: 0 additions & 6 deletions src/service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ Json::Value Service::send_get_request(const std::string &url) {
}

Condition Service::extract_condition(const Json::Value &value) {
BOOST_LOG_TRIVIAL(debug) << "Extracting weather condition from JSON value";

const TimePoint date{value.get("dt", 0).asInt64() * 1s};
const TimePoint sunrise{value.get("sunrise", 0).asInt64() * 1s};
const TimePoint sunset{value.get("sunset", 0).asInt64() * 1s};
Expand Down Expand Up @@ -233,8 +231,6 @@ Condition Service::extract_condition(const Json::Value &value) {
}

DailyCondition Service::extract_daily_condition(const Json::Value &value) {
BOOST_LOG_TRIVIAL(debug) << "Extracting daily condition from JSON value";

const TimePoint date{value.get("dt", 0).asInt64() * 1s};
const TimePoint sunrise{value.get("sunrise", 0).asInt64() * 1s};
const TimePoint sunset{value.get("sunset", 0).asInt64() * 1s};
Expand Down Expand Up @@ -296,8 +292,6 @@ DailyCondition Service::extract_daily_condition(const Json::Value &value) {
}

std::vector<Alert> Service::extract_alerts(const Json::Value &value) {
BOOST_LOG_TRIVIAL(debug) << "Extracting alerts from JSON value";

std::vector<Alert> alerts;

for (auto &alert_value : value) {
Expand Down
4 changes: 4 additions & 0 deletions src/state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ std::string ApplicationState::get_application_state_path() {
void ApplicationState::restore_model(const Json::Value &root) {
const auto &model_value = root[MODEL_KEY];
update_from_json(*this->model, model_value);

BOOST_LOG_TRIVIAL(info) << "Model restored from application state";
}

void ApplicationState::dump_model(Json::Value &root) {
auto &model_value = root[MODEL_KEY];
model_value = to_json(*this->model);

BOOST_LOG_TRIVIAL(info) << "Model dumped to application state";
}

} // namespace taranis
6 changes: 2 additions & 4 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ namespace taranis {
class ApplicationState {

public:
ApplicationState(std::shared_ptr<Model> model) : model{model} {
this->restore();
}
ApplicationState(std::shared_ptr<Model> model) : model{model} {}

~ApplicationState() { this->dump(); }
~ApplicationState() {}

void restore();

Expand Down

0 comments on commit f865f25

Please sign in to comment.