3

我正在为我的 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

我在这里错过了什么吗?

谢谢!

4

1 回答 1

8

事实证明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
于 2016-07-08T15:18:24.207 回答