glog编译与使用

1.拉取 

git clone https://github.com/google/glog.git

cd glog

mkdir build 

cd build 

cmake ..

vs2022打开,编译release的 

2. 放到项目中

 

3.案例一

#include <glog/logging.h>

int main()
{
    // 初始化日志系统
    google::InitGoogleLogging("MyProgram");  // 初始化glog,并设置程序名
    google::SetLogDestination(google::GLOG_INFO, "C:/chenqi/lianxi/glogtest/logs/");  // 设置日志输出路径和级别
    google::SetLogFilenameExtension(".log");
    // 记录日志
    LOG(INFO) << "This is an informational message.";
    LOG(WARNING) << "This is a warning message.";
    LOG(ERROR) << "This is an error message.";

    // 关闭日志系统
    google::ShutdownGoogleLogging();

    return 0;
}

案例二(google::SetLogSymlink()的使用)




#include <glog/logging.h>


void SomeFunction()
{
    // 记录日志
    LOG(INFO) << "This is a log message from SomeFunction().";
}

int main()
{
    // 初始化日志系统
    google::InitGoogleLogging("MyProgram");
    google::SetLogDestination(google::GLOG_INFO, "C:/chenqi/lianxi/glogtest/logs/");  // 设置日志输出路径和级别
    google::SetLogFilenameExtension(".log");
    // 设置符号链接
    std::string logDir = "C:/chenqi/lianxi/glogtest/logs/";
    std::string symlinkPath = logDir + "latest_log";
    google::SetLogSymlink(google::GLOG_INFO, symlinkPath.c_str());

    // 记录日志
    LOG(INFO) << "This is the main log message.";

    // 调用另一个函数
    SomeFunction();

    // 关闭日志系统
    google::ShutdownGoogleLogging();

    return 0;
}

在这个示例中,我们创建了一个名为 SomeFunction() 的函数,并在其中记录了一条日志。在 main() 函数中,我们使用 google::SetLogSymlink() 函数创建了一个符号链接。

假设程序运行后生成了多个日志文件,比如 ./logs/20230710.log./logs/20230711.log 等。当我们运行程序时,符号链接 latest_log 会指向最新生成的日志文件,比如 ./logs/20230711.log

通过这种方式,我们可以通过访问符号链接 latest_log 来轻松地找到最新的日志文件,而无需知道具体的日志文件名。

请确保在运行程序之前先创建一个名为 ./logs/ 的目录,并确保在程序运行时生成了一些日志文件,以便能够看到符号链接的效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

江河地笑

实践是检验真理的唯一标准

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

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

打赏作者

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

抵扣说明:

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

余额充值