获取当前文件函数名、行号
#include <iostream>
#include <string>
#include <source_location>
// compile by using: /std:c++20 (or later)
void log(std::string message)
{
const std::source_location& location = std::source_location::current();
std::cout << "信息:"
<< location.file_name() << ':'
<< location.line() << ' '
<< message << '\n';
}
int main()
{
log("Hello world!");
return 0;
}
运行结果