gpt4 book ai didi

c++ - Google glog 不打印堆栈跟踪

转载 作者:行者123 更新时间:2023-11-30 01:43:18 32 4
gpt4 key购买 nike

我正在为我的 C++ 程序使用 google glog 库。我以前使用过 glog 库,所以我知道它应该在 CHECK 失败时打印出堆栈跟踪。但它不会为我的程序打印任何堆栈跟踪:

#include <glog/logging.h>

void bar(int x) {
CHECK_EQ(x, 1);
}

void foo(int x) {
bar(x + 1);
}

int main() {
foo(1);
}

Makefile 是

all: Makefile test.cpp
g++ -g -O3 test.cpp -lglog -o test

我得到的输出是

$ ./test 
WARNING: Logging before InitGoogleLogging() is written to STDERR
F0629 14:09:45.900789 37730 test.cpp:4] Check failed: x == 1 (2 vs. 1)
*** Check failure stack trace: ***
Aborted

我是不是漏掉了什么?

谢谢!

最佳答案

事实证明,如果我想要打印堆栈跟踪,我应该首先调用 InstallFailureSignalHandler() 函数来初始化 glog。所以固定程序是:

#include <glog/logging.h>

void bar(int x) {
CHECK_EQ(x, 1);
}

void foo(int x) {
bar(x + 1);
}

int main(int argc, char** argv) {
google::InstallFailureSignalHandler();
foo(1);
}

输出是:

$ ./test 
WARNING: Logging before InitGoogleLogging() is written to STDERR
F0708 09:15:35.401262 44752 test.cpp:4] Check failed: x == 1 (2 vs. 1)
*** Check failure stack trace: ***
*** Aborted at 1467990935 (unix time) try "date -d @1467990935" if you are using GNU date ***
PC: @ 0x7f3c96566f89 (unknown)
*** SIGABRT (@0x275c0000aed0) received by PID 44752 (TID 0x7f3c9725b780) from PID 44752; stack trace: ***
@ 0x7f3c96567000 (unknown)
@ 0x7f3c96566f89 (unknown)
@ 0x7f3c9656a398 (unknown)
@ 0x7f3c96e28d81 (unknown)
@ 0x7f3c96e28daa (unknown)
@ 0x7f3c96e28ce4 (unknown)
@ 0x7f3c96e286e6 (unknown)
@ 0x7f3c96e2b687 (unknown)
@ 0x400d80 bar()
@ 0x400dab foo()
@ 0x400dcb main
@ 0x7f3c96551ec5 (unknown)
@ 0x400c39 (unknown)
@ 0x0 (unknown)
Aborted

关于c++ - Google glog 不打印堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062352/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com