Capture __LINE__ and __FILE__ without #define

前端 未结 2 802
生来不讨喜
生来不讨喜 2020-12-07 01:02

Trying to determine a \"modern\" implementation for the following C-style code:

#define logError(...) log(__FILE__, __LINE__, __VA_ARGS__)

相关标签:
2条回答
  • 2020-12-07 01:42

    Actually the preprocessor is the only choice when you want to work with line numbers and filenames.

    For the compiler it's not possible to use line numbers and filenames as arguments for function calls (or storing them in a variable).

    In my company we had the exactly same issue with logging. We ended up with an external script scanning the source files and then building proper functions to call.

    0 讨论(0)
  • 2020-12-07 01:52

    Macros are indeed your only choice, at least until std::source_location makes it into the standard and fulfills your wish.

    0 讨论(0)
提交回复
热议问题