X-Git-Url: http://git.maemo.org/git/?p=vicar;a=blobdiff_plain;f=src%2Fvicar-lib%2Fcpp%2Flogutility.h;fp=src%2Fvicar-lib%2Fcpp%2Flogutility.h;h=5d1c255aadb05ee368bf24221da280b0189cf9dc;hp=ebca69e5c876fc156bc3cf6ccaac94566d4c8c67;hb=f8710fc521c8941fc417018999806695ace8a2cb;hpb=38440679f13b8dc528a9aff93cbf4935011fdb22 diff --git a/src/vicar-lib/cpp/logutility.h b/src/vicar-lib/cpp/logutility.h index ebca69e..5d1c255 100644 --- a/src/vicar-lib/cpp/logutility.h +++ b/src/vicar-lib/cpp/logutility.h @@ -1,56 +1,71 @@ /* -@version: 0.6 +@version: 0.4 @author: Sudheer K. @license: GNU General Public License */ #ifndef LOGUTILITY_H #define LOGUTILITY_H +#include #include #include -#include #include -#include +#include class LogUtility : public QObject { Q_OBJECT +private: + QFile * logFile; + public: - LogUtility(QString logPath,QObject *parent = 0) : + LogUtility(QObject *parent = 0) : QObject(parent){ - logFilePath = logPath; + QString strPath; + +#if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6) + //For maemo fremantle or harmattan use a common path + QDir logDir = QDir(QDir().homePath() + "/.vicar"); + if (!logDir.exists()){ + if (QDir().mkpath(logDir.absolutePath())){ + qDebug() << "Vicar: Log directory created successfully"; + } + else{ + qDebug() << "Vicar: Error creating log directory"; + } + } + strPath = logDir.absolutePath() + "/vicar.log"; +#else + strPath = "vicar.log"; +#endif + logFile = new QFile(strPath,this); + + if (!logFile->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) { + qDebug() << "Vicar: Error opening logfile for writing at path " << strPath; + } } ~LogUtility(){ + if (logFile->isOpen()) + logFile->close(); + qDebug() << "Vicar: In LogUtility object destructor.."; } public slots: - void logMessage(QString strMessage, bool appendMode = true) { - - QFile logFile(logFilePath); - - bool success = false; - - if (appendMode){ - success = logFile.open(QIODevice::Append | QIODevice::WriteOnly | QIODevice::Text); - } - else{ - success = logFile.open(QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Text); - } + void logMessage(QString strMessage) { QString strTimeNow = QDateTime::currentDateTime().toString("dd-MMM-yyyy HH:mm:ss"); - if (success) { - QTextStream logStream(&logFile); - logStream << strTimeNow << " - " << strMessage << endl; + + if (logFile->isOpen() && logFile->isWritable()) { + QTextStream logStream(logFile); + logStream << QString("[%1] - %2").arg(strTimeNow,strMessage) << endl; + qDebug() << QString("Vicar: [%1] - %2").arg(strTimeNow,strMessage); } else{ - qDebug() << "ERROROPENINGLOGFILE" << " - " << strMessage; + qDebug() << "ERROROPENINGLOGFILE" << QString("Vicar: [%1] - %2").arg(strTimeNow,strMessage); } } - -private: - QString logFilePath; }; #endif // LOGUTILITY_H