Version 0.7-0
[vicar] / src / vicar-lib / cpp / logutility.h
1 /*
2 @version: 0.6
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 <QFile>
10 #include <QIODevice>
11 #include <QTextStream>
12 #include <QDateTime>
13 #include <QDebug>
14
15 class LogUtility : public QObject
16 {
17     Q_OBJECT
18
19 public:
20     LogUtility(QString logPath,QObject *parent = 0) :
21         QObject(parent){
22         logFilePath = logPath;
23     }
24
25     ~LogUtility(){
26     }
27
28 public slots:
29     void logMessage(QString strMessage, bool appendMode = true) {
30
31         QFile logFile(logFilePath);
32
33         bool success = false;
34
35         if (appendMode){
36             success = logFile.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text);
37         }
38         else{
39             success = logFile.open(QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Text);
40         }
41
42         QString strTimeNow = QDateTime::currentDateTime().toString("dd-MMM-yyyy HH:mm:ss");
43         if (success) {
44             QTextStream logStream(&logFile);
45             logStream <<  strTimeNow << " - " << strMessage << endl;
46         }
47         else{
48             qDebug() <<  "ERROROPENINGLOGFILE" << " - " << strMessage;
49         }
50     }
51
52 private:
53     QString logFilePath;
54 };
55
56 #endif // LOGUTILITY_H