Line | |
---|
1 | #include "logger.h"
|
---|
2 |
|
---|
3 | Logger::Logger(QString appDir, QString logFileName)
|
---|
4 | {
|
---|
5 | myLogFile = std::make_unique<QFile>(appDir+"/"+logFileName);
|
---|
6 |
|
---|
7 | if (!myLogFile->open(QIODevice::WriteOnly | QIODevice::Text)){ //open to write
|
---|
8 | return;
|
---|
9 | }
|
---|
10 |
|
---|
11 | logStream = std::make_unique<QTextStream>(myLogFile.get());
|
---|
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("yyyy-MM-dd HH:mm:ss") << "\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("yyyy-MM-dd HH:mm:ss") << "\n" << arrToWrite << "\nEvent End.\n";
|
---|
34 | *this->logStream << "--------------------------------------------";
|
---|
35 | this->logStream->flush();
|
---|
36 | mutex.unlock();
|
---|
37 | }
|
---|
38 |
|
---|
39 | Logger::~Logger(){
|
---|
40 |
|
---|
41 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.