Last active
July 2, 2019 13:05
-
-
Save gipert/d2a4f0fb69381223dccccba174cc7428 to your computer and use it in GitHub Desktop.
Basic C++ logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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