mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-04-22 04:15:39 +02:00
log: initial framework for logging things
This commit is contained in:
parent
c1e71eca60
commit
d516ffdda0
3 changed files with 62 additions and 0 deletions
|
@ -26,6 +26,7 @@ add_executable(f00bar
|
|||
bar.c bar.h
|
||||
config.c config.h
|
||||
font.c font.h
|
||||
log.c log.h
|
||||
main.c
|
||||
module.c module.h
|
||||
particle.c particle.h
|
||||
|
|
45
log.c
Normal file
45
log.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include "log.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
static void
|
||||
_log(enum log_class log_class, const char *module, const char *file, int lineno,
|
||||
const char *fmt, va_list va)
|
||||
{
|
||||
bool colorize = true;
|
||||
|
||||
const char *class;
|
||||
int class_clr;
|
||||
switch (log_class) {
|
||||
case LOG_CLASS_ERROR: class = "error"; class_clr = 31; break;
|
||||
case LOG_CLASS_WARNING: class = "warning"; class_clr = 33; break;
|
||||
case LOG_CLASS_INFO: class = "info"; class_clr = 97; break;
|
||||
case LOG_CLASS_DEBUG: class = "debug"; class_clr = 36; break;
|
||||
}
|
||||
|
||||
char clr[16];
|
||||
snprintf(clr, sizeof(clr), "\e[%dm", class_clr);
|
||||
printf("%s%s%s: ", colorize ? clr : "", class, colorize ? "\e[0m" : "");
|
||||
|
||||
#if defined(_DEBUG)
|
||||
printf("%s:%d: ", file, lineno);
|
||||
#else
|
||||
printf("%s: ", module);
|
||||
#endif
|
||||
|
||||
//printf("%%s\n", buf);
|
||||
vprintf(fmt, va);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void
|
||||
log_class(enum log_class log_class, const char *module,
|
||||
const char *file, int lineno, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
_log(log_class, module, file, lineno, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
16
log.h
Normal file
16
log.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
enum log_class { LOG_CLASS_ERROR, LOG_CLASS_WARNING, LOG_CLASS_INFO, LOG_CLASS_DEBUG };
|
||||
|
||||
void log_class(enum log_class log_class, const char *module,
|
||||
const char *file, int lineno,
|
||||
const char *fmt, ...) __attribute__((format (printf, 5, 6)));
|
||||
|
||||
#define LOG_ERR(fmt, ...) \
|
||||
log_class(LOG_CLASS_ERROR, LOG_MODULE, __FILE__, __LINE__, fmt, __VA_ARGS__)
|
||||
#define LOG_WARN(fmt, ...) \
|
||||
log_class(LOG_CLASS_WARNING, LOG_MODULE, __FILE__, __LINE__, fmt, __VA_ARGS__)
|
||||
#define LOG_INFO(fmt, ...) \
|
||||
log_class(LOG_CLASS_INFO, LOG_MODULE, __FILE__, __LINE__, fmt, __VA_ARGS__)
|
||||
#define LOG_DBG(fmt, ...) \
|
||||
log_class(LOG_CLASS_DEBUG, LOG_MODULE, __FILE__, __LINE__, fmt, __VA_ARGS__)
|
Loading…
Add table
Reference in a new issue