Initial script logging functions

This commit is contained in:
Karol Suprynowicz 2023-09-24 15:17:43 +02:00
parent 00bcf0d41f
commit ee980cbc5b
8 changed files with 151 additions and 10 deletions

View file

@ -34,7 +34,7 @@ QList<QString> ConsoleScriptingInterface::_groupDetails = QList<QString>();
ScriptValue ConsoleScriptingInterface::info(ScriptContext* context, ScriptEngine* engine) {
if (ScriptManager* scriptManager = engine->manager()) {
scriptManager->scriptInfoMessage(appendArguments(context));
scriptManager->scriptInfoMessage(appendArguments(context), context->currentFileName(), context->currentLineNumber());
}
return engine->nullValue();
}
@ -43,7 +43,7 @@ ScriptValue ConsoleScriptingInterface::log(ScriptContext* context, ScriptEngine*
QString message = appendArguments(context);
if (_groupDetails.count() == 0) {
if (ScriptManager* scriptManager = engine->manager()) {
scriptManager->scriptPrintedMessage(message);
scriptManager->scriptPrintedMessage(message, context->currentFileName(), context->currentLineNumber());
}
} else {
logGroupMessage(message, engine);
@ -53,28 +53,28 @@ ScriptValue ConsoleScriptingInterface::log(ScriptContext* context, ScriptEngine*
ScriptValue ConsoleScriptingInterface::debug(ScriptContext* context, ScriptEngine* engine) {
if (ScriptManager* scriptManager = engine->manager()) {
scriptManager->scriptPrintedMessage(appendArguments(context));
scriptManager->scriptPrintedMessage(appendArguments(context), context->currentFileName(), context->currentLineNumber());
}
return engine->nullValue();
}
ScriptValue ConsoleScriptingInterface::warn(ScriptContext* context, ScriptEngine* engine) {
if (ScriptManager* scriptManager = engine->manager()) {
scriptManager->scriptWarningMessage(appendArguments(context));
scriptManager->scriptWarningMessage(appendArguments(context), context->currentFileName(), context->currentLineNumber());
}
return engine->nullValue();
}
ScriptValue ConsoleScriptingInterface::error(ScriptContext* context, ScriptEngine* engine) {
if (ScriptManager* scriptManager = engine->manager()) {
scriptManager->scriptErrorMessage(appendArguments(context));
scriptManager->scriptErrorMessage(appendArguments(context), context->currentFileName(), context->currentLineNumber());
}
return engine->nullValue();
}
ScriptValue ConsoleScriptingInterface::exception(ScriptContext* context, ScriptEngine* engine) {
if (ScriptManager* scriptManager = engine->manager()) {
scriptManager->scriptErrorMessage(appendArguments(context));
scriptManager->scriptErrorMessage(appendArguments(context), context->currentFileName(), context->currentLineNumber());
}
return engine->nullValue();
}

View file

@ -57,6 +57,13 @@ public:
virtual int argumentCount() const = 0;
virtual ScriptValue argument(int index) const = 0;
virtual QStringList backtrace() const = 0;
// Name of the file in which message was generated. Empty string when no file name is available.
virtual int currentLineNumber() const = 0;
// Number of the line on which message was generated. -1 if there line number is not available.
virtual QString currentFileName() const = 0;
virtual ScriptValue callee() const = 0;
virtual ScriptEnginePointer engine() const = 0;
virtual ScriptFunctionContextPointer functionContext() const = 0;

View file

@ -573,21 +573,38 @@ void ScriptManager::loadURL(const QUrl& scriptURL, bool reload) {
void ScriptManager::scriptErrorMessage(const QString& message) {
qCCritical(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));
emit errorMessage(message, getFilename());
if (!currentEntityIdentifier.isInvalidID()) {
// TODO: add line number and proper file name
//if (engine() && engine()->currentContext() && engine()->currentContext()->)
emit errorEntityMessage(message, getFilename(), currentEntityIdentifier);
}
}
void ScriptManager::scriptWarningMessage(const QString& message) {
qCWarning(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));
emit warningMessage(message, getFilename());
if (!currentEntityIdentifier.isInvalidID()) {
// TODO: add line number and proper file name
emit warningEntityMessage(message, getFilename(), currentEntityIdentifier);
}
}
void ScriptManager::scriptInfoMessage(const QString& message) {
qCInfo(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));
emit infoMessage(message, getFilename());
if (!currentEntityIdentifier.isInvalidID()) {
// TODO: add line number and proper file name
emit infoEntityMessage(message, getFilename(), currentEntityIdentifier);
}
}
void ScriptManager::scriptPrintedMessage(const QString& message) {
qCDebug(scriptengine, "[%s] %s", qUtf8Printable(getFilename()), qUtf8Printable(message));
emit printedMessage(message, getFilename());
if (!currentEntityIdentifier.isInvalidID()) {
// TODO: add line number and proper file name
emit printedEntityMessage(message, getFilename(), currentEntityIdentifier);
}
}
void ScriptManager::clearDebugLogWindow() {

View file

@ -1074,8 +1074,10 @@ public:
* Emits errorMessage()
*
* @param message Message to send to the log
* @param fileName Name of the file in which message was generated. Empty string when no file name is available.
* @param lineNumber Number of the line on which message was generated. -1 if there line number is not available.
*/
void scriptErrorMessage(const QString& message);
void scriptErrorMessage(const QString& message, const QString& fileName, int lineNumber);
/**
* @brief Logs a script warning message and emits an warningMessage event
@ -1083,8 +1085,10 @@ public:
* Emits warningMessage()
*
* @param message Message to send to the log
* @param fileName Name of the file in which message was generated. Empty string when no file name is available.
* @param lineNumber Number of the line on which message was generated. -1 if there line number is not available.
*/
void scriptWarningMessage(const QString& message);
void scriptWarningMessage(const QString& message, const QString& fileName, int lineNumber);
/**
* @brief Logs a script info message and emits an infoMessage event
@ -1092,8 +1096,10 @@ public:
* Emits infoMessage()
*
* @param message Message to send to the log
* @param fileName Name of the file in which message was generated. Empty string when no file name is available.
* @param lineNumber Number of the line on which message was generated. -1 if there line number is not available.
*/
void scriptInfoMessage(const QString& message);
void scriptInfoMessage(const QString& message, const QString& fileName, int lineNumber);
/**
* @brief Logs a script printed message and emits an printedMessage event
@ -1102,9 +1108,11 @@ public:
* Emits printedMessage()
*
* @param message Message to send to the log
* @param fileName Name of the file in which message was generated. Empty string when no file name is available.
* @param lineNumber Number of the line on which message was generated. -1 if there line number is not available.
*/
void scriptPrintedMessage(const QString& message);
void scriptPrintedMessage(const QString& message, const QString& fileName, int lineNumber);
/**
* @brief Clears the debug log window
@ -1321,6 +1329,50 @@ signals:
*/
void infoMessage(const QString& message, const QString& scriptName);
/**
* @brief Triggered when a client side entity script prints a message to the program log
*
* @param message
* @param fileName Name of the file in which message was generated.
* @param lineNumber Number of the line on which message was generated.
* @param entityID
*/
void printedEntityMessage(const QString& message, const QString& fileName, int lineNumber, const EntityItemID& entityID);
/**
* @brief Triggered when a client side entity script generates an error
*
* @param message
* @param fileName Name of the file in which message was generated.
* @param lineNumber Number of the line on which message was generated.
* @param entityID
*/
void errorEntityMessage(const QString& message, const QString& fileName, int lineNumber, const EntityItemID& entityID);
/**
* @brief Triggered when a client side entity script generates a warning
*
* @param message
* @param fileName Name of the file in which message was generated.
* @param lineNumber Number of the line on which message was generated.
* @param entityID
*/
void warningEntityMessage(const QString& message, const QString& fileName, int lineNumber, const EntityItemID& entityID);
/**
* @brief Triggered when a client side entity script generates an information message
*
* @param message
* @param fileName Name of the file in which message was generated.
* @param lineNumber Number of the line on which message was generated.
* @param entityID
*/
void infoEntityMessage(const QString& message, const QString& fileName, int lineNumber, const EntityItemID& entityID);
/**
* @brief Triggered when the running state of the script changes, e.g., from running to stopping.

View file

@ -35,6 +35,10 @@
connect(_manager, &ScriptManager::printedMessage, this, &ScriptManagerScriptingInterface::printedMessage);
connect(_manager, &ScriptManager::errorMessage, this, &ScriptManagerScriptingInterface::errorMessage);
connect(_manager, &ScriptManager::warningMessage, this, &ScriptManagerScriptingInterface::warningMessage);
connect(_manager, &ScriptManager::infoEntityMessage, this, &ScriptManagerScriptingInterface::infoEntityMessage);
connect(_manager, &ScriptManager::printedEntityMessage, this, &ScriptManagerScriptingInterface::printedEntityMessage);
connect(_manager, &ScriptManager::errorEntityMessage, this, &ScriptManagerScriptingInterface::errorEntityMessage);
connect(_manager, &ScriptManager::warningEntityMessage, this, &ScriptManagerScriptingInterface::warningEntityMessage);
connect(_manager, &ScriptManager::infoMessage, this, &ScriptManagerScriptingInterface::infoMessage);
connect(_manager, &ScriptManager::runningStateChanged, this, &ScriptManagerScriptingInterface::runningStateChanged);
connect(_manager, &ScriptManager::clearDebugWindow, this, &ScriptManagerScriptingInterface::clearDebugWindow);

View file

@ -644,6 +644,53 @@ signals:
*/
void infoMessage(const QString& message, const QString& scriptName);
/*@jsdoc
* Triggered when a client side entity script prints a message to the program log via {@link print}, {@link Script.print},
* {@link console.log}, {@link console.debug}, {@link console.group}, {@link console.groupEnd}, {@link console.time}, or
* {@link console.timeEnd}.
* @function Script.printedMessage
* @param {string} message - The message.
* @param {string} fileName - Name of the file in which message was generated. Empty string when no file name is available.
* @param {number} lineNumber - Number of the line on which message was generated. -1 if there line number is not available.
* @param {Uuid} entityID - Entity ID.
* @returns {Signal}
*/
void printedEntityMessage(const QString& message, const QString& fileName, int lineNumber, const EntityItemID& entityID);
/*@jsdoc
* Triggered when a client side entity script generates an error, {@link console.error} or {@link console.exception} is called, or
* {@link console.assert} is called and fails.
* @function Script.errorMessage
* @param {string} message - The error message.
* @param {string} fileName - Name of the file in which message was generated. Empty string when no file name is available.
* @param {number} lineNumber - Number of the line on which message was generated. -1 if there line number is not available.
* @param {Uuid} entityID - Entity ID.
* @returns {Signal}
*/
void errorEntityMessage(const QString& message, const QString& fileName, int lineNumber, const EntityItemID& entityID);
/*@jsdoc
* Triggered when a client side entity script generates a warning or {@link console.warn} is called.
* @function Script.warningMessage
* @param {string} message - The warning message.
* @param {string} fileName - Name of the file in which message was generated. Empty string when no file name is available.
* @param {number} lineNumber - Number of the line on which message was generated. -1 if there line number is not available.
* @param {Uuid} entityID - Entity ID.
* @returns {Signal}
*/
void warningEntityMessage(const QString& message, const QString& fileName, int lineNumber, const EntityItemID& entityID);
/*@jsdoc
* Triggered when a client side entity script generates an information message or {@link console.info} is called.
* @function Script.infoMessage
* @param {string} message - The information message.
* @param {string} fileName - Name of the file in which message was generated. Empty string when no file name is available.
* @param {number} lineNumber - Number of the line on which message was generated. -1 if there line number is not available.
* @param {Uuid} entityID - Entity ID.
* @returns {Signal}
*/
void infoEntityMessage(const QString& message, const QString& fileName, int lineNumber, const EntityItemID& entityID);
/*@jsdoc
* Triggered when the running state of the script changes, e.g., from running to stopping.
* @function Script.runningStateChanged

View file

@ -45,6 +45,13 @@ public: // ScriptContext implementation
virtual int argumentCount() const override;
virtual ScriptValue argument(int index) const override;
virtual QStringList backtrace() const override;
// Name of the file in which message was generated. Empty string when no file name is available.
virtual int currentLineNumber() const override;
// Number of the line on which message was generated. -1 if there line number is not available.
virtual QString currentFileName() const override;
virtual ScriptValue callee() const override;
virtual ScriptEnginePointer engine() const override;
virtual ScriptFunctionContextPointer functionContext() const override;

View file

@ -56,6 +56,13 @@ public: // ScriptContext implementation
virtual int argumentCount() const override { return _parent->argumentCount(); }
virtual ScriptValue argument(int index) const override { return _parent->argument(index); }
virtual QStringList backtrace() const override { return _parent->backtrace(); }
// Name of the file in which message was generated. Empty string when no file name is available.
virtual int currentLineNumber() const override { return _parent->currentLineNumber(); }
// Number of the line on which message was generated. -1 if there line number is not available.
virtual QString currentFileName() const override { return _parent->currentFileName(); }
virtual ScriptValue callee() const override { return _parent->callee(); }
virtual ScriptEnginePointer engine() const override { return _parent->engine(); }
virtual ScriptFunctionContextPointer functionContext() const override { return _parent->functionContext(); }