Skip to content

Instantly share code, notes, and snippets.

@bl4ckb0ne
Last active June 11, 2018 19:07
Show Gist options
  • Save bl4ckb0ne/e831413f14d6ff8d0835a840885314a4 to your computer and use it in GitHub Desktop.
Save bl4ckb0ne/e831413f14d6ff8d0835a840885314a4 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <array>
enum Log_level
{
L_SILENT = 0,
L_ERROR = 1,
L_INFO = 2,
L_DEBUG = 3,
};
static Log_level log_level = L_ERROR;
static constexpr std::array<const char*, 4> log_colors
{
"",
"\x1B[1;31m",
"\x1B[1;34m",
"\x1B[1;30m"
};
template<typename... Args>
void vlk_log_internal(const Log_level level, const char* msg, Args&&... args)
{
if (level > log_level)
{
return;
}
std::FILE* output = (level == L_ERROR ? stderr : stdout);
std::fprintf(output, "%s", log_colors[level]);
std::fprintf(output, msg, std::forward<Args>(args)...);
std::fprintf(output, "\x1B[0m");
std::fprintf(output, "\n");
}
#define vlk_log(lvl, msg, ...) { vlk_log_internal(lvl, "[%s:%d] " msg, __FILE__, __LINE__, ##__VA_ARGS__); } (void)0
int main()
{
log_level = L_SILENT;
vlk_log(L_ERROR, "Error will not show");
log_level = L_ERROR;
vlk_log(L_ERROR, "Error %s", "msg");
vlk_log(L_INFO, "Info will not show");
log_level = L_INFO;
vlk_log(L_INFO, "Info %s", "msg");
vlk_log(L_DEBUG, "Debug will not show");
log_level = L_DEBUG;
vlk_log(L_DEBUG, "Debug %s", "msg");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment