There may be times when your app does not work as expected during initial design, testing, and deployment. When this happens, it can be useful to implement ways to present and diagnose errors.
Console logs
When your app is behaving in ways that you don't expect or understand, it can be helpful to refer to a console to observe what the app is doing internally. AppStudio contains a built-in console (see Capture console output for more information), and Qt Creator displays console messages when an app is run within it. Additional console information can be supplied through a line of code, either to provide a property that might be useful or as a signpost for a function with complex output that might not be behaving correctly.
The following code sample prints a line in the console log displaying the new filepath
when it's changed:
TextField {
Layout.fillWidth: true
text: fileFolder.path
onEditingFinished: {
fileFolder.path = text.trim();
console.log("New filepath:", fileFolder.path);
}
}
For more involved console support, both in the form of live output and log files, the AppStudio AppFramework provides a Logging component with a series of functions intended to aid in and configure console logs.
To start a console log, set the enabled
property in the Logging component to true
. This can be set in the background to log the entire session, or set by a switch or button to start logging only when requested. When a log is started, it refers to the output
property to determine the location and form where it's outputting. This destination can be either a file path, in which case, the app will create and output into a log file, or the URL of a syslog console, in which case, the app will output directly there. The formatting of log messages can be configured through the message
property.
The message
property uses a series of placeholders to structure information. To display desired information in console messages, include the following placeholders:
Placeholder | Description |
---|---|
%{appname} | QCoreApplication::applicationName>() |
%{category} | Logging category |
%{file} | Path to source file |
%{function} | Function |
%{line} | Line in source file |
%{message} | The actual message |
%{pid} | QCoreApplication::applicationPid() |
%{threadid} | The system-wide ID of current thread (if it can be obtained) |
%{qthreadptr} | A pointer to the current QThread (result of QThread::currentThread() |
%{type} | "debug", "warning", "critical", or "fatal" |
%{time process} | Time of the message, in seconds, since the process started |
%{time boot} | Time of the message, in seconds, since the system boot if that can be determined |
%{time [format]} | System time when the message occurred, formatted by passing the format to QDateTime::toString() |
Add error messages
There are also errors that you will be able to predict and recognize but cannot solve. This could be due to an unavoidable limitation of the app; for example, an app that requires constant access to an online service will not work in an environment with no internet access. This can be handled by displaying a message when the on
signal provided in many components is emitted. Some of these components also provide additional properties to inform the user in the event of an error, such as the NetworkRequest error
property.
The following code sample demonstrates how to introduce an error message when NetworkRequest fails, with the error message displaying the returned error. It also sets up the network request to fail by directing it to make an impossible request.
MessageDialog {
id: messageDialog
title: "Network error"
text: "Error code: " + networkRequest.errorCode + "\n" + networkRequest.errorText
}
NetworkRequest {
id: networkRequest
url: "http://appstudio.arcgis.com/images/index/introview.jpg2"
responseType: "json"
onError: {
messageDialog.open();
}
}
Button {
id: sendBtn
text: "Send Request"
onClicked: {
networkRequest.send();
}
}