6/15/2015

C/C++ gflags and glog

Install gflags and glog


$ sudo apt-get install libgflags-dev libgoogle-glog-dev

gflag Source code on GitHub: https://github.com/gflags/gflags
glog Source code on GitHub: https://github.com/google/glog

gflag sample code

===============================================
// Test.cpp
#include <iostream>
#include <gflags/gflags.h> // #include <google/gflags.h>
using namespace std;

DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing");
DEFINE_string(languages, "english,french,german", "comma-separated list of languages to offer in the 'lang' menu");


int main(int argc, char** agrv)
{
    ::google::ParseCommandLineFlags(&argc, &agrv, true);
    std::cout<< "FLAGS_big_menu : " << FLAGS_big_menu << "\n";
    return 0;
}

$ g++ -o test Test.cpp -I /usr/include/gflags -L /usr/lib/x86_64-linux-gnu -lgflags
$ ./Test  --big_menu=false
Output:
FLAGS_big_menu : 0

gflag Defining Flags In Program

Defining a flag is easy: just use the appropriate macro for the type you want the flag to be
DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing");
DEFINE_string(languages, "english,french,german",
                 "comma-separated list of languages to offer in the 'lang' menu");
DEFINE_bool defines a boolean flag. Here are the types supported:
DEFINE_bool: boolean
DEFINE_int32: 32-bit integer
DEFINE_int64: 64-bit integer
DEFINE_uint64: unsigned 64-bit integer
DEFINE_double: double
DEFINE_string: C++ string

gflag Setting Flags on the Command Line

app_containing_foo --languages="chinese,japanese,korean"
app_containing_foo -languages="chinese,japanese,korean"
app_containing_foo --languages "chinese,japanese,korean"
app_containing_foo -languages "chinese,japanese,korean"
For boolean flags, the possibilities are slightly different:
app_containing_foo --big_menu
app_containing_foo --nobig_menu
app_containing_foo --big_menu=true
app_containing_foo --big_menu=false

glog sample code

=============================================
#include <glog/logging.h>
int main(int argc, char** argv) {
    FLAGS_alsologtostderr = 1; // It will dump to console
    google::InitGoogleLogging("test");


    LOG(INFO) << "Dump log test";
    return 0;
}
Output:
I0825 14:45:06.432374 22528 objectdection.cpp:15] Dump log test

Miniglog

If you would like to use glog for cross-platform like Android, you can use miniglog as bellows because glog didn't support Android NDK.

https://github.com/tzutalin/miniglog

Ref:

https://google-gflags.googlecode.com/svn/trunk/doc/gflags.html#intro

1 comment:

  1. Anonymous4/11/2019

    nice work, thanks for your efforts, I have been searching for two days didn't figure how to compile or build glog containing code

    ReplyDelete