From: tkawata Date: Tue, 29 May 2012 00:24:40 +0000 (+0900) Subject: Refactored messaging facility X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=016ae39399d5cf7f10fd1bb2867d66d86772eb11;p=dennco%2Fdennco.git Refactored messaging facility --- diff --git a/Source/DNContainerBuilder.cpp b/Source/DNContainerBuilder.cpp index f495b68..ecce3c5 100644 --- a/Source/DNContainerBuilder.cpp +++ b/Source/DNContainerBuilder.cpp @@ -75,12 +75,7 @@ bool DNContainerBuilder::buildContainerFromXHTML(const char* docRoot) { std::string message = std::string("Failed to construct cell: ").append(cellInfo->location).append("#").append(cellInfo->cellName); message.append("\nCellCode '").append(cellInfo->cellCodeClassOrType).append("' couldn't be found."); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } } else @@ -91,12 +86,7 @@ bool DNContainerBuilder::buildContainerFromXHTML(const char* docRoot) if (newCell == NULL) { std::string message = std::string("Failed to construct cell: ").append(cellInfo->location).append("#").append(cellInfo->cellName); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } mPendingCellDefinitions.pop(); } @@ -113,23 +103,13 @@ bool DNContainerBuilder::buildContainerFromXHTML(const char* docRoot) if (!orgCell->connectTo(connInfo->connectionName, tgtCell)) { std::string message = std::string("Failed to make connection. Receptor name for the target cell may be duplicated. (").append(connInfo->connectionName).append(" cell:").append(connInfo->originCellName).append(" - > ").append(connInfo->targetCellName); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } } else { std::string message = std::string("Failed to make connection (").append(connInfo->connectionName).append(" cell:").append(connInfo->originCellName).append(" - > ").append(connInfo->targetCellName); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } mPendingConnections.pop(); } @@ -204,12 +184,7 @@ void DNContainerBuilder::parseXHTML(const char *docRoot, const char *path) if (defineDepth > 0) { std::string message = std::string("Syntax error while parsing ").append(path).append(". A defnie element seems to be nested."); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } else { @@ -227,12 +202,7 @@ void DNContainerBuilder::parseXHTML(const char *docRoot, const char *path) else { std::string message = std::string("Syntax error while parsing ").append(path).append(". defnie='").append("' isn't correct."); - TKLog::printf("%s\n", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } } } @@ -252,12 +222,7 @@ void DNContainerBuilder::parseXHTML(const char *docRoot, const char *path) { std::string message = std::string("Syntax error while parsing ").append(path).append(". CellCode:").append(cellName); message.append("\n'type' can't be defined here if you use CellCode class."); - TKLog::printf("%s\n", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } } else if (parameter== "connection") @@ -273,12 +238,7 @@ void DNContainerBuilder::parseXHTML(const char *docRoot, const char *path) else { std::string message = std::string("Syntax error while parsing ").append(path).append(". parameter='").append(parameter).append("' isn't correct."); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } } diff --git a/Source/DNEngine.cpp b/Source/DNEngine.cpp index c698036..b4b5095 100644 --- a/Source/DNEngine.cpp +++ b/Source/DNEngine.cpp @@ -47,6 +47,7 @@ DNEngine::DNEngine(const char *contentPath) : mUIPath = "/ui/index.html"; dnGlobal()->updateRunningStatus(DNGlobal::STOPPED); + dnGlobal()->resetErrorStatus(); std::string basePath(contentPath); std::string containerRoot = basePath; @@ -56,15 +57,7 @@ DNEngine::DNEngine(const char *contentPath) : succeeded = parseSettingFile(contentPath); if (!succeeded || !dnGlobal()->isErrorStatusNormal()) { - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("property.xml parse error"); - dnGlobal()->setMessage2("failed to parse setting file /property.xml"); - } - else - { - dnGlobal()->setMessage1("property.xml parse error"); - } + dnNotifyError("property.xml parse error", "failed to parse setting file /property.xml"); return; } @@ -74,30 +67,14 @@ DNEngine::DNEngine(const char *contentPath) : succeeded = mContainer->setDataStore(dataStorePath.c_str()); if (!succeeded) { - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2("Failed to the setup data store"); - } - else - { - dnGlobal()->setMessage1("Initialization failed"); - } + dnNotifyError("Initialization failed","Failed to the setup data store"); return; } succeeded = parseContainerFile(containerRoot.c_str()); if (!succeeded) { - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2("Failed to parse container file"); - } - else - { - dnGlobal()->setMessage1("Initialization failed"); - } + dnNotifyError("Initialization failed", "Failed to parse container file"); return; } @@ -154,32 +131,14 @@ bool DNEngine::parseSettingFile(const char *contentRoot) { valid = false; std::string message = "Failed to load property.xml file"; - TKLog::printf("ERROR wile loading property.xml file.\n%s\n",message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } - else - { - dnGlobal()->setMessage1("Initialization failed"); - } + dnNotifyError("Initialization failed",message); } if (valid && element->name != "dennco") { valid = false; std::string message = "First element of property.xml should be "; - TKLog::printf("ERROR wile parsing property.xml file.\n%s\n",message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } - else - { - dnGlobal()->setMessage1("Initialization failed"); - } + dnNotifyError("Initialization failed",message); } if (valid) @@ -202,16 +161,7 @@ bool DNEngine::parseSettingFile(const char *contentRoot) { valid = false; std::string message = "Error in property.xml. TickIntervalSec is not configured properly."; - TKLog::printf("ERROR wile parsing property.xml file.\n%s\n",message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } - else - { - dnGlobal()->setMessage1("Initialization failed"); - } + dnNotifyError("Initialization failed",message); } } else if (pname == "UIPATH") @@ -365,6 +315,9 @@ void DNEngine::doTickThread(void *self) float DNEngine::doClientGetRequest(const char* path) { + if (dnGlobal()->getRunningStatus() != DNGlobal::RUNNIING) + return 0; + std::string fqn = getFQNString("/",path); TKUICell *cell = (TKUICell*)mContainer->getInterfaceCell(fqn); float result = 0; @@ -372,6 +325,13 @@ float DNEngine::doClientGetRequest(const char* path) { result = cell->getValue(); } + else + { + std::string message = "The requested path "; + message += path; + message += " doesn't exist."; + dnNotifyWarning("Failed to process the client request. (getValue)", message); + } return result; } @@ -382,6 +342,9 @@ bool DNEngine::doClientSetRequest(const char* path, const char* value) bool DNEngine::doClientSetRequest(const char* path, float value) { + if (dnGlobal()->getRunningStatus() != DNGlobal::RUNNIING) + return false; + std::string fqn = getFQNString("/",path); TKUICell *cell = (TKUICell*)mContainer->getInterfaceCell(fqn); bool result = false; @@ -390,6 +353,13 @@ bool DNEngine::doClientSetRequest(const char* path, float value) cell->setValue(value); result = true; } + else + { + std::string message = "The requested path "; + message += path; + message += " doesn't exist."; + dnNotifyWarning("Failed to process the client request. (setValue)", message); + } return result; } diff --git a/Source/DNUtils.cpp b/Source/DNUtils.cpp index f760ae0..e7e4e83 100644 --- a/Source/DNUtils.cpp +++ b/Source/DNUtils.cpp @@ -20,6 +20,7 @@ #include "DNUtils.h" #include "TKLog.h" #include "TKDebug.h" +#include "DNGlobal.h" #ifndef _MSC_VER #include @@ -220,7 +221,6 @@ std::string parseCellCodeForScriptEngine(std::string jname, std::string cellCode if (inFunction == -1) { //FUNCTION DEF - std::string fn = ""; funcs.append(jname); funcs.append(".prototype."); funcs.append(fff); @@ -545,3 +545,58 @@ std::string getJSEscapeString(const char *cstring) return result; } +static TKLock NotifyLock; + +void dnNotifyError(std::string title, std::string message) +{ + NotifyLock.lock(); + + std::string::size_type pos = 0; + while(pos = message.find("\n", pos), pos != std::string::npos) { + message.replace(pos, 1, "\n "); + pos += 1; + } + + std::string outMessage = "[ERROR!!!]:"; + outMessage.append(title).append("\n "); + outMessage.append(message); + + TKLog::printf(TKLog::ERROR, "%s", outMessage.c_str()); + if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) + { + dnGlobal()->setMessage1(title); + dnGlobal()->setMessage2(message); + } + else + { + dnGlobal()->setMessage1(title); + } + NotifyLock.unlock(); +} + +void dnNotifyWarning(std::string title, std::string message) +{ + NotifyLock.lock(); + + std::string::size_type pos = 0; + while(pos = message.find("\n", pos), pos != std::string::npos) { + message.replace(pos, 1, "\n "); + pos += 1; + } + + std::string outMessage = "[WARNING] :"; + outMessage.append(title).append("\n "); + outMessage.append(message); + + TKLog::printf(TKLog::WARNING, "%s", outMessage.c_str()); + if (dnGlobal()->updateErrorStatus(DNGlobal::WARNING)) + { + dnGlobal()->setMessage1(title); + dnGlobal()->setMessage2(message); + } + else + { + dnGlobal()->setMessage1(title); + } + NotifyLock.unlock(); +} diff --git a/Source/DNUtils.h b/Source/DNUtils.h index 9394c3e..aee5d1b 100644 --- a/Source/DNUtils.h +++ b/Source/DNUtils.h @@ -30,6 +30,9 @@ std::string getFQNString(const char *location, const char *name); std::string getJSEscapeString(const char *cstring); std::string parseCellCodeForScriptEngine(std::string jname, std::string cellCode); +void dnNotifyError(std::string title, std::string message); +void dnNotifyWarning(std::string title, std::string message); + class DNLocker { public: diff --git a/Source/QtDennco/mainwindow.cpp b/Source/QtDennco/mainwindow.cpp index 6f3d0a8..1624469 100644 --- a/Source/QtDennco/mainwindow.cpp +++ b/Source/QtDennco/mainwindow.cpp @@ -33,13 +33,18 @@ #include "DNSettings.h" //static +QColor MainWindow::NORMAL_COLOR(0,0,0); +//static +QColor MainWindow::WARN_COLOR(0xff, 0x95, 0x2b); +//static +QColor MainWindow::ERROR_COLOR(0xff, 0,0); +//static QWidget *MainWindow::instance = NULL; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), - mEngine(NULL), mWebInterface(NULL), - mHasPendingConsoleMessage(false) + mEngine(NULL), mWebInterface(NULL) { instance = this; ui->setupUi(this); @@ -49,7 +54,7 @@ MainWindow::MainWindow(QWidget *parent) : ui->filePath->setText(contentPath); - ui->console->document()->setMaximumBlockCount(2000); + ui->console->document()->setMaximumBlockCount(500); QFont monofont("Courier"); monofont.setStyleHint(QFont::Monospace); ui->console->setFont(monofont); @@ -186,28 +191,40 @@ void MainWindow::deleteCurrentEngine() void MainWindow::paintEvent( QPaintEvent * event ) { mConsoleLock.lock(); - if (mHasPendingConsoleMessage) + while (!mPendingConsoleMessages.isEmpty()) { - ui->console->append(mPendingConsoleMessage); - mPendingConsoleMessage.clear(); - mHasPendingConsoleMessage = false; + PendingConsoleMessage aMessage = mPendingConsoleMessages.dequeue(); + switch(aMessage.messageType) + { + case TKLog::NORMAL : + ui->console->setTextColor(NORMAL_COLOR); + break; + case TKLog::WARNING : + ui->console->setTextColor(WARN_COLOR); + break; + case TKLog::ERROR : + ui->console->setTextColor(ERROR_COLOR); + break; + } + ui->console->append(aMessage.text); } mConsoleLock.unlock(); QMainWindow::paintEvent(event); } -void MainWindow::vprintf(const char *fmt, va_list ap) + +void MainWindow::vprintf(TKLog::MessageType type, const char *fmt, va_list ap) { QString msg = QString().vsprintf(fmt,ap); #ifdef DEBUG qDebug() << msg << endl; #endif mConsoleLock.lock(); - if (mHasPendingConsoleMessage) - mPendingConsoleMessage.append("\n"); - mPendingConsoleMessage.append(msg); - mHasPendingConsoleMessage = true; + PendingConsoleMessage newMessage; + newMessage.messageType = type; + newMessage.text = msg; + mPendingConsoleMessages.enqueue(newMessage); mConsoleLock.unlock(); emit consoleUpdated(); } diff --git a/Source/QtDennco/mainwindow.h b/Source/QtDennco/mainwindow.h index a2e2757..11f7c94 100644 --- a/Source/QtDennco/mainwindow.h +++ b/Source/QtDennco/mainwindow.h @@ -20,6 +20,7 @@ #define MAINWINDOW_H #include +#include #include "TKConsole.h" #include "TKLock.h" @@ -62,11 +63,21 @@ private: TKConsole *mConsole; DNWebInterface *mWebInterface; - virtual void vprintf(const char *fmt, va_list arg); + class PendingConsoleMessage + { + public: + TKLog::MessageType messageType; + QString text; + }; + + static QColor NORMAL_COLOR; + static QColor WARN_COLOR; + static QColor ERROR_COLOR; + + virtual void vprintf(TKLog::MessageType type, const char *fmt, va_list arg); virtual void vDebugPrintf(const char *fmt, va_list arg); TKLock mConsoleLock; - QString mPendingConsoleMessage; - bool mHasPendingConsoleMessage; + QQueue mPendingConsoleMessages; signals: void consoleUpdated(); diff --git a/Source/QtScript/dnqsbasiccell.cpp b/Source/QtScript/dnqsbasiccell.cpp index 2e8e3ac..bcb3c7d 100644 --- a/Source/QtScript/dnqsbasiccell.cpp +++ b/Source/QtScript/dnqsbasiccell.cpp @@ -24,6 +24,7 @@ #include "TKReceptor.h" #include "TKAxon.h" #include "TKCellCodeInstance.h" +#include "DNUtils.h" DNQSBasicCell::DNQSBasicCell(DNQSContainer *container, std::string location, std::string name, bool canInterface): DNQSCellBase(container,location,name,canInterface) @@ -65,14 +66,19 @@ void DNQSBasicCell::reflectValueFromScript() void DNQSBasicCell::handleScriptException(const char *funcName) { - TKLog::printf("Script Error! in %s#%s.%s", mLocation.c_str(), mName.c_str(), funcName); + std::string message = "Script Error! in "; + message += mLocation; + message += "#"; + message += funcName; if (mEngine->hasUncaughtException()) { QScriptValue error = mEngine->uncaughtException(); QString errorString = error.toString(); - TKLog::printf(" Error Message: %s", errorString.toLocal8Bit().data()); + message += "\n"; + message += errorString.toStdString(); mEngine->clearExceptions(); } + dnNotifyWarning("Script error", message); } bool DNQSBasicCell::doTick(float time) diff --git a/Source/QtScript/dnqsbasicstoragecell.cpp b/Source/QtScript/dnqsbasicstoragecell.cpp index 1a61b66..7f325aa 100644 --- a/Source/QtScript/dnqsbasicstoragecell.cpp +++ b/Source/QtScript/dnqsbasicstoragecell.cpp @@ -262,7 +262,7 @@ QScriptValue DNQSBasicStorageCell::scriptFunction_storage_getXYZVArray(QScriptCo { QScriptValue error = engine->uncaughtException(); QString errorString = error.toString(); - TKLog::printf("Script error at scriptFunction_storage_getXYZVArray. Error Message: %s", errorString.toLocal8Bit().data()); + TKLog::debugPrintf("Script error at scriptFunction_storage_getXYZVArray. Error Message: %s", errorString.toLocal8Bit().data()); engine->clearExceptions(); } #endif //DEBUG diff --git a/Source/QtScript/dnqscellcode.cpp b/Source/QtScript/dnqscellcode.cpp index 2b7ab93..af66c12 100644 --- a/Source/QtScript/dnqscellcode.cpp +++ b/Source/QtScript/dnqscellcode.cpp @@ -24,6 +24,7 @@ #include "dnqscontainer.h" #include "dnqscellbase.h" #include "dnqscellcodeinstance.h" +#include "DNGlobal.h" DNQSCellCode::DNQSCellCode(std::string name, std::string cellapi, DNQSContainer *container, std::string code): TKCellCode(name,cellapi), mCellContainer(container) @@ -34,10 +35,27 @@ DNQSCellCode::DNQSCellCode(std::string name, std::string cellapi, DNQSContainer QScriptEngine *scriptEngine = mCellContainer->getScriptEngine(); QScriptValue scriptGlobalObject = mCellContainer->getScriptGlobalObject(); scriptEngine->evaluate(QScriptProgram(stmt)); + if (scriptEngine->hasUncaughtException()) + { + QScriptValue error = scriptEngine->uncaughtException(); + QString errorString = error.toString(); + scriptEngine->clearExceptions(); + + std::string message = "Failed to construct Cell code : "; + message.append(getName()).append("\n"); + message.append("Error Message:").append(errorString.toStdString()); + dnNotifyError("Initialization failed", message); + return; + } + mCellCodeConstructor = scriptGlobalObject.property(QString::fromStdString(jname), QScriptValue::ResolveLocal); if (mCellCodeConstructor.isNull() || mCellCodeConstructor.isUndefined() || ! mCellCodeConstructor.isValid()) { - TKLog::printf("DNQSCellCode ERROR creating:%s", name.c_str()); + std::string message = "Failed to construct Cell code : "; + message.append(getName()).append("\n"); + message.append("The constructor is invalid."); + dnNotifyError("Initialization failed", message); + return; } } @@ -53,20 +71,38 @@ TKCellCodeInstance* DNQSCellCode::createCellCodeInstance(TKCell *owner, const vo } if (mCellCodeConstructor.isNull() || mCellCodeConstructor.isUndefined() || ! mCellCodeConstructor.isValid()) { - TKLog::printf("DNQSCellCode::createCellCodeInstance ERROR. %s", getName().c_str()); + std::string message = "Failed to construct Cell code '"; + message.append(owner->getName()); + message.append("' cellcode:"); + message.append(getName()).append("\n"); + message.append("Cell code invalid"); + dnNotifyError("Initialization failed", message); return NULL; } TKCellCodeInstance *result = NULL; + QScriptEngine *scriptEngine = mCellContainer->getScriptEngine(); QScriptValueList args; args << ((DNQSCellBase*)owner)->getCellAPI(); QScriptValue instance = mCellCodeConstructor.construct(args); + if (scriptEngine->hasUncaughtException()) + { + QScriptValue error = scriptEngine->uncaughtException(); + QString errorString = error.toString(); + scriptEngine->clearExceptions(); + + std::string message = "Failed to construct Cell code '"; + message.append(owner->getName()).append("'\n"); + message.append("cellcode:"); + message.append(getName()).append("\n"); + message.append("Error Message:").append(errorString.toStdString()); + dnNotifyError("Initialization failed", message); + return NULL; + } if (instance.isValid()) { - QScriptEngine *scriptEngine = mCellContainer->getScriptEngine(); - // QScriptContext *context = scriptEngine->pushContext(); context->setThisObject(instance); @@ -74,6 +110,20 @@ TKCellCodeInstance* DNQSCellCode::createCellCodeInstance(TKCell *owner, const vo QString customScriptStr((const char*)data); QScriptProgram customScript(customScriptStr); scriptEngine->evaluate(customScript); + if (scriptEngine->hasUncaughtException()) + { + QScriptValue error = scriptEngine->uncaughtException(); + QString errorString = error.toString(); + scriptEngine->clearExceptions(); + + std::string message = ""; + message.append("Failed to construct Cell code '"); + message.append(owner->getName()).append("'\n"); + message.append("cellcode:"); + message.append(getName()).append("\n"); + message.append("Error Message:").append(errorString.toStdString()); + dnNotifyError("Initialization failed", message); + } scriptEngine->popContext(); diff --git a/Source/QtScript/dnqscontainer.cpp b/Source/QtScript/dnqscontainer.cpp index 1e2de37..5cc1d16 100644 --- a/Source/QtScript/dnqscontainer.cpp +++ b/Source/QtScript/dnqscontainer.cpp @@ -42,7 +42,7 @@ static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine) if (context->argumentCount() >= 1) { QString value = context->argument(0).toString(); - TKLog::printf("%s", value.toLocal8Bit().data()); + TKLog::printf(TKLog::NORMAL, "%s", value.toLocal8Bit().data()); } return engine->nullValue(); } @@ -119,12 +119,7 @@ TKCell* DNQSContainer::createCellWithoutCellCodeClass(std::string theLocation, s { std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type."); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } if (cell) @@ -145,19 +140,18 @@ TKCell* DNQSContainer::createCellWithoutCellCodeClass(std::string theLocation, s { std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); message.append("'\nThe cellcode is type '").append(type).append("'. This type doesn't support custom script."); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed",message); } } } else { std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str()); - TKLog::printf("Failed to create a cell. %s", fqnName.c_str()); + std::string message = "Failed to create a cell. "; + message += fqnName; + message += "\n"; + message += "Out of memory?"; + dnNotifyError("Initialization failed", message); } return cell; @@ -181,34 +175,19 @@ TKCell* DNQSContainer::createCellWithCellCodeClass(std::string theLocation, std: { std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); message.append("'\nThe cellcode type '").append(type).append("' doesn't support to have a CellCode class."); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } else if (type == CELLTYPE_IN) { std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); message.append("'\nThe cellcode requires type '").append(type).append("' doesn't support to have a CellCode class."); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed", message); } else { std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type."); - TKLog::printf("%s", message.c_str()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Initialization failed"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Initialization failed",message); } @@ -226,7 +205,11 @@ TKCell* DNQSContainer::createCellWithCellCodeClass(std::string theLocation, std: else { std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str()); - TKLog::printf("Failed to create a cell. %s", fqnName.c_str()); + std::string message = "Failed to create a cell. "; + message += fqnName; + message += "\n"; + message += "Out of memory?"; + dnNotifyError("Initialization failed", message); } return cell; diff --git a/Source/TKConsole.h b/Source/TKConsole.h index 1b04780..1ab6e84 100644 --- a/Source/TKConsole.h +++ b/Source/TKConsole.h @@ -21,11 +21,12 @@ #define dennco_TKConsole_h #include +#include "TKLog.h" class TKConsole { public: - virtual void vprintf(const char *fmt, va_list arg); + virtual void vprintf(TKLog::MessageType type, const char *fmt, va_list arg); virtual void vDebugPrintf(const char *fmt, va_list arg); }; diff --git a/Source/TKLog.cpp b/Source/TKLog.cpp index 28a0469..d182b0d 100644 --- a/Source/TKLog.cpp +++ b/Source/TKLog.cpp @@ -24,13 +24,13 @@ TKConsole *TKLog::msConsole = 0; -void TKLog::printf(const char* fmt,...) +void TKLog::printf(MessageType type, const char* fmt,...) { if (TKLog::msConsole) { va_list ap; va_start(ap, fmt); - msConsole->vprintf(fmt, ap); + msConsole->vprintf(type, fmt, ap); va_end(ap); } } diff --git a/Source/TKLog.h b/Source/TKLog.h index f885d5c..7ed34f0 100644 --- a/Source/TKLog.h +++ b/Source/TKLog.h @@ -25,10 +25,12 @@ class TKConsole; class TKLog { public: - static void printf(const char* fmt,...); - static void debugPrintf(const char* fmt,...); + enum MessageType {NORMAL, WARNING, ERROR}; + + static void printf(MessageType type, const char* fmt,...); + static void debugPrintf(const char* fmt,...); static void setDestination(TKConsole *console); private: static TKConsole *msConsole; }; -#endif \ No newline at end of file +#endif diff --git a/Source/TKReceptor.cpp b/Source/TKReceptor.cpp index a0404cf..45e4a3a 100644 --- a/Source/TKReceptor.cpp +++ b/Source/TKReceptor.cpp @@ -26,7 +26,7 @@ float TKReceptor::getValue() { #ifdef DEBUG_CONNECTION - TKLog::printf("TKReceptor::getValue called: owner:%s tarminal owner:%s value:%f\n", + TKLog::debugPrintf("TKReceptor::getValue called: owner:%s tarminal owner:%s value:%f\n", mOwner->getName().c_str(), mTerminal->getOwner()->getOwner()->getName().c_str(), mTerminal->getValue()); diff --git a/Source/platform/qt/QtTKConsole.cpp b/Source/platform/qt/QtTKConsole.cpp index 860cc0e..364af26 100644 --- a/Source/platform/qt/QtTKConsole.cpp +++ b/Source/platform/qt/QtTKConsole.cpp @@ -21,8 +21,9 @@ #include -void TKConsole::vprintf(const char *fmt, va_list ap) +void TKConsole::vprintf(TKLog::MessageType type, const char *fmt, va_list ap) { + (type); QString msg = QString().vsprintf(fmt,ap); qDebug() << msg << endl; } diff --git a/Source/platform/qt/qtdnserialportimpl.cpp b/Source/platform/qt/qtdnserialportimpl.cpp index 3b18940..4cd1eba 100644 --- a/Source/platform/qt/qtdnserialportimpl.cpp +++ b/Source/platform/qt/qtdnserialportimpl.cpp @@ -224,7 +224,7 @@ void QtDNSerialPortImpl::serialCommunicationThreadBody(void *_impl) port = NULL; } retry--; - TKLog::printf("Failed to open the serial port. retrying..."); + dnNotifyWarning("SerialPort initialization", "Failed to open the serial port. retrying..."); } } while (!r && retry >= 0); @@ -237,7 +237,7 @@ void QtDNSerialPortImpl::serialCommunicationThreadBody(void *_impl) impl->mStartStopLock.lock(); impl->mStopping = true; impl->mStartStopLock.unlock(); - TKLog::printf("Failed to open the serial port. giving up!"); + dnNotifyWarning("SerialPort initialization", "Failed to open the serial port. giving up!"); return; } @@ -350,7 +350,7 @@ void QtDNSerialPortImpl::requestHandelrThreadBody(void *_impl) if (cmd == "M") { //message - TKLog::printf("(SERIALl):%s", par.toLocal8Bit().constData()); + TKLog::printf(TKLog::NORMAL, "(SERIALl):%s", par.toLocal8Bit().constData()); } else if (cmd == "S") { diff --git a/Source/platform/qt/qtdnstorageimpl.cpp b/Source/platform/qt/qtdnstorageimpl.cpp index 4f0a43f..b1fb909 100644 --- a/Source/platform/qt/qtdnstorageimpl.cpp +++ b/Source/platform/qt/qtdnstorageimpl.cpp @@ -21,6 +21,7 @@ #include "DNStorage.h" #include "DNStorageImpl.h" #include "TKLog.h" +#include "DNUtils.h" #include #include @@ -77,7 +78,7 @@ bool QtDNStorageImpl::setValue(const char *path, const char *key, float value) message.append(key); message.append("\n"); message.append(mQueries->propertiesCountQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); return false; } mQueries->propertiesCountQuery.next(); @@ -95,7 +96,7 @@ bool QtDNStorageImpl::setValue(const char *path, const char *key, float value) message.append(key); message.append("\n"); message.append(mQueries->propertiesCountQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); return false; } } @@ -112,7 +113,7 @@ bool QtDNStorageImpl::setValue(const char *path, const char *key, float value) message.append(key); message.append("\n"); message.append(mQueries->propertiesCountQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); return false; } } @@ -137,7 +138,7 @@ float QtDNStorageImpl::getValue(const char *path, const char *key) message.append(key); message.append("\n"); message.append(mQueries->propertiesCountQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); return false; } mQueries->propertiesGetQuery.next(); @@ -161,7 +162,7 @@ int QtDNStorageImpl::getCount(const char *path, const char *key) message.append(key); message.append("\n"); message.append(mQueries->propertiesCountQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); return 0; } @@ -186,7 +187,7 @@ int QtDNStorageImpl::countXYZVData(const char *path, const char *key) message.append(key); message.append("\n"); message.append(mQueries->xyzvDataCountQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); return 0; } @@ -217,7 +218,7 @@ bool QtDNStorageImpl::insertXYZVData(const char *path, const char *key, int inde message.append(key); message.append("\n"); message.append(mQueries->xyzvDataInsetQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); return false; } @@ -241,7 +242,7 @@ DNStorageXYZVRecords* QtDNStorageImpl::queryXYZVData(const char *path, const cha message.append(key); message.append("\n"); message.append(mQueries->xyzvDataCountQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); return 0; } mQueries->xyzvDataCountQuery.next(); @@ -257,7 +258,7 @@ DNStorageXYZVRecords* QtDNStorageImpl::queryXYZVData(const char *path, const cha message.append(key); message.append("\n"); message.append(mQueries->xyzvDataGetQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); if (records) delete records; return 0; @@ -295,7 +296,7 @@ bool QtDNStorageImpl::deleteXYZVData(const char *path, const char *key) message.append(key); message.append("\n"); message.append(mQueries->xyzvDataRemoveQuery.lastError().text().toStdString()); - TKLog::printf("%s", message.c_str()); + dnNotifyWarning("SQL ERROR", message); return false; } return true; @@ -347,11 +348,7 @@ bool QtDNStorageImpl::initDB() message.append(mStoragePath.toStdString()); message.append("\n"); message.append(mDatabase->database.lastError().text().toStdString()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Persistent storage initialization error."); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Persistent storage initialization error.", message); return false; } @@ -366,11 +363,7 @@ bool QtDNStorageImpl::initDB() message.append(mStoragePath.toStdString()); message.append("\n"); message.append(mDatabase->database.lastError().text().toStdString()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Persistent storage initialization error."); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Persistent storage initialization error.", message); return false; } } @@ -385,11 +378,7 @@ bool QtDNStorageImpl::initDB() message.append(mStoragePath.toStdString()); message.append("\n"); message.append(mDatabase->database.lastError().text().toStdString()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Persistent storage initialization error."); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Persistent storage initialization error.", message); return false; } } @@ -412,11 +401,7 @@ bool QtDNStorageImpl::initDB() message.append(mStoragePath.toStdString()); message.append("\n"); message.append(mDatabase->database.lastError().text().toStdString()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Persistent storage initialization error."); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Persistent storage initialization error.", message); return false; } diff --git a/Source/platform/qt/qtdnthreadimpl.cpp b/Source/platform/qt/qtdnthreadimpl.cpp index 049cc50..215768f 100644 --- a/Source/platform/qt/qtdnthreadimpl.cpp +++ b/Source/platform/qt/qtdnthreadimpl.cpp @@ -37,7 +37,7 @@ QtDNThreadImpl::~QtDNThreadImpl() { if (!wait(5000)) { - TKLog::printf("!WARNING! DNThread %p hasn't finished in 5sec. execute terminate to quit forcedly ", this); + TKLog::printf(TKLog::WARNING, "!WARNING! DNThread %p hasn't finished in 5sec. execute terminate to quit forcedly ", this); setTerminationEnabled(true); terminate(); } diff --git a/Source/platform/qt/qtdnxmlimpl.cpp b/Source/platform/qt/qtdnxmlimpl.cpp index fe55db5..f8d56dd 100644 --- a/Source/platform/qt/qtdnxmlimpl.cpp +++ b/Source/platform/qt/qtdnxmlimpl.cpp @@ -21,6 +21,7 @@ #include "DNXMLElement.h" #include "TKLog.h" #include "DNGlobal.h" +#include "DNUtils.h" DNXMLImpl *DNXMLImpl::createXMLFromFileImpl(const char *docRoot, const char *docPath) { @@ -52,14 +53,9 @@ QtDNXMLImpl::QtDNXMLImpl(QString filePath) : mHandler(NULL), mValid(false), mFil if (!mValid) { - TKLog::printf("ERROR failed to load file.%s", filePath.toLocal8Bit().data()); std::string message = "failed to load file:"; message.append(filePath.toStdString()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Error at loading XML file"); - dnGlobal()->setMessage2(message); - } + dnNotifyError("Error at loading XML file", message); } } @@ -160,7 +156,7 @@ bool QtXMLHandler::characters (const QString & ch ) bool QtXMLHandler::error (const QXmlParseException & exception ) { std::string message = exception.message().toStdString(); - TKLog::printf("Error while reading XML file!! \n%s", message.c_str()); + TKLog::printf(TKLog::WARNING, "Error while reading XML file!! \n%s", message.c_str()); return true; } @@ -168,12 +164,7 @@ bool QtXMLHandler::fatalError(const QXmlParseException & exception ) { QString message = QString().sprintf("ERROR while reading XML file!! file.%s (line:%d) \nReason:%s", mFilePath.toLocal8Bit().data(), exception.lineNumber(), exception.message().toLocal8Bit().data()); - TKLog::printf("%s", message.toLocal8Bit().data()); - if (dnGlobal()->updateErrorStatus(DNGlobal::ERROR)) - { - dnGlobal()->setMessage1("Error at loading XML file"); - dnGlobal()->setMessage2(message.toStdString()); - } + dnNotifyError("Error at loading XML file", message.toStdString()); return true; }