1 |
|
#include "logger.h" |
2 |
|
|
3 |
< |
Logger::Logger(QString appDir) |
3 |
> |
Logger::Logger(QString appDir, QString logFileName) |
4 |
|
{ |
5 |
< |
myLogFile = new QFile(appDir+"/"+GlobalVars::AppLogName); |
5 |
> |
myLogFile = std::make_unique<QFile>(appDir+"/"+logFileName); |
6 |
|
|
7 |
< |
if (!myLogFile->open(QIODevice::WriteOnly | QIODevice::Text)){ //open to write |
8 |
< |
return; |
9 |
< |
} |
7 |
> |
if (!myLogFile->open(QIODevice::WriteOnly | QIODevice::Text)){ //open to write |
8 |
> |
return; |
9 |
> |
} |
10 |
|
|
11 |
< |
logStream = new QTextStream (myLogFile); |
11 |
> |
logStream = std::make_unique<QTextStream>(myLogFile.get()); |
12 |
|
//logStream->setCodec("UTF-8"); |
13 |
|
} |
14 |
|
|
15 |
< |
/** |
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"; |
21 |
> |
*this->logStream << "\nEvent Start: " << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss") << "\n" << strToWrite << "\nEvent End.\n"; |
22 |
|
*this->logStream << "--------------------------------------------\n"; |
23 |
|
this->logStream->flush(); |
24 |
|
mutex.unlock(); |
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(); |
31 |
> |
mutex.lock(); |
32 |
> |
*this->logStream << "--------------------------------------------"; |
33 |
> |
*this->logStream << "\nEvent Start: " << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss") << "\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... |
39 |
|
Logger::~Logger(){ |
40 |
< |
delete this->logStream; |
42 |
< |
delete this->myLogFile; //the deconstructor of the file will close it automatically |
40 |
> |
|
41 |
|
} |