Get file name, line number and function name in QML Javascript. How?
-
wrote on 18 Jan 2021, 11:29 last edited by
Hello all!
Is there any way to get file name, line number and function name in QML Javascript without using QDebug? The key point WITHOUT QDebug. I know how to get it in C++ via FILE, LINE, FUNCTION. I am seeking something like this C++ function but for QML/JavaScript. -
Hello all!
Is there any way to get file name, line number and function name in QML Javascript without using QDebug? The key point WITHOUT QDebug. I know how to get it in C++ via FILE, LINE, FUNCTION. I am seeking something like this C++ function but for QML/JavaScript.@bogong
I'm not entirely sure, and I would love, if someone has more insight here.What I do is using console.log() and print information that way for example:
function myFunction() { console.log("myFunction", this.toString(), this, objectName) }
-
Hello all!
Is there any way to get file name, line number and function name in QML Javascript without using QDebug? The key point WITHOUT QDebug. I know how to get it in C++ via FILE, LINE, FUNCTION. I am seeking something like this C++ function but for QML/JavaScript.wrote on 18 Jan 2021, 11:52 last edited by@bogong
I know nothing about QML. In JS it's not native, have a look at these "tricks" and see if they work for you/are suitable: -
@bogong
I know nothing about QML. In JS it's not native, have a look at these "tricks" and see if they work for you/are suitable:@JonB
i can confirm that parsingnew Error().stack
works also in QML, already used it myself
Simply parse for\n
,@
and:
to get the info you want. -
@JonB
i can confirm that parsingnew Error().stack
works also in QML, already used it myself
Simply parse for\n
,@
and:
to get the info you want.wrote on 18 Jan 2021, 12:13 last edited by@raven-worx Got it. Thx.
-
wrote on 18 Jan 2021, 12:14 last edited by
Issue solved.
var err = new Error(); console.log(inMessage,err.stack);
-
wrote on 18 Jan 2021, 18:41 last edited by
This
stack
tactic from @raven-worx looks very useful. I'm glad to have learned it just now.I'll come at this from a different angle, in case anyone was unaware of
qSetMessagePattern
.I have been using QML and logging with
console.log
, and I see the QML file name and line number attached to every output statement.In my
main
function, I invokeqSetMessagePattern
, being sure to make"%{file}(%{line}):"
part of the specified pattern:Then a simple call to
console.log
such as:console.log(fAwesomeFamily)
- used here: https://github.com/219-design/qt-qml-project-template-with-ci/blob/master/src/libstyles/imports/libstyles/Fonts.qml#L11
Produces the following level of detail when Qt prints it to stderr:
2021-01-03 18:58:56 [QT-debug][v-441e072d50][thr:18325]qml: qrc:/libstyles/Fonts.qml(15): Font Awesome 5 Free
For QML code, this works in both debug and release builds.
That stands in contrast to Qt logging (with "
"%{file}(%{line}):"
") in C++ code. My C++ Qt logging shows up as desired in a debug build, looking like"event_filter.cc(74):"
. But in release builds it shows"unknown(0):"
. However, with QML logging I see the filename and line of the QML source no matter whether the build was debug or release. -
wrote on 26 Apr 2021, 07:20 last edited by
@KH-219Design said in Get file name, line number and function name in QML Javascript. How?:
qSetMessagePattern
Thank you for this great tip!
BTW the file name is printed with the whole path, which is taking a lot of space. Is there a way to print file name only?
-
wrote on 26 Apr 2021, 15:37 last edited by KH-219Design
@ondrejandrej said in Get file name, line number and function name in QML Javascript. How?:
Is there a way to print file name only?
Yes and no.
These are the tokens accepted by
qSetMessagePattern
: https://doc.qt.io/qt-5/qtglobal.html#qSetMessagePatternFor filename, there is only one choice (the "%{file}" that we have already seen). There isn't anything like "shortfile" or "filebasename" from what I can see.
So I can think of two options.
(1) you can write your own
QtMessageHandler
function, as done in this example: https://github.com/qt/qtbase/blob/fcea8e7aa8a/examples/vulkan/hellovulkanwidget/main.cpp#L79Anything that is logged will get routed to your handler function, and you can get the filename and all the other info from the
QMessageLogContext
passed to the handler, and you can then have total control over logging.(2) Keep the logging as you have it now (with long file name and file path), and write a separate little bash script (or perl script, or python script) to filter the output whenever you pipe logging info to your script.
I guess (2) might not be great if you are looking at the output inside a debugger.
Nonetheless, here is an example of what I mean in (2), as demonstrated at a bash prompt...
If I launch my app like so:
./app
This long line of output appears
2021-04-26 08:33:52 [QT-info][thr:1991]/opt/repositories/client/self/heory/gits/heory/src/app/event_filter.cc(76): this GUI thread is running unblocked.
But if I launch it and pipe output through a
sed
filter:./app |& sed 's/opt\/repositories\/client\/self\/heory\/gits//'
That same log line will shrink to:
2021-04-26 08:33:46 [QT-info][thr:1951]//heory/src/app/event_filter.cc(76): this GUI thread is running unblocked.
(Sidenote: in case the
|&
that I like to call pipepersand is as new to anyone as it is recently new to me, here is a reference on piping in bash with {pipe,ampersand})