Fixed bug with updates. Finished logging.
[yandex-traffic] / log.cpp
diff --git a/log.cpp b/log.cpp
new file mode 100644 (file)
index 0000000..a0b52cc
--- /dev/null
+++ b/log.cpp
@@ -0,0 +1,68 @@
+#include <QtCore>
+
+#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 ();
+}