Skip to content

Instantly share code, notes, and snippets.

@Ben1980

Ben1980/loguru.cpp

Last active Mar 12, 2019
Embed
What would you like to do?
#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
You can’t perform that action at this time.