Some bugs fixed.
[gpssportsniffer] / log.cpp
1 /****************************************************************************
2 **
3 **  Copyright (C) 2011  Tito Eritja Real <jtitoo@gmail.com>
4 **
5 **  This program is free software: you can redistribute it and/or modify
6 **  it under the terms of the GNU General Public License as published by
7 **  the Free Software Foundation, either version 3 of the License, or
8 **  (at your option) any later version.
9 **
10 **  This program is distributed in the hope that it will be useful,
11 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 **  GNU General Public License for more details.
14 **
15 **  You should have received a copy of the GNU General Public License
16 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 **
18 ****************************************************************************/
19
20 #include "log.h"
21 #include <QDate>
22 #include <QMessageBox>
23 #include <QFile>
24 #include <QDebug>
25
26 Log::Log(QString fileText,int verbosity)
27     :verbosity(verbosity)
28 {
29     if(verbosity>NOLOG_VERBOSITY){
30         QString name = QString(APPLICATION_PATH);
31         name.append(QDate::currentDate().toString(DATE_FORMAT));
32         file = new QFile(name.append("_" + fileText));
33
34         if (!file->open(QIODevice::WriteOnly | QIODevice::Text)){
35             qDebug() << "Error: Can not write Log file!";
36             return;
37         }
38         QTextStream out(file);
39         out << "start logging\n" << file << "out=" << &out << ", verbosity:" << verbosity << "(-1 no log, 0->debug, 1->info, 2->warn, 3->error)\n";
40
41     }
42 }
43
44 Log::Log(QWidget *win, QString fileText,int verbosity)
45     :verbosity(verbosity)
46 {
47     if(verbosity>NOLOG_VERBOSITY){
48         QString name = QString(APPLICATION_PATH);
49         name.append(QDate::currentDate().toString(DATE_FORMAT));
50
51         file = new QFile(name.append("_" + fileText));
52         if (!file->open(QIODevice::WriteOnly | QIODevice::Text)){
53             QMessageBox::information(win, tr("GPSSniffer"),tr("Error: Can not write Log file!"));
54
55             return;
56         }
57         QTextStream out(file);
58         out << "start logging\n"<< file << "out=" << &out << ", verbosity:" << verbosity << "(0->debug, 1->info, 2->warn, 3->error)\n";
59     }
60
61 }
62 Log::~Log(){
63     if(file)
64         file->close();
65     delete file;
66 }
67
68
69 void Log::setVerbosity(int num){
70     verbosity=num;
71 }
72
73 int Log::getVerbosity(){
74     return verbosity;
75 }
76
77 void Log::debug(QString msg){
78     if(verbosity>NOLOG_VERBOSITY){
79         QTextStream out(file);
80         if(&out!=0){
81             if(verbosity==0){
82                 out << QDateTime::currentDateTime().toString(CLEAN_DATE_FORMAT)<< " [DEBUG] " << msg << "\n";
83             }
84         }
85     }
86 }
87
88 void Log::info(QString msg){
89     if(verbosity>NOLOG_VERBOSITY){
90         QTextStream out(file);
91         if(&out!=0){
92             if(verbosity<=1){
93                 out << QDateTime::currentDateTime().toString(CLEAN_DATE_FORMAT)<< " [INFO] " << msg << "\n";
94             }
95         }
96     }
97 }
98
99 void Log::warn(QString msg){
100     if(verbosity>NOLOG_VERBOSITY){
101         QTextStream out(file);
102         if(&out!=0){
103             if(verbosity<=2){
104                 out << QDateTime::currentDateTime().toString(CLEAN_DATE_FORMAT)<< " [WARN] " << msg << "\n";
105             }
106         }
107     }
108 }
109
110 void Log::err(QString msg){
111     if(verbosity>NOLOG_VERBOSITY){
112         QTextStream out(file);
113         if(&out!=0){
114             if(verbosity<=3){
115                 out << QDateTime::currentDateTime().toString(CLEAN_DATE_FORMAT)<< " [ERROR] " << msg << "\n";
116             }
117         }
118     }
119 }
120
121
122
123