Release 0.4-0
[marketstoday] / src / cpp / logutility.h
index 46ba5eb..b762616 100644 (file)
@@ -1,5 +1,5 @@
 /*
-@version: 0.2
+@version: 0.4
 @author: Sudheer K. <scifi1947 at gmail.com>
 @license: GNU General Public License
 */
 #include <QFile>
 #include <QIODevice>
 #include <QDateTime>
+#include <QDir>
 
 class LogUtility : public QObject
 {
     Q_OBJECT
 
+private:
+    QFile * logFile;
+
 public:
     LogUtility(QObject *parent = 0) :
         QObject(parent){
+        QString strPath;
+
+#if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
+        //For maemo fremantle or harmattan use a common path
+        strPath = QDir().homePath() + "/.marketstoday/marketstoday.log";
+#else
+        strPath = "marketstoday.log";
+#endif
+        logFile = new QFile(strPath,this);
 
+        if (!logFile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
+            qDebug() << "MT: Error opening logfile for writing";
+        }
     }
+
     ~LogUtility(){
+        if (logFile->isOpen())
+            logFile->close();
         qDebug() << "Markets Today: In LogUtility object destructor..";
     }
 
@@ -28,19 +47,12 @@ public slots:
     void logMessage(QString strMessage) {
 
         QString strTimeNow = QDateTime::currentDateTime().toString("dd-MMM-yyyy HH:mm:ss");
-        qDebug() << QString("MT: [%1] - %2").arg(strTimeNow,strMessage);
-
-#if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
-    //For maemo fremantle or harmattan use a common path
-        QFile logFile("/home/user/.marketstoday/marketstoday.log");
-#else
-        QFile logFile("marketstoday.log");
-#endif
-
-        if (!logFile.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text)) { return; }
+        qDebug() << QString("MT: [%1] - %2").arg(strTimeNow,strMessage);        
 
-        QTextStream logStream(&logFile);
-        logStream <<  QString("[%1] - %2").arg(strTimeNow,strMessage) << endl;
+        if (logFile->isOpen() && logFile->isWritable()) {
+            QTextStream logStream(logFile);
+            logStream <<  QString("[%1] - %2").arg(strTimeNow,strMessage) << endl;
+        }
     }
 };