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;
}