Skip to content

Instantly share code, notes, and snippets.

@raytroop
Last active August 24, 2021 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raytroop/d8b95e96ad1bab719d7c40e78dc748aa to your computer and use it in GitHub Desktop.
Save raytroop/d8b95e96ad1bab719d7c40e78dc748aa to your computer and use it in GitHub Desktop.
glog with gflags
cmake_minimum_required(VERSION 3.10)
# set the project name
project(useGlog)
find_package(gflags REQUIRED)
find_package(glog REQUIRED)
INCLUDE_DIRECTORIES(${GLOG_INCLUDE_DIR})
# add the executable
add_executable(example example.cpp)
# https://github.com/gflags/example/blob/6768ef36b9b47c8a26d0402a88b3165e3961d7fb/foo/CMakeLists.txt#L1-L14
# target_link_libraries(example gflags::gflags glog::glog)
target_link_libraries(example gflags glog::glog)
#include "glog/logging.h"
#include <gflags/gflags.h>
int main(int argc, char *argv[])
{
// Initialize Google's logging library.
google::InitGoogleLogging(argv[0]);
google::ParseCommandLineFlags(&argc, &argv, true);
FLAGS_colorlogtostderr = 1;
// FLAGS_alsologtostderr = 1;
LOG(INFO) << "There is info";
LOG(WARNING) << "There is warning";
LOG(ERROR) << "There is error !!!";
LOG(WARNING) << "There is another warning";
for (int i = 0; i < 20; i++)
{
LOG_IF(INFO, i > 15) << i << ">15"; //When I > 15, Log
}
google::ShutdownGoogleLogging();
return 0;
}
anon@myserver:~/maze/useGlog/build$ ./example
E20210824 22:58:20.879582 997166 example.cpp:14] There is error !!!
anon@myserver:~/maze/useGlog/build$ ./example --logtostderr=1
I20210824 22:58:48.791034 998166 example.cpp:12] There is info
W20210824 22:58:48.791204 998166 example.cpp:13] There is warning
E20210824 22:58:48.791260 998166 example.cpp:14] There is error !!!
W20210824 22:58:48.791302 998166 example.cpp:15] There is another warning
I20210824 22:58:48.791342 998166 example.cpp:19] 16>15
I20210824 22:58:48.791364 998166 example.cpp:19] 17>15
I20210824 22:58:48.791386 998166 example.cpp:19] 18>15
I20210824 22:58:48.791407 998166 example.cpp:19] 19>15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment