c++glog

`glog` example is not showing output as expected


I am trying to understand glog and therefore trying to run the example code on their github page.

I have installed glog (version - 0.6.0) and its dependency gflags (version - 2.2) on my mac OS (10.15.7)

I compile the example below

#include <glog/logging.h>

int main(int argc, char* argv[]) {
    // Initialize Google’s logging library.
    google::InitGoogleLogging(argv[0]);

    // test with setting a value for num_cookies
    int num_cookies = 3;

    // ...
    LOG(INFO) << "Found " << num_cookies << " cookies";
}

using the following command (and it compiles without any errors or warnings).

g++ glog-test.cpp -I/usr/local/include -L/usr/local/lib -lglog -lgflags -o glog-test.o

When I run the example using the following command,

./glog-test.o --logtostderr=1 --stderrthreshold=0

I expect to see the message Found 3 cookies on my terminal, but I see nothing being printed.

I have also experimented with different values for logtostderr (0, 1) and stderrthreshold (0, 1, 2, 3) and nothing gets written to the directory or gets printed on the terminal.

Any help in understanding what I am doing wrong here would be much appreciated, thank you!


Solution

  • You have to parse the command line flags manually through gflags::ParseCommandLineFlags

    #include <glog/logging.h>
    #incldue <gflags/gflags.h>
    
    int main(int argc, char* argv[]) {
        // Initialize Google’s logging library.
        google::InitGoogleLogging(argv[0]);
    
        gflags::ParseCommandLineFlags(&argc, &argv, true);
    
        // test with setting a value for num_cookies
        int num_cookies = 3;
    
        // ...
        LOG(INFO) << "Found " << num_cookies << " cookies";
    }