0

I'm trying to use std::source_location in combination with a logging mechanism. However it turns out that std::source_location points to the wrong location.

For reference the compiler explorer link https://godbolt.org/z/Wx3har1zG

The problem was that I could figure out a way of combining default args and va args. That's why I had to split it into 2 separate functions.

Now std::source_location points to the log() function and not the origin of the log. Could you help me out here? How can I combine std::source_location and va args (without having to wrap it all into macros).

#include <source_location>
#include <string>
#include <string_view>
#include <format>
#include <iostream>
#include <fmt/format.h>



template <typename... Args>
bool log(fmt::format_string<Args...> fmt, Args&&... args);


bool _log(std::string strFormatedDebugMessage, const std::source_location loc = std::source_location::current());




template<typename ...Args>
bool log( fmt::format_string<Args...> fmt, Args&&... args)
  {
    fmt::basic_string_view<char> asStringView = fmt;
    auto asStdStringView = std::string_view(asStringView.data());
    const bool bRes = _log(std::string(asStdStringView));
    return bRes;
  }


bool _log(std::string strFormatedDebugMessage, const std::source_location loc)
{
    //for debugging
    //this should point to line 43 44. infact it points to line 24
    std::cout << "Called from Line "<< loc.line() << std::endl;
    std::cout << "Log: " << strFormatedDebugMessage <<std::endl;

    //write to the actual hardware => might fail therfore return bool
    return true;
}


int main()
{
    bool bRes = log("Log with format {} {} value", "Some dummy", 1);
    bRes &= log("Log without format args");
    std::cout <<"Hello World" << bRes << std::endl;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
JHeni
  • 455
  • 3
  • 12
  • 1
    No time to test this or write up an answer, but... the [example on cppreference.com](https://en.cppreference.com/w/cpp/utility/source_location) uses a default function argument to force construction of the `std::source_location` at the call site, rather than inside the log function. – Thomas Aug 29 '23 at 15:05
  • 2
    It is showing the correct source location. The location of the call of function `_log` and `std::source_location::current()`. – Agent_L Aug 29 '23 at 15:11
  • 1
    Does https://stackoverflow.com/q/76646533/3966456 address your need? I asked that question. – Weijun Zhou Aug 29 '23 at 15:52
  • 2
    @WeijunZhou: [how-to-use-source-location-in-a-variadic-template-function/57547898](https://stackoverflow.com/questions/57547273/how-to-use-source-location-in-a-variadic-template-function/57547898) is more appropriate, OP has less constraints than your version. – Jarod42 Aug 29 '23 at 19:48
  • 1
    *"How can I combine `std::source_location` and va args"*. You don't use va args (C-ellipsis, but variadic template :-) ). – Jarod42 Aug 29 '23 at 20:24
  • 1
    [Demo](https://godbolt.org/z/h477WYj65) using duplicated solution – Jarod42 Aug 29 '23 at 20:26
  • thx guys for all of your help. I'm trying out different proposed solutions now :) – JHeni Aug 30 '23 at 14:48

0 Answers0