Getting base name of the source file at compile time

后端 未结 14 2251
有刺的猬
有刺的猬 2020-12-14 10:07

I\'m using GCC; __FILE__ returns the current source file\'s entire path and name: /path/to/file.cpp. Is there a way to get just the file\'s name file.cpp<

14条回答
  •  一向
    一向 (楼主)
    2020-12-14 10:20

    What does your error logging macro do? I would presume at some point the macro eventually calls a function of some kind in order to do the logging, why not have the called function strip off the path component at runtime?

    #define LOG(message) _log(__FILE__, message)
    
    void _log(file, message)
    {
      #ifndef DEBUG
      strippath(file); // in some suitable way
      #endif
    
      cerr << "Log: " << file << ": " << message; // or whatever
    }
    

提交回复
热议问题