Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. With gcc projects, is there a way to get the filename (for logging) without a path?
Forum Updated to NodeBB v4.3 + New Features

With gcc projects, is there a way to get the filename (for logging) without a path?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 604 Views 1 Watching
  • 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.
  • T Offline
    T Offline
    thierryhenry14
    wrote on last edited by
    #1

    When logging, my macros prefix the log line with __ FILE __ (and more, like function name and line) so when I write LOG("Oh no something went wrong") what's logged is actually "ClientConnection.cpp: L250 : functionName Oh no something went wrong". It's all compile-time macros so there's no runtime cost.

    Or at least that's what I wanted. In practice, the __ FILE__ macro includes the whole path to the source file as given by qmake to gcc, so the final result is annoyingly long:
    ../../myproject/libs/mycommlib/src/networking/messaging/ClientConnection.cpp: L250 : functionName Oh no something went wrong

    Is there a way to configure qmake to end up with a trimmed value of the __ FILE__ macro with just the filename? I've found cmake-related solutions posted online but none for qmake, has anyone managed to do the qmake equivalent?

    I'm aware of inferior solutions (like manually setting the filename with "#line", or using runtime string stripping operations), but I wouldn't use anything that wasn't both compile-time, and fully automatic like current __ FILE__ is.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You can use:

      • Q_FUNC_INFO - won't print file name but it will print function signature which is enough for log / debug usually (you know exactly where the log happens)
      • use custom format with qDebug by calling qSetMessagePattern(). This might have some runtime overhead, though

      (Z(:^

      1 Reply Last reply
      4
      • T Offline
        T Offline
        thierryhenry14
        wrote on last edited by
        #3

        After a few years, I found an answer here: https://stackoverflow.com/questions/31050113/how-to-extract-the-source-filename-without-path-and-suffix-at-compile-time

        GCC comes with built-in string manipulation functions that allow you to trim __FILE__ at compile time. Hope this helps someone who finds this.

        1 Reply Last reply
        1

        • Login

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