Skip to content

Instantly share code, notes, and snippets.

@gipert
Last active July 2, 2019 13:05
Show Gist options
  • Save gipert/d2a4f0fb69381223dccccba174cc7428 to your computer and use it in GitHub Desktop.
Save gipert/d2a4f0fb69381223dccccba174cc7428 to your computer and use it in GitHub Desktop.
Basic C++ logger
/* cpplog.hpp
*
* Author: Luigi Pertoldi
* Created: Tue 02 Jul 2019
*
* Usage:
*
* logging::min_level = logging::info;
* logging::out(info) << "the program is doing well" << std::endl;
* logging::out(error) << "an error occurred!" << std::endl;
*
*/
#include <iostream>
#include <fstream>
namespace logging {
enum level {debug, info, warning, error};
level min_level = info;
std::ofstream devnull("/dev/null");
std::ostream& out(const level& lvl) {
if (lvl == debug and min_level <= debug) {
std::cout << "\033[36m[ Debug:\033[0m ";
return std::cout;
}
if (lvl == info and min_level <= info) {
std::cout << "\033[97;1m[ Info:\033[0m ";
return std::cout;
}
if (lvl == warning and min_level <= warning) {
std::cerr << "\033[33m[ Warning:\033[0m ";
return std::cerr;
}
if (lvl == error and min_level <= error) {
std::cerr << "\033[91m[ Error:\033[0m ";
return std::cerr;
}
else return devnull;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment