Skip to content

Instantly share code, notes, and snippets.

@Ben1980
Last active March 12, 2019 09: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 Ben1980/cb77c95e24130ab376f6f72c42d26a8c to your computer and use it in GitHub Desktop.
Save Ben1980/cb77c95e24130ab376f6f72c42d26a8c to your computer and use it in GitHub Desktop.
#include <thread>
#include "loguru.hpp"
#include "loguru.cpp"
void sleep(int ms)
{
//We can also inject parameters into the logging strings
VLOG_F(0, "Sleeping for %d ms", ms);
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
void complex()
{
//LOG_SCOPE_F is indenting the following logs
LOG_SCOPE_F(INFO, "Preparing complex calculation");
//VLOG_F can take a dynamic defined verbosity level
VLOG_F(0, "Heating up CPU");
sleep(500);
std::thread([](){
loguru::set_thread_name("complex lambda");
const bool value = true;
LOG_IF_F(INFO, value, "This log is printed inside another thread");
}).join();
}
void crashingFunction(int index)
{
//ERROR_CONTEXT is logging certain arguments in case of a crash
ERROR_CONTEXT("Computing with index", index);
//Asserts are also possible
std::vector<std::string> list;
CHECK_F(index > 1, "Oh no, wrong index, index is %d!!", index);
}
int main(int argc, char* argv[])
{
//Time stamping begin of logging and we can forward cli parameters such as -v (verbosity level) to loguru
loguru::init(argc, argv);
//Additional, if we need we can also put the logs into a defined file
loguru::add_file("important.log", loguru::Truncate, loguru::Verbosity_INFO);
LOG_F(INFO, "We are starting our complex threaded computation!");
complex();
LOG_F(INFO, "Complex computation done!");
crashingFunction(-1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment