| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "COREFilePrintLogObserver.h" |
| 22 |
|
| 23 |
namespace Konclude { |
| 24 |
|
| 25 |
namespace Logger { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
COREFilePrintLogObserver::COREFilePrintLogObserver(const QString& outputFileString) : mOutputFile(outputFileString) { |
| 30 |
mOutputFileString = outputFileString; |
| 31 |
mOutputFileOpened = false; |
| 32 |
mMinLogLevel = 30; |
| 33 |
mMaxLogLevel = 100; |
| 34 |
|
| 35 |
CLogger* logger = CLogger::getInstance(); |
| 36 |
CCallbackRequestLogMessagesContext* logMessagesContext = logger->getLogMessages(0,-1,30,100,QStringList(),QLinkedList<void*>()); |
| 37 |
foreach (CLogMessage* message, logMessagesContext->getLogMessages()) { |
| 38 |
postLogMessage(message); |
| 39 |
} |
| 40 |
logMessagesContext->releaseMessages(); |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
COREFilePrintLogObserver::~COREFilePrintLogObserver() { |
| 45 |
if (mOutputFileOpened) { |
| 46 |
mOutputFile.close(); |
| 47 |
mOutputFileOpened = false; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
void COREFilePrintLogObserver::closeOutputFile() { |
| 52 |
if (mOutputFileOpened) { |
| 53 |
mOutputFile.close(); |
| 54 |
mOutputFileOpened = false; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
void COREFilePrintLogObserver::flushLogging() { |
| 59 |
closeOutputFile(); |
| 60 |
} |
| 61 |
|
| 62 |
void COREFilePrintLogObserver::forcedPathCreated(const QString& filePath) { |
| 63 |
QString path = filePath; |
| 64 |
if (path.contains("/") || path.contains("\\")) { |
| 65 |
int lastSlash = path.lastIndexOf("/"); |
| 66 |
int lastBackSlash = path.lastIndexOf("\\"); |
| 67 |
int lastSeparator = qMax(lastBackSlash,lastSlash); |
| 68 |
path = path.mid(0,lastSeparator); |
| 69 |
if (!path.isEmpty()) { |
| 70 |
QDir dir; |
| 71 |
dir.mkpath(path); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void COREFilePrintLogObserver::postLogMessage(CLogMessage *message) { |
| 77 |
|
| 78 |
bool printMessage = false; |
| 79 |
double logLevel = message->getLogLevel(); |
| 80 |
QString domainString = message->getDomain(); |
| 81 |
if (logLevel > mMinLogLevel && logLevel <= mMaxLogLevel) { |
| 82 |
printMessage = true; |
| 83 |
} |
| 84 |
|
| 85 |
if (printMessage) { |
| 86 |
if (!mOutputFileOpened) { |
| 87 |
forcedPathCreated(mOutputFileString); |
| 88 |
mOutputFileOpened = true; |
| 89 |
mOutputFile.open(QIODevice::Append); |
| 90 |
} |
| 91 |
|
| 92 |
QString messageString; |
| 93 |
QString logLevelString; |
| 94 |
if (logLevel >= 90) { |
| 95 |
logLevelString = "catastrophic error"; |
| 96 |
} else if (logLevel >= 80) { |
| 97 |
logLevelString = "exceptional error"; |
| 98 |
} else if (logLevel >= 70) { |
| 99 |
logLevelString = "error"; |
| 100 |
} else if (logLevel >= 60) { |
| 101 |
logLevelString = "warning"; |
| 102 |
} else if (logLevel >= 30) { |
| 103 |
logLevelString = "info"; |
| 104 |
} else { |
| 105 |
printMessage = false; |
| 106 |
logLevelString = "notice"; |
| 107 |
} |
| 108 |
|
| 109 |
messageString += QString("{%1} %2 [%3] >> %4\r\n").arg(logLevelString).arg(message->getTimeMSecString()).arg(domainString).arg(message->getMessage()); |
| 110 |
|
| 111 |
mOutputFile.write( messageString.toUtf8().data() ); |
| 112 |
closeOutputFile(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
}; // end namespace Logger |
| 118 |
|
| 119 |
}; // end namespace Konclude |