httpdirfs/src/log.c

57 lines
1.3 KiB
C
Raw Normal View History

2021-08-22 01:51:37 +02:00
#include "log.h"
2021-08-22 03:26:09 +02:00
#include "config.h"
#include "util.h"
2021-08-22 03:26:09 +02:00
2021-08-22 01:51:37 +02:00
#include <stdarg.h>
#include <stdio.h>
2021-08-22 03:26:09 +02:00
#include <stdlib.h>
2021-08-22 01:51:37 +02:00
2021-08-22 03:26:09 +02:00
int log_level_init()
2021-08-22 01:51:37 +02:00
{
2021-08-22 03:26:09 +02:00
char *env = getenv("HTTPDIRFS_LOG_LEVEL");
2021-08-22 01:51:37 +02:00
if (env) {
return atoi(env);
}
return DEFAULT_LOG_LEVEL;
}
void log_printf(LogType type, const char *file, const char *func, int line,
const char *format, ...)
2021-08-22 01:51:37 +02:00
{
2021-08-29 23:46:24 +02:00
FILE *out = stderr;
2021-08-22 03:26:09 +02:00
if (type & CONFIG.log_level) {
switch (type) {
2021-08-29 23:46:24 +02:00
case fatal:
fprintf(out, "Fatal: ");
2021-08-22 03:26:09 +02:00
break;
2021-08-29 23:46:24 +02:00
case error:
fprintf(out, "Error: ");
2021-08-22 03:26:09 +02:00
break;
2021-08-29 23:46:24 +02:00
case warning:
fprintf(out, "Warning: ");
2021-08-22 03:26:09 +02:00
break;
2021-08-29 23:46:24 +02:00
case info:
out = stdout;
goto print_actual_message;
2021-08-22 03:26:09 +02:00
break;
2021-08-29 23:46:24 +02:00
default:
fprintf(out, "Debug (%x):", type);
2021-08-22 03:26:09 +02:00
break;
}
2021-08-29 23:46:24 +02:00
fprintf(out, "(%s:%s:%d): ", file, func, line);
2021-08-29 23:46:24 +02:00
2021-08-22 03:26:09 +02:00
print_actual_message:
/* A label can only be part of a statement, this is a statement. lol*/
{}
va_list args;
va_start(args, format);
2021-08-29 23:46:24 +02:00
vfprintf(out, format, args);
2021-08-22 03:26:09 +02:00
va_end(args);
if (type == fatal) {
exit_failure();
}
2021-08-22 03:26:09 +02:00
}
2021-08-22 01:51:37 +02:00
}