Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Get file name, line number and function name in QML Javascript. How?

Get file name, line number and function name in QML Javascript. How?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 6 Posters 5.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bogong
    wrote on 18 Jan 2021, 11:29 last edited by
    #1

    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.

    J J 2 Replies Last reply 18 Jan 2021, 11:49
    1
    • B bogong
      18 Jan 2021, 11:29

      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.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 18 Jan 2021, 11:49 last edited by
      #2

      @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)
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • B bogong
        18 Jan 2021, 11:29

        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.

        J Offline
        J Offline
        JonB
        wrote on 18 Jan 2021, 11:52 last edited by
        #3

        @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:

        • https://stackoverflow.com/a/47105238/489865
        • https://stackoverflow.com/questions/50877998/how-to-get-filename-and-line-number-in-javascript-dynamically
        R 1 Reply Last reply 18 Jan 2021, 12:02
        2
        • J JonB
          18 Jan 2021, 11:52

          @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:

          • https://stackoverflow.com/a/47105238/489865
          • https://stackoverflow.com/questions/50877998/how-to-get-filename-and-line-number-in-javascript-dynamically
          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 18 Jan 2021, 12:02 last edited by
          #4

          @JonB
          i can confirm that parsing new Error().stack works also in QML, already used it myself
          Simply parse for \n, @ and : to get the info you want.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          B 1 Reply Last reply 18 Jan 2021, 12:13
          7
          • R raven-worx
            18 Jan 2021, 12:02

            @JonB
            i can confirm that parsing new Error().stack works also in QML, already used it myself
            Simply parse for \n, @ and : to get the info you want.

            B Offline
            B Offline
            bogong
            wrote on 18 Jan 2021, 12:13 last edited by
            #5

            @raven-worx Got it. Thx.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bogong
              wrote on 18 Jan 2021, 12:14 last edited by
              #6

              Issue solved.

              var err = new Error();
              console.log(inMessage,err.stack);
              
              1 Reply Last reply
              1
              • K Offline
                K Offline
                KH-219Design
                wrote on 18 Jan 2021, 18:41 last edited by
                #7

                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 invoke qSetMessagePattern, being sure to make "%{file}(%{line}):" part of the specified pattern:

                • https://github.com/219-design/qt-qml-project-template-with-ci/blob/441e072d50613bf/src/app/main.cc#L25

                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.

                www.219design.com
                Software | Electrical | Mechanical | Product Design

                1 Reply Last reply
                1
                • O Offline
                  O Offline
                  ondrejandrej
                  wrote on 26 Apr 2021, 07:20 last edited by
                  #8

                  @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?

                  1 Reply Last reply
                  1
                  • K Offline
                    K Offline
                    KH-219Design
                    wrote on 26 Apr 2021, 15:37 last edited by KH-219Design
                    #9

                    @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})

                    www.219design.com
                    Software | Electrical | Mechanical | Product Design

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved