3

I'm using the google glog library for my C++ program. I have used the glog library before, so I know that it should print out the stack trace when there is a CHECK failure. But it does not print any stack trace for my program:

#include <glog/logging.h>

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

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

int main() {
  foo(1);
}

The Makefile is

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

And the output I'm getting is

$ ./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

Am I missing something here?

Thank you!

Cui

user2100910
  • 327
  • 3
  • 12

1 Answers1

8

I turns out that I should first call the InstallFailureSignalHandler() function to initialize glog, if I want my stack trace to be printed. So the fixed program is:

#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);
}

And the output is:

$ ./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
user2100910
  • 327
  • 3
  • 12