History

add more details to messages logged in entity scripts

Heather Anderson committed 6 years ago

Commit ee43cfd1 parent c099cfe1 file · +40 −5Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 1 file). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+40−5
@@ -109,6 +109,7 @@ int functionSignatureMetaID = qRegisterMetaType<QScriptEngine::FunctionSignature · 108 unmodified lines
109109 int scriptEnginePointerMetaID = qRegisterMetaType<ScriptEnginePointer>();
110110
111111 static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine) {
112+ // assemble the message by concatenating our arguments
112113 QString message = "";
113114 for (int i = 0; i < context->argumentCount(); i++) {
114115 if (i > 0) {
@@ -117,13 +118,47 @@ static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine) { · 2 unmodified lines
117118 message += context->argument(i).toString();
118119 }
119120
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) {
125124 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();
126133 }
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));
127162
128163 return QScriptValue();
129164 }