With gcc projects, is there a way to get the filename (for logging) without a path?
-
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 wrongIs 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.
-
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
-
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.