SIGSEGV Fixed
[vicar] / src / vicar-lib / cpp / logutility.h
1 /*
2 @version: 0.4
3 @author: Sudheer K. <scifi1947 at gmail.com>
4 @license: GNU General Public License
5 */
6
7 #ifndef LOGUTILITY_H
8 #define LOGUTILITY_H
9 #include <QDebug>
10 #include <QFile>
11 #include <QIODevice>
12 #include <QDateTime>
13 #include <QDir>
14
15 class LogUtility : public QObject
16 {
17     Q_OBJECT
18
19 private:
20     QFile * logFile;
21
22 public:
23     LogUtility(QObject *parent = 0) :
24         QObject(parent){
25         QString strPath;
26
27 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
28         //For maemo fremantle or harmattan use a common path                
29         QDir logDir = QDir(QDir().homePath() + "/.vicar");
30         if (!logDir.exists()){
31             if (QDir().mkpath(logDir.absolutePath())){
32                 qDebug() << "Vicar: Log directory created successfully";
33             }
34             else{
35                 qDebug() << "Vicar: Error creating log directory";
36             }
37         }
38         strPath = logDir.absolutePath() + "/vicar.log";
39 #else
40         strPath = "vicar.log";
41 #endif
42         logFile = new QFile(strPath,this);
43
44         if (!logFile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
45             qDebug() << "Vicar: Error opening logfile for writing at path " << strPath;
46         }
47     }
48
49     ~LogUtility(){
50         if (logFile->isOpen())
51             logFile->close();
52         qDebug() << "Vicar: In LogUtility object destructor..";
53     }
54
55 public slots:
56     void logMessage(QString strMessage) {
57
58         QString strTimeNow = QDateTime::currentDateTime().toString("dd-MMM-yyyy HH:mm:ss");
59
60         if (logFile->isOpen() && logFile->isWritable()) {
61             QTextStream logStream(logFile);
62             logStream <<  QString("[%1] - %2").arg(strTimeNow,strMessage) << endl;
63             qDebug() << QString("Vicar: [%1] - %2").arg(strTimeNow,strMessage);
64         }
65         else{
66             qDebug() <<  "ERROROPENINGLOGFILE" << QString("Vicar: [%1] - %2").arg(strTimeNow,strMessage);
67         }
68     }
69 };
70
71 #endif // LOGUTILITY_H