Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 1 file). Show all
Unified · Split
| @@ -109,6 +109,7 @@ int functionSignatureMetaID = qRegisterMetaType<QScriptEngine::FunctionSignature · 108 unmodified lines | ||
| 109 | 109 | int scriptEnginePointerMetaID = qRegisterMetaType<ScriptEnginePointer>(); |
| 110 | 110 | |
| 111 | 111 | static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine) { |
| 112 | + // assemble the message by concatenating our arguments | |
| 112 | 113 | QString message = ""; |
| 113 | 114 | for (int i = 0; i < context->argumentCount(); i++) { |
| 114 | 115 | if (i > 0) { |
| @@ -117,13 +118,47 @@ static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine) { · 2 unmodified lines | ||
| 117 | 118 | message += context->argument(i).toString(); |
| 118 | 119 | } |
| 119 | 120 | |
| 120 | − if (ScriptEngine *scriptEngine = qobject_cast<ScriptEngine*>(engine)) { | |
| 121 | − scriptEngine->print(message); | |
| 122 | − // prefix the script engine name to help disambiguate messages in the main debug log | |
| 123 | − qCDebug(scriptengine_script, "[%s] %s", qUtf8Printable(scriptEngine->getFilename()), qUtf8Printable(message)); | |
| 124 | − } else { | |
| 121 | + // was this generated by a script engine? If we don't recognize it then send the message and exit | |
| 122 | + ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine); | |
| 123 | + if (scriptEngine == nullptr) { | |
| 125 | 124 | qCDebug(scriptengine_script, "%s", qUtf8Printable(message)); |
| 125 | + return QScriptValue(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + // This message was sent by one of our script engines, let's try to see if we can find the source. | |
| 129 | + // Note that the first entry in the backtrace should be "print" and is somewhat useless to us | |
| 130 | + QScriptContext* userContext = context; | |
| 131 | + while (userContext != nullptr && QScriptContextInfo(userContext).functionType() == QScriptContextInfo::NativeFunction) { | |
| 132 | + userContext = userContext->parentContext(); | |
| 126 | 133 | } |
| 134 | + QString location; | |
| 135 | + if (userContext != nullptr) { | |
| 136 | + QScriptContextInfo contextInfo(userContext); | |
| 137 | + QString fileName = contextInfo.fileName(); | |
| 138 | + int lineNumber = contextInfo.lineNumber(); | |
| 139 | + QString functionName = contextInfo.functionName(); | |
| 140 | + | |
| 141 | + location = functionName; | |
| 142 | + if (lineNumber != -1) { | |
| 143 | + location = QString("%1:%2").arg(location).arg(lineNumber); | |
| 144 | + } | |
| 145 | + if (!fileName.isEmpty()) { | |
| 146 | + if (location.isEmpty()) { | |
| 147 | + location = fileName; | |
| 148 | + } else { | |
| 149 | + location = QString("%1 at %2").arg(location).arg(fileName); | |
| 150 | + } | |
| 151 | + } | |
| 152 | + } | |
| 153 | + if (location.isEmpty()) { | |
| 154 | + location = scriptEngine->getFilename(); | |
| 155 | + } | |
| 156 | + | |
| 157 | + // give the script engine a chance to notify the system about this message | |
| 158 | + scriptEngine->print(message); | |
| 159 | + | |
| 160 | + // send the message to debug log | |
| 161 | + qCDebug(scriptengine_script, "[%s] %s", qUtf8Printable(location), qUtf8Printable(message)); | |
| 127 | 162 | |
| 128 | 163 | return QScriptValue(); |
| 129 | 164 | } |