Merge branch 'master' of ssh://drop.maemo.org/git/vicar
[vicar] / src / vicar-lib / cpp / logutility.h
1 /*
2 <<<<<<< HEAD
3 @version: 0.4
4 =======
5 @version: 0.6
6 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
7 @author: Sudheer K. <scifi1947 at gmail.com>
8 @license: GNU General Public License
9 */
10
11 #ifndef LOGUTILITY_H
12 #define LOGUTILITY_H
13 <<<<<<< HEAD
14 #include <QDebug>
15 #include <QFile>
16 #include <QIODevice>
17 #include <QDateTime>
18 #include <QDir>
19 =======
20 #include <QFile>
21 #include <QIODevice>
22 #include <QTextStream>
23 #include <QDateTime>
24 #include <QDebug>
25 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
26
27 class LogUtility : public QObject
28 {
29     Q_OBJECT
30
31 <<<<<<< HEAD
32 private:
33     QFile * logFile;
34
35 public:
36     LogUtility(QObject *parent = 0) :
37         QObject(parent){
38         QString strPath;
39
40 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
41         //For maemo fremantle or harmattan use a common path                
42         QDir logDir = QDir(QDir().homePath() + "/.vicar");
43         if (!logDir.exists()){
44             if (QDir().mkpath(logDir.absolutePath())){
45                 qDebug() << "Vicar: Log directory created successfully";
46             }
47             else{
48                 qDebug() << "Vicar: Error creating log directory";
49             }
50         }
51         strPath = logDir.absolutePath() + "/vicar.log";
52 #else
53         strPath = "vicar.log";
54 #endif
55         logFile = new QFile(strPath,this);
56
57         if (!logFile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
58             qDebug() << "Vicar: Error opening logfile for writing at path " << strPath;
59         }
60     }
61
62     ~LogUtility(){
63         if (logFile->isOpen())
64             logFile->close();
65         qDebug() << "Vicar: In LogUtility object destructor..";
66     }
67
68 public slots:
69     void logMessage(QString strMessage) {
70
71         QString strTimeNow = QDateTime::currentDateTime().toString("dd-MMM-yyyy HH:mm:ss");
72
73         if (logFile->isOpen() && logFile->isWritable()) {
74             QTextStream logStream(logFile);
75             logStream <<  QString("[%1] - %2").arg(strTimeNow,strMessage) << endl;
76             qDebug() << QString("Vicar: [%1] - %2").arg(strTimeNow,strMessage);
77         }
78         else{
79             qDebug() <<  "ERROROPENINGLOGFILE" << QString("Vicar: [%1] - %2").arg(strTimeNow,strMessage);
80         }
81     }
82 =======
83 public:
84     LogUtility(QString logPath,QObject *parent = 0) :
85         QObject(parent){
86         logFilePath = logPath;
87     }
88
89     ~LogUtility(){
90     }
91
92 public slots:
93     void logMessage(QString strMessage, bool appendMode = true) {
94
95         QFile logFile(logFilePath);
96
97         bool success = false;
98
99         if (appendMode){
100             success = logFile.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text);
101         }
102         else{
103             success = logFile.open(QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Text);
104         }
105
106         QString strTimeNow = QDateTime::currentDateTime().toString("dd-MMM-yyyy HH:mm:ss");
107         if (success) {
108             QTextStream logStream(&logFile);
109             logStream <<  strTimeNow << " - " << strMessage << endl;
110         }
111         else{
112             qDebug() <<  "ERROROPENINGLOGFILE" << " - " << strMessage;
113         }
114     }
115
116 private:
117     QString logFilePath;
118 >>>>>>> 74800375ecf7f41e290cf7cc7fa9ee8b230be68e
119 };
120
121 #endif // LOGUTILITY_H