Fix typo
[yandex-traffic] / log.cpp
1 #include <QtCore>
2
3 #include "log.hpp"
4
5
6 // --------------------------------------------------
7 // Log
8 // --------------------------------------------------
9 static Log *_log;
10
11
12 Log* Log::instance ()
13 {
14     if (!_log)
15         _log = new Log;
16
17     return _log;
18 }
19
20
21 Log::Log ()
22     : f (0)
23 {
24     enable ();
25 }
26
27
28 void Log::initFile ()
29 {
30     f = new QFile ("/tmp/yandex-traffic-widget.log");
31
32     if (!f->open (QIODevice::Text | QIODevice::Truncate | QIODevice::WriteOnly)) {
33         delete f;
34         f = NULL;
35     }
36 }
37
38
39 void Log::enable ()
40 {
41     if (f)
42         return;
43
44     initFile ();
45 }
46
47
48 void Log::disable ()
49 {
50     f->close ();
51     delete f;
52     f = NULL;
53 }
54
55
56 void Log::add (const QString &line)
57 {
58     if (!f)
59         return;
60
61     // Build timestamp
62     QString ts = QDateTime::currentDateTime ().toString ("dd:MM:yy hh.mm.ss.zzz: ");
63
64     f->write (ts.toUtf8 ());
65     f->write (line.toUtf8 ());
66     f->write ("\n");
67     f->flush ();
68 }