mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 03:44:02 +02:00
Complete the functionality of console.trace().
This commit is contained in:
parent
5df3b88c85
commit
5c324264a2
5 changed files with 21 additions and 114 deletions
|
@ -52,46 +52,10 @@ void ConsoleScriptingInterface::error(QString message) {
|
|||
|
||||
void ConsoleScriptingInterface::exception(QString message) {
|
||||
if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) {
|
||||
scriptEngine->logConsoleException(message);
|
||||
scriptEngine->scriptErrorMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool ConsoleScriptingInterface:: hasCorrectSyntax(QScriptProgram& program) {
|
||||
const auto syntaxCheck = QScriptEngine::checkSyntax(program.sourceCode());
|
||||
if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) {
|
||||
const auto error = syntaxCheck.errorMessage();
|
||||
const auto line = QString::number(syntaxCheck.errorLineNumber());
|
||||
const auto column = QString::number(syntaxCheck.errorColumnNumber());
|
||||
const auto message = QString("[SyntaxError] %1 in %2:%3(%4)").arg(error, program.fileName(), line, column);
|
||||
qCritical() << qPrintable(message);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QString ConsoleScriptingInterface::hadUncaughtExceptions(QScriptEngine& engine, QString& fileName) {
|
||||
log("4");
|
||||
//if (engine.hasUncaughtException()) {
|
||||
const auto backtrace = engine.uncaughtExceptionBacktrace();
|
||||
const auto exception = engine.uncaughtException().toString();
|
||||
const auto line = QString::number(engine.uncaughtExceptionLineNumber());
|
||||
engine.clearExceptions();
|
||||
log("5");
|
||||
static const QString SCRIPT_EXCEPTION_FORMAT = "[UncaughtException] %1 in %2:%3";
|
||||
auto message = QString(SCRIPT_EXCEPTION_FORMAT).arg(exception, fileName, line);
|
||||
if (!backtrace.empty()) {
|
||||
static const auto lineSeparator = "\n ";
|
||||
message += QString("\n[Backtrace]%1%2").arg(lineSeparator, backtrace.join(lineSeparator));
|
||||
}
|
||||
// qCritical() << qPrintable(message);
|
||||
log("6");
|
||||
return message;
|
||||
//}
|
||||
//return false;
|
||||
}
|
||||
|
||||
|
||||
void ConsoleScriptingInterface::time(QString labelName) {
|
||||
QDateTime _currentTime = QDateTime::currentDateTime().toUTC();
|
||||
_listOfTimeValues.insert(labelName, _currentTime.toUTC());
|
||||
|
@ -106,10 +70,8 @@ void ConsoleScriptingInterface::timeEnd(QString labelName) {
|
|||
|
||||
//Check if not exist items in list
|
||||
if (!_labelInList) {
|
||||
if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) {
|
||||
message = "No such label found " + labelName;
|
||||
scriptEngine->scriptErrorMessage(message);
|
||||
}
|
||||
message = "No such label found " + labelName;
|
||||
error(message);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -123,14 +85,10 @@ void ConsoleScriptingInterface::timeEnd(QString labelName) {
|
|||
if (_dateTimeOfLabel.toString(TIME_FORMAT) == _currentDateTimeValue.toString(TIME_FORMAT)) {
|
||||
millisecondsDiff = _dateTimeOfLabel.msecsTo(_currentDateTimeValue);
|
||||
message = QString::number(millisecondsDiff) + " ms";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
message = secondsToString(_dateTimeOfLabel.secsTo(_currentDateTimeValue));
|
||||
}
|
||||
|
||||
if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) {
|
||||
scriptEngine->scriptPrintedMessage("Time : " + message);
|
||||
}
|
||||
log("Time : " + message);
|
||||
}
|
||||
|
||||
QString ConsoleScriptingInterface::secondsToString(qint64 seconds)
|
||||
|
@ -147,24 +105,24 @@ void ConsoleScriptingInterface::asserts(bool condition, QString message) {
|
|||
QString assertFailed = "Assertion failed";
|
||||
if (message.isEmpty()) {
|
||||
message = assertFailed;
|
||||
}
|
||||
else {
|
||||
if (typeid(message) != typeid(QString)) {
|
||||
} else {
|
||||
QString inputType = typeid(message).name();
|
||||
if (!inputType.compare("QString")) {
|
||||
message = assertFailed;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
message = assertFailed + " " + message;
|
||||
}
|
||||
}
|
||||
if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) {
|
||||
scriptEngine->scriptErrorMessage(message);
|
||||
}
|
||||
error(message);
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleScriptingInterface::trace() {
|
||||
if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) {
|
||||
scriptEngine->logTraceException();
|
||||
void ConsoleScriptingInterface::trace() {
|
||||
const auto lineSeparator = "\n ";
|
||||
if (ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine())) {
|
||||
QStringList backtrace = scriptEngine->currentContext()->backtrace();
|
||||
auto message = QString("\n[Backtrace]%1%2").arg(lineSeparator, backtrace.join(lineSeparator));
|
||||
scriptEngine->scriptPrintedMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -145,17 +145,6 @@ QString encodeEntityIdIntoEntityUrl(const QString& url, const QString& entityID)
|
|||
return url + " [EntityID:" + entityID + "]";
|
||||
}
|
||||
|
||||
void ScriptEngine::logConsoleException(QString message) {
|
||||
this->raiseConsoleException(this->makeError(message));
|
||||
}
|
||||
|
||||
QString ScriptEngine::logTraceException() {
|
||||
QScriptValue value = this->raiseConsoleTraceException(this->makeError(""));
|
||||
auto trace = formatTrace(value, _enableExtendedJSExceptions.get());
|
||||
scriptInfoMessage(trace);
|
||||
return trace;
|
||||
}
|
||||
|
||||
QString ScriptEngine::logException(const QScriptValue& exception) {
|
||||
auto message = formatException(exception, _enableExtendedJSExceptions.get());
|
||||
scriptErrorMessage(message);
|
||||
|
|
|
@ -147,9 +147,7 @@ public:
|
|||
/// to run... NOTE - this is used by Application currently to load the url. We don't really want it to be exposed
|
||||
/// to scripts. we may not need this to be invokable
|
||||
void loadURL(const QUrl& scriptURL, bool reload);
|
||||
bool hasValidScriptSuffix(const QString& scriptFileName);
|
||||
void logConsoleException(QString message);
|
||||
QString logTraceException();
|
||||
bool hasValidScriptSuffix(const QString& scriptFileName);
|
||||
Q_INVOKABLE QString getContext() const;
|
||||
Q_INVOKABLE bool isClientScript() const { return _context == CLIENT_SCRIPT; }
|
||||
Q_INVOKABLE bool isEntityClientScript() const { return _context == ENTITY_CLIENT_SCRIPT; }
|
||||
|
|
|
@ -22,7 +22,6 @@
|
|||
#include "Profile.h"
|
||||
|
||||
const QString BaseScriptEngine::SCRIPT_EXCEPTION_FORMAT { "[%0] %1 in %2:%3" };
|
||||
const QString BaseScriptEngine::SCRIPT_TRACE_FORMAT{ "[%0] %1" };
|
||||
const QString BaseScriptEngine::SCRIPT_BACKTRACE_SEP { "\n " };
|
||||
|
||||
bool BaseScriptEngine::IS_THREADSAFE_INVOCATION(const QThread *thread, const QString& method) {
|
||||
|
@ -160,7 +159,7 @@ QScriptValue BaseScriptEngine::cloneUncaughtException(const QString& extraDetail
|
|||
#endif
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
QString BaseScriptEngine::formatException(const QScriptValue& exception, bool includeExtendedDetails) {
|
||||
if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
|
||||
return QString();
|
||||
|
@ -193,27 +192,6 @@ QString BaseScriptEngine::formatException(const QScriptValue& exception, bool in
|
|||
return result;
|
||||
}
|
||||
|
||||
QString BaseScriptEngine::formatTrace(const QScriptValue& value, bool includeExtendedDetails) {
|
||||
if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
|
||||
return QString();
|
||||
}
|
||||
QString note{ "TRACE" };
|
||||
QString result;
|
||||
|
||||
if (!value.isObject()) {
|
||||
return result;
|
||||
}
|
||||
const auto fileName = value.property("fileName").toString();
|
||||
const auto lineNumber = value.property("lineNumber").toString();
|
||||
const auto stacktrace = value.property("stack").toString();
|
||||
|
||||
result = QString(SCRIPT_TRACE_FORMAT).arg(fileName, lineNumber);
|
||||
if (!stacktrace.isEmpty()) {
|
||||
result += QString("\n[Trace]%1%2").arg(SCRIPT_BACKTRACE_SEP).arg(stacktrace);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool BaseScriptEngine::raiseException(const QScriptValue& exception) {
|
||||
if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
|
||||
return false;
|
||||
|
@ -230,18 +208,6 @@ bool BaseScriptEngine::raiseException(const QScriptValue& exception) {
|
|||
return false;
|
||||
}
|
||||
|
||||
void BaseScriptEngine::raiseConsoleException(const QScriptValue& exception) {
|
||||
if (currentContext()) {
|
||||
currentContext()->throwValue(makeError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
QScriptValue BaseScriptEngine::raiseConsoleTraceException(const QScriptValue& exception) {
|
||||
if (currentContext()) {
|
||||
return currentContext()->throwValue(makeError(exception));
|
||||
}
|
||||
}
|
||||
|
||||
bool BaseScriptEngine::maybeEmitUncaughtException(const QString& debugHint) {
|
||||
if (!IS_THREADSAFE_INVOCATION(thread(), __FUNCTION__)) {
|
||||
return false;
|
||||
|
|
|
@ -20,8 +20,7 @@
|
|||
class BaseScriptEngine : public QScriptEngine, public QEnableSharedFromThis<BaseScriptEngine> {
|
||||
Q_OBJECT
|
||||
public:
|
||||
static const QString SCRIPT_EXCEPTION_FORMAT;
|
||||
static const QString SCRIPT_TRACE_FORMAT;
|
||||
static const QString SCRIPT_EXCEPTION_FORMAT;
|
||||
static const QString SCRIPT_BACKTRACE_SEP;
|
||||
|
||||
// threadsafe "unbound" version of QScriptEngine::nullValue()
|
||||
|
@ -31,8 +30,7 @@ public:
|
|||
|
||||
Q_INVOKABLE QScriptValue lintScript(const QString& sourceCode, const QString& fileName, const int lineNumber = 1);
|
||||
Q_INVOKABLE QScriptValue makeError(const QScriptValue& other = QScriptValue(), const QString& type = "Error");
|
||||
Q_INVOKABLE QString formatException(const QScriptValue& exception, bool includeExtendedDetails);
|
||||
Q_INVOKABLE QString formatTrace(const QScriptValue& val, bool includeExtendedDetails);
|
||||
Q_INVOKABLE QString formatException(const QScriptValue& exception, bool includeExtendedDetails);
|
||||
QScriptValue cloneUncaughtException(const QString& detail = QString());
|
||||
QScriptValue evaluateInClosure(const QScriptValue& locals, const QScriptProgram& program);
|
||||
|
||||
|
@ -41,9 +39,7 @@ public:
|
|||
|
||||
// if the currentContext() is valid then throw the passed exception; otherwise, immediately emit it.
|
||||
// note: this is used in cases where C++ code might call into JS API methods directly
|
||||
bool raiseException(const QScriptValue& exception);
|
||||
void raiseConsoleException(const QScriptValue& exception);
|
||||
QScriptValue raiseConsoleTraceException(const QScriptValue& exception);
|
||||
bool raiseException(const QScriptValue& exception);
|
||||
// helper to detect and log warnings when other code invokes QScriptEngine/BaseScriptEngine in thread-unsafe ways
|
||||
static bool IS_THREADSAFE_INVOCATION(const QThread *thread, const QString& method);
|
||||
signals:
|
||||
|
|
Loading…
Reference in a new issue