1
0
forked from aniani/gmnisrv
gmnisrv/src/log.c

64 lines
1.1 KiB
C
Raw Normal View History

2020-09-23 14:32:52 -04:00
#include <arpa/inet.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/socket.h>
#include "log.h"
static void
server_logf(FILE *f, const char *fmt, va_list ap)
{
fprintf(f, "[gmnisrv] ");
2020-09-23 14:32:52 -04:00
vfprintf(f, fmt, ap);
2020-09-24 11:15:04 -04:00
fprintf(f, "\n");
2020-09-23 14:32:52 -04:00
}
static void
client_logf(FILE *f, struct sockaddr *addr, const char *fmt, va_list ap)
{
char abuf[INET6_ADDRSTRLEN + 1];
2020-09-23 14:32:52 -04:00
const char *addrs = inet_ntop(addr->sa_family,
addr->sa_data, abuf, sizeof(abuf));
assert(addrs);
fprintf(f, "%s ", addrs);
2020-09-23 14:32:52 -04:00
vfprintf(f, fmt, ap);
2020-09-24 11:15:04 -04:00
fprintf(f, "\n");
2020-09-23 14:32:52 -04:00
}
void
server_log(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
server_logf(stderr, fmt, ap);
2020-09-23 14:32:52 -04:00
va_end(ap);
}
void
client_log(struct sockaddr *addr, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
client_logf(stdout, addr, fmt, ap);
va_end(ap);
}
void
server_error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
server_logf(stderr, fmt, ap);
va_end(ap);
}
void
client_error(struct sockaddr *addr, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
client_logf(stderr, addr, fmt, ap);
va_end(ap);
}