From ee43cfd89207a1b6e4e4a9bde05680a200bc25fa Mon Sep 17 00:00:00 2001 From: Heather Anderson Date: Sun, 30 Aug 2020 23:47:54 -0700 Subject: [PATCH] add more details to messages logged in entity scripts --- libraries/script-engine/src/ScriptEngine.cpp | 45 +++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 3b2a122e71..6a85a9d8ac 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -109,6 +109,7 @@ int functionSignatureMetaID = qRegisterMetaType(); static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine) { + // assemble the message by concatenating our arguments QString message = ""; for (int i = 0; i < context->argumentCount(); i++) { if (i > 0) { @@ -117,14 +118,48 @@ static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine) { message += context->argument(i).toString(); } - if (ScriptEngine *scriptEngine = qobject_cast(engine)) { - scriptEngine->print(message); - // prefix the script engine name to help disambiguate messages in the main debug log - qCDebug(scriptengine_script, "[%s] %s", qUtf8Printable(scriptEngine->getFilename()), qUtf8Printable(message)); - } else { + // was this generated by a script engine? If we don't recognize it then send the message and exit + ScriptEngine* scriptEngine = qobject_cast(engine); + if (scriptEngine == nullptr) { qCDebug(scriptengine_script, "%s", qUtf8Printable(message)); + return QScriptValue(); } + // This message was sent by one of our script engines, let's try to see if we can find the source. + // Note that the first entry in the backtrace should be "print" and is somewhat useless to us + QScriptContext* userContext = context; + while (userContext != nullptr && QScriptContextInfo(userContext).functionType() == QScriptContextInfo::NativeFunction) { + userContext = userContext->parentContext(); + } + QString location; + if (userContext != nullptr) { + QScriptContextInfo contextInfo(userContext); + QString fileName = contextInfo.fileName(); + int lineNumber = contextInfo.lineNumber(); + QString functionName = contextInfo.functionName(); + + location = functionName; + if (lineNumber != -1) { + location = QString("%1:%2").arg(location).arg(lineNumber); + } + if (!fileName.isEmpty()) { + if (location.isEmpty()) { + location = fileName; + } else { + location = QString("%1 at %2").arg(location).arg(fileName); + } + } + } + if (location.isEmpty()) { + location = scriptEngine->getFilename(); + } + + // give the script engine a chance to notify the system about this message + scriptEngine->print(message); + + // send the message to debug log + qCDebug(scriptengine_script, "[%s] %s", qUtf8Printable(location), qUtf8Printable(message)); + return QScriptValue(); }