| 1 |
#include "logger.h" |
| 2 |
|
| 3 |
Logger::Logger() |
| 4 |
{ |
| 5 |
myLogFile = new QFile(GlobalVars::AppLogName); |
| 6 |
|
| 7 |
if (!myLogFile->open(QIODevice::WriteOnly | QIODevice::Text)){ //open to write |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
logStream = new QTextStream (myLogFile); |
| 12 |
//logStream->setCodec("UTF-8"); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
** Mutex makes it thread safe. (not sure if needed although) |
| 17 |
**/ |
| 18 |
void Logger::writeString(QString strToWrite){ |
| 19 |
mutex.lock(); |
| 20 |
*this->logStream << "--------------------------------------------"; |
| 21 |
*this->logStream << "\nEvent Start: " << QDateTime::currentDateTime().toString() << "\n" << strToWrite << "\nEvent End.\n"; |
| 22 |
*this->logStream << "--------------------------------------------\n"; |
| 23 |
this->logStream->flush(); |
| 24 |
mutex.unlock(); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
** Mutex makes it thread safe. (not sure if needed although) |
| 29 |
**/ |
| 30 |
void Logger::writeBytes(QByteArray arrToWrite){ |
| 31 |
mutex.lock(); |
| 32 |
*this->logStream << "--------------------------------------------"; |
| 33 |
*this->logStream << "\nEvent Start: " << QDateTime::currentDateTime().toString() << "\n" << arrToWrite << "\nEvent End.\n"; |
| 34 |
*this->logStream << "--------------------------------------------"; |
| 35 |
this->logStream->flush(); |
| 36 |
mutex.unlock(); |
| 37 |
} |
| 38 |
|
| 39 |
//deconstructor, will close the file automatically and free the resources... |
| 40 |
Logger::~Logger(){ |
| 41 |
delete this->logStream; |
| 42 |
delete this->myLogFile; //the deconstructor of the file will close it automatically |
| 43 |
} |