From bec1f3b8ea934fbf047421dcbf548fcc8ebac5cf Mon Sep 17 00:00:00 2001 From: Max Lapan Date: Thu, 18 Mar 2010 12:08:00 +0300 Subject: [PATCH] Fixed bug with updates. Finished logging. --- log.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++ log.hpp | 23 ++++++++++++++++ mainwidget.cpp | 28 +++++++++++++++---- yandex-traffic-core.pri | 4 +-- 4 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 log.cpp create mode 100644 log.hpp diff --git a/log.cpp b/log.cpp new file mode 100644 index 0000000..a0b52cc --- /dev/null +++ b/log.cpp @@ -0,0 +1,68 @@ +#include + +#include "log.hpp" + + +// -------------------------------------------------- +// Log +// -------------------------------------------------- +static Log *_log; + + +Log* Log::instance () +{ + if (!_log) + _log = new Log; + + return _log; +} + + +Log::Log () + : f (0) +{ + enable (); +} + + +void Log::initFile () +{ + f = new QFile ("/tmp/yandex-traffic-widget.log"); + + if (!f->open (QIODevice::Text | QIODevice::Truncate | QIODevice::WriteOnly)) { + delete f; + f = NULL; + } +} + + +void Log::enable () +{ + if (f) + return; + + initFile (); +} + + +void Log::disable () +{ + f->close (); + delete f; + f = NULL; +} + + +void Log::add (const QString &line) +{ + if (!f) + return; + + // Build timestamp + QString ts = QDateTime::currentDateTime ().toString ("dd:MM:yy hh.mm.ss.zzz: "); + + f->write (ts.toUtf8 ()); + f->write (line.toUtf8 ()); + f->write ("\n"); + f->flush (); +} diff --git a/log.hpp b/log.hpp new file mode 100644 index 0000000..d9f6b97 --- /dev/null +++ b/log.hpp @@ -0,0 +1,23 @@ +#ifndef __LOG_H__ +#define __LOG_H__ + + +class Log +{ +private: + QFile *f; + + Log (); + void initFile (); + +public: + static Log* instance (); + + void add (const QString &line); + + void disable (); + void enable (); +}; + + +#endif // __LOG_H__ diff --git a/mainwidget.cpp b/mainwidget.cpp index f4aa592..7e9a455 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -5,6 +5,7 @@ #include "connection.hpp" #include "devstate.hpp" #include "settings.hpp" +#include "log.hpp" // -------------------------------------------------- @@ -64,7 +65,6 @@ void MainWidget::paintEvent(QPaintEvent *event) } - void MainWidget::trafficUpdated () { ExtendedTrafficInfo info = _traffic->lookup_ext (_settings->regionID ()); @@ -105,15 +105,24 @@ void MainWidget::updateData () { bool update = true; + Log::instance ()->add ("updateData called"); + #if CHECK_FOR_CONNECTION update = ConnectionChecker::instance ()->checkConnection (_settings->check (Settings::C_UpdateOnGSM), _settings->check (Settings::C_UpdateOnWiFi)); - if (!_settings->check (Settings::C_UpdateWhenLocked)) + Log::instance ()->add (QString ("checkConnection returned %1").arg (update ? "true" : "false")); + if (!_settings->check (Settings::C_UpdateWhenLocked)) { + Log::instance ()->add ("Check for device state"); update &= !DeviceState::instance ()->locked (); + } #endif - if (update) + if (update) { + Log::instance ()->add ("Perform update"); _traffic->update (); + } + else + Log::instance ()->add ("Update not performed"); } @@ -154,10 +163,17 @@ void MainWidget::applySettings () updateSize (); - if (_settings->updateInterval () < 0) + Log::instance ()->add (QString ("applySettings: updateInterval is %1").arg (_settings->updateInterval ())); + + if (_settings->updateInterval () < 0) { _timer->stop (); - else + Log::instance ()->add ("Timer disabled"); + } + else { _timer->setInterval (1000 * 60 * _settings->updateInterval ()); + _timer->start (); + Log::instance ()->add (QString ("Timer interval set to %1 ms").arg (1000 * 60 * _settings->updateInterval ())); + } } @@ -166,6 +182,8 @@ void MainWidget::mousePressEvent (QMouseEvent *event) QMenu menu; QAction *settingsAction, *updateAction, *todo; + Log::instance ()->add (QString ("mousePressEvent at %1,%2").arg (event->pos ().x ()).arg (event->pos ().y ())); + settingsAction = menu.addAction (tr ("Settings")); updateAction = menu.addAction (tr ("Update")); diff --git a/yandex-traffic-core.pri b/yandex-traffic-core.pri index 279f265..663a514 100644 --- a/yandex-traffic-core.pri +++ b/yandex-traffic-core.pri @@ -1,5 +1,5 @@ -HEADERS += $$PWD/regions.hpp $$PWD/settings.hpp $$PWD/traffic.hpp $$PWD/http_fetcher.hpp $$PWD/connection.hpp $$PWD/devstate.hpp -SOURCES += $$PWD/regions.cpp $$PWD/settings.cpp $$PWD/traffic.cpp $$PWD/http_fetcher.cpp $$PWD/connection.cpp $$PWD/devstate.cpp +HEADERS += $$PWD/regions.hpp $$PWD/settings.hpp $$PWD/traffic.hpp $$PWD/http_fetcher.hpp $$PWD/connection.hpp $$PWD/devstate.hpp $$PWD/log.hpp +SOURCES += $$PWD/regions.cpp $$PWD/settings.cpp $$PWD/traffic.cpp $$PWD/http_fetcher.cpp $$PWD/connection.cpp $$PWD/devstate.cpp $$PWD/log.cpp HEADERS += $$PWD/icd2_light.h $$PWD/globals.hpp -- 1.7.9.5