As a project I am working on is getting bigger I am getting tired of writing comprehensive log messages, needed to find out what went wrong and where.
So it would be extremely useful if I can incorporate that information into the messages automatically. For C++ we have the convenient __FILE__, __LINE__ and __FUNCTION__ macros, but I don't seem to find any for QML.
Note that there is console.trace() which outputs a trace in the console in the following format:
onCompleted (qrc:/main.qml:72)
So it includes function, file and line, which is all I need, therefore I assume there already exists a way to get those. Natural, console.trace() doesn't really quite cut it, because it directly outputs to the console, whereas I need those as strings to incorporate in my log messages.
So is there any way to get those?
Naturally I don't want to get them in the actual QML source, but the same way console.trace() does - on the spot where my Log.error() is invoked, so I can just Log.error("some error") instead of Log.error("some error in " + file + ", at " + line + " while executing " + func) which will actually be even more tedious than writing the whole thing manually.
Alternatively, I'd also appreciate if someone can point me to the implementation of the qml console, because I combed through the entire qtdeclarative code base for "console" and found nothing.
Note that I already found this, however it isn't of use to me, because I need that for a specific subset of messages and don't want to override the default message handler for all output.
console.log, console.debug, console.info, console.warn and console.error can be used to print debugging information to the console. The output is generated using the qDebug, qWarning, qCritical methods in C++ (see also Debugging Techniques and how such functions are implemented, especially this internal function). Setting the environment variable QML_CONSOLE_EXTENDED also prints the source code location of the call. For example,
export QT_MESSAGE_PATTERN="[%{type}] %{appname} (%{file}:%{line}) - %{message}"
Now the output looks like this,

This link contains details about customizing the QT_MESSAGE_PATTERN environment variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With