Small code snippets that might prove useful again in the future.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.3 KiB

2 years ago
/* ----------------------------------------------------------------------------
* C-Report
* ----------------------------------------------------------------------------
* Author : Markus Koch <markus@notsyncing.net>
* Contributors : None
* License : Mozilla Public License (MPL) Version 2
* ----------------------------------------------------------------------------
*/
#include "logging.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
static enum log_level current_log_level = LL_INFO;
static const char *ll_string[] = {
"\x1B[31m\x1B[1m[X] ",
"\x1B[31m[E] ",
"\x1B[33m[W] ",
"\x1B[32m[I] ",
"\x1B[34m[D] ",
"\x1B[34m[d] ",
"[?] "
};
int should_report(enum log_level log_level)
{
return (log_level <= current_log_level);
}
enum log_level report(enum log_level log_level, const char *format, ...)
{
va_list vargs;
if (log_level > LL_COUNT)
log_level = LL_COUNT;
if (should_report(log_level)) {
va_start(vargs, format);
fprintf(stderr, "%s", ll_string[log_level]);
vfprintf(stderr, format, vargs);
fprintf(stderr, "\x1B[0m\n");
va_end(vargs);
}
return log_level;
}
void set_log_level(enum log_level log_level)
{
if (log_level >= LL_COUNT)
log_level = LL_COUNT - 1;
current_log_level = log_level;
}
enum log_level get_log_level()
{
return current_log_level;
}