Is there a way to get the line number with out a macro in c++?

前端 未结 2 491
说谎
说谎 2021-01-28 05:16

I am writing a logger and I would like the logger to also record which line, function, and file called it. I have tried #define my_logger(format, ...) _log(format, __LINE_

相关标签:
2条回答
  • 2021-01-28 05:52

    There cannot be such macro as __LINE_FUNCTION_WAS_CALLED_FROM__. There's no way it could work.

    Since C++20, there is a way to get the line, function, etc. into a normal function by passing default initialised std::source_location:

    void log(std::string_view message,
             std::source_location location = std::source_location::current());
    

    However, since it relies on a defaulted parameter, variadic arguments are a bit tricky to implement: How to use source_location in a variadic template function?

    Prior to C++20, a macro is your only portable option.


    I see that you call a function named _log in your macro. That identifier is reserved to the language implementation in the global namespace. You probably should give the function another name to avoid undefined behaviour.

    log is also reserved in the global namespace. You should declare all your names in a custom namespace anyway (except that custom namespace naturally).


    [the macro is] ... not the most portable

    If you were to replace __PRETTY_FUNCTION__ with __func__, then the macro would be portable to all standard conforming compilers - although the exact string given by __func__ may differ between implementations.

    0 讨论(0)
  • 2021-01-28 06:04

    Is there a way where I can declare a normal function like void my_logger(const char* format, ...) and in the function definition have it know the line it was called from with something equivalent to __LINE_FUNCTION_WAS_CALLED_FROM__ if such macro existed?

    Not in general.

    On Linux x86-64, in 2020, with all your (or other) C++ code (C++11 or better) compiled with DWARF debug information (so if using a recent GCC, with g++ -g -Wall and if using a recent Clang, with clang++ -g -Wall), you might use Ian Taylor's libbacktrace library.

    Also, a good optimizing compiler is allowed to inline expand or unroll loop (and actually g++ -O2 or clang++ -O2 will do both, and so does Microsoft compilers), and then your __LINE_FUNCTION_WAS_CALLED_FROM__ makes no sense.

    As an open source example, RefPerSys is using it to show backtrace and part of call stacks. Disclaimer: RefPerSys is my own project.

    Be aware of ASLR related to shared libraries. And plugins loaded with dlopen(3) add complexity to the picture.

    At a minimum it needs to work on GCC and really should be cross-platform.

    It cannot be cross-platform. Some architectures (e.g. IBM Z series) have different calling conventions, and you'll need efforts (or pay them) to port libbacktrace to them.

    You might consider debugging on some systems, then porting your code to others, and you should consider using frameworks like libSFML or Qt for your game engine. My opinion is to recommend debugging your code on Linux (to make it correct) and later porting it to Windows.

    If you need cross-compilation, things become harder. you may also consider metaprogramming approaches: writing your C++ code generator or using other ones (e.g. like SWIG or ANTLR)

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