@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#qSetMessagePattern
For 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#L79
Anything 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})