Skip to content

Instantly share code, notes, and snippets.

@soumyakantiroychowdhury
Last active March 5, 2018 01:16
Show Gist options
  • Save soumyakantiroychowdhury/b7eaaf2e2bf773415cb6d679d5b03694 to your computer and use it in GitHub Desktop.
Save soumyakantiroychowdhury/b7eaaf2e2bf773415cb6d679d5b03694 to your computer and use it in GitHub Desktop.
Logging from multiple threads using glog
#include <glog/logging.h>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>
void logger()
{
int num = 1000000;
auto tid = boost::this_thread::get_id();
auto before = boost::chrono::system_clock::now();
for(int i = 0; i < num; i++)
LOG(INFO) << "Logged " << i << "-th time from " << tid;
auto now = boost::chrono::system_clock::now();
auto duration =
boost::chrono::duration_cast<boost::chrono::milliseconds>(now - before);
std::cout << "Time taken to log 1 million lines is " << duration.count() << " milliseconds by thread " << tid << std::endl;
LOG(INFO) << "Time taken to log 1 million lines is " << duration.count() << " milliseconds by thread " << tid;
}
int main(int argc, char* argv[]) {
// Initialize Google's logging library.
//FLAGS_log_dir = "/var/tmp/test/logs"; // Sets log destination
google::SetLogDestination(google::INFO, "/var/tmp/test/logs/file.log"); // Sets log destination and filename
google::InitGoogleLogging(argv[0]);
boost::thread t1{logger};
boost::thread t2{logger};
boost::thread t3{logger};
t1.join();
t2.join();
t3.join();
}
@soumyakantiroychowdhury
Copy link
Author

soumyakantiroychowdhury commented Mar 5, 2018

Build: g++ -std=c++11 glog-performance-test.cpp -lglog -lboost_system -lboost_chrono -lboost_thread -lpthread -o glog-performance-test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment