2025-11-21 19:39:15 +01:00
|
|
|
#include "errors.h"
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2025-11-24 20:43:30 +01:00
|
|
|
#include "../utils/time/fmt_time.h"
|
2025-11-21 19:39:15 +01:00
|
|
|
#include "logs.h"
|
|
|
|
|
|
|
|
|
|
// === Static variable
|
|
|
|
|
|
|
|
|
|
static struct logs_config config;
|
|
|
|
|
|
|
|
|
|
// === Functions
|
|
|
|
|
|
|
|
|
|
void errlog_init(bool enabled, FILE *logfile_stream)
|
|
|
|
|
{
|
|
|
|
|
config.enabled = enabled;
|
|
|
|
|
config.logfile_stream = logfile_stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void print_err()
|
|
|
|
|
{
|
|
|
|
|
print_log_err("%s", get_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void print_log_err(char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
// Log prefix (time and server name)
|
|
|
|
|
fprintf(config.logfile_stream, "%s [%s] ", get_time(),
|
|
|
|
|
config.server_cfg->server_name);
|
|
|
|
|
|
|
|
|
|
// Print actual log
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, format);
|
|
|
|
|
vfprintf(config.logfile_stream, format, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
// New line
|
|
|
|
|
fprintf(config.logfile_stream, "\n");
|
|
|
|
|
|
|
|
|
|
// Print to stderr
|
|
|
|
|
fprintf(stderr, "Error: %s", get_err());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *get_err()
|
|
|
|
|
{
|
|
|
|
|
return strerror(errno);
|
|
|
|
|
}
|