add more details to messages logged in entity scripts

This commit is contained in:
Heather Anderson 2020-08-30 23:47:54 -07:00
parent c099cfe904
commit ee43cfd892

View file

@ -109,6 +109,7 @@ int functionSignatureMetaID = qRegisterMetaType<QScriptEngine::FunctionSignature
int scriptEnginePointerMetaID = qRegisterMetaType<ScriptEnginePointer>();
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<ScriptEngine*>(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<ScriptEngine*>(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();
}