C++20 源代码信息类std::source_location 实现简易日志打印功能

文章介绍了传统通过预定义宏获取源代码信息的方法,如__LINE__、__FUNCTION__和__FILE__,并展示了如何通过宏简化日志系统的使用。然后,文章重点讨论了C++20引入的std::source_location类,这是一个表示源码位置的新工具,提供了获取行号、列号、文件名和函数名的成员函数,简化了获取代码上下文的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传统方法获取源代码信息

传统方法是使用预定义宏来获取,比如有__LINE____FUNCTION____FILE__
比如一个简易的日志系统,我们的log方法用于打印日志信息:

void log(PSTR level, PSTR file, PSTR func, int line, PSTR fmt, ...){
    char str[256] = {0};
    va_list arglst;
    va_start(arglst,fmt);
    vsnprintf(str,sizeof(str), fmt, arglst);
    printf("[%s][%s] %s in function %s() at line:%d : %s\n",__TIME__, level, file, func, line, str);
    va_end(arglst);
}

每次都要传入__FILE____FUNCTION____LINE__非常麻烦,如果你使用默认的参数的话,因为其实宏,在预编译阶段就会解析为函数声明的行。而且如果你使用可变参数的话,就无法使用默认参数了。

在这里插入图片描述
所以我们可以通过声明宏函数来解决,解析时直接解析为调用行。然后我们简易的日志类实现如下:

#include <stdio.h>
#include <stdarg.h>

using PSTR = const char*;

#define DEBUG_LOG(...) log("DEBUG", __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define WARNING_LOG(...) log("WARN", __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define ERROR_LOG(...) log("ERROR", __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)

void log(PSTR level, PSTR file, PSTR func, int line, PSTR fmt, ...){
    char str[256] = {0};
    va_list arglst;
    va_start(arglst,fmt);
    vsnprintf(str,sizeof(str), fmt, arglst);
    printf("[%s][%s] %s in function %s() at line:%d : %s\n",__TIME__, level, file, func, line, str);
    va_end(arglst);
}

int main(){
    int a,b;
    printf("Type your division expression:");
    scanf("%d/%d",&a,&b);
    if(b!=0)
        printf("Result: %f",a/b);
    else
	    WARNING_LOG("The expression \'%d/%d\' is not right, cause you cannot divide by 0.",a,b);
	return 0;
}

样例1:

Type your division expression:2/7
Result: 0.000000

样例2:

Type your division expression:8/0
[00:01:00][WARN] D:\SingleSources\CPP\3.cpp in function main() at line:26 : The expression '8/0' is not right, cause you cannot divide by 0.

std::source_location

source_location 类于C++20被引入,它表示关于源码的具体信息,它的结构以及使用都非常简单。

成员函数

我们使用其构造函数和一个静态成员函数current()创建实例。此外还有访问域的函数:

  • line():获取行号。
  • column():获取列号。
  • file_name():获取文件名。
  • function_name():获取函数域名。

实例

废话不多说,直接上实例。

#include <iostream>
#include <string>
#include <source_location>
 
void log(std::string level, 
    std::string message,
    const std::source_location& location = std::source_location::current()){
    std::cout << level
              << location.file_name() << " in function "
              << location.function_name() << " at "
              << location.line() << ':' << location.column() << ": "
              << message << '\n';
}
 
int main()
{
    log("[DEBUG]","Test");
}

输出内容:

[DEBUG]2.cpp in function int main() at 17:8: Test
boost::property_tree::read_xml(strTmp, pt);加入这个严重性 代码 说明 项目 文件 行 禁止显示状态 详细信息 错误 LNK2019 无法解析的外部符号 "void __cdecl boost::throw_exception(class std::exception const &,struct boost::source_location const &)" (?throw_exception@boost@@YAXAEBVexception@std@@AEBUsource_location@1@@Z),函数 "void __cdecl boost::property_tree::xml_parser::read_xml<class boost::property_tree::basic_ptree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class boost::property_tree::basic_ptree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,int,class std::locale const &)" (??$read_xml@V?$basic_ptree@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@property_tree@boost@@@xml_parser@property_tree@boost@@YAXAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@AEAV?$basic_ptree@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@12@HAEBVlocale@4@@Z) 中引用了该符号 D:\SAMA\sama\build\msvc-Debug\sama D:\SAMA\sama\build\msvc-Debug\copidfunction.cpp.obj 1 错误 LNK1120 1 个无法解析的外部命令 D:\SAMA\sama\build\msvc-Debug\sama D:\SAMA\sama\out\MSVC-Debug\sama.exe 1
最新发布
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

orbitgw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值