summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorDanny Holman <dholman@gymli.org>2023-01-04 00:33:15 -0600
committerDanny Holman <dholman@gymli.org>2023-01-04 00:33:15 -0600
commit760fada7269b083ce00a1f18a4fb1cbb9ab13dc7 (patch)
tree8c1f806f761abbc7c55245b82cb5338c2af084b7 /server
parentbe4f102b8aa16bb90ed7d8d51a84650987dfdb40 (diff)
logging: add a close function
Add a close function to close the file handle pointing at the log file. Signed-off-by: Danny Holman <dholman@gymli.org>
Diffstat (limited to 'server')
-rw-r--r--server/Makefile.am3
-rw-r--r--server/configure.ac2
-rw-r--r--server/include/logging.h2
-rw-r--r--server/src/logging.c21
4 files changed, 22 insertions, 6 deletions
diff --git a/server/Makefile.am b/server/Makefile.am
index f5712fc..3ecc941 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -1,2 +1,3 @@
bin_PROGRAMS = mratd
-mratd_SOURCES = src/mini-rat.c src/server.c src/logging.c
+mratd_SOURCES = src/mini-rat.c src/server.c src/logging.c src/session.c
+mratd_LDADD = -lpthread
diff --git a/server/configure.ac b/server/configure.ac
index 7f2e0d5..4309259 100644
--- a/server/configure.ac
+++ b/server/configure.ac
@@ -9,7 +9,7 @@ AC_CONFIG_MACRO_DIR([../m4])
AM_INIT_AUTOMAKE([1.16 -Wall -Werror foreign subdir-objects])
AC_CONFIG_FILES([Makefile])
-AC_CHECK_HEADERS([stdio.h stdlib.h string.h pthread.h arpa/inet.h sys/socket.h])
+AC_CHECK_HEADERS([stdio.h stdlib.h string.h pthread.h arpa/inet.h sys/socket.h], [], AC_MSG_ERROR([missing required headers]))
AX_CHECK_COMPILE_FLAG([-Wall], [CFLAGS+=" -Wall"])
AX_CHECK_COMPILE_FLAG([-Wextra], [CFLAGS+= "-Wextra"])
diff --git a/server/include/logging.h b/server/include/logging.h
index a964230..6e8dfaa 100644
--- a/server/include/logging.h
+++ b/server/include/logging.h
@@ -15,4 +15,6 @@ int init_logging(FILE *out_file);
int vlog_msg(int level, const char *fmt, va_list args);
int log_msg(int level, const char *fmt, ...);
+void close_logfile(void);
+
#endif
diff --git a/server/src/logging.c b/server/src/logging.c
index 9084baf..66f1661 100644
--- a/server/src/logging.c
+++ b/server/src/logging.c
@@ -1,6 +1,8 @@
#include <logging.h>
#include <stdio.h>
#include <pthread.h>
+#include <time.h>
+#include <string.h>
static const char *severe = "SEVERE";
static const char *warn = "WARN";
@@ -16,26 +18,33 @@ int init_logging(FILE *out_file) {
}
int vlog_msg(int level, const char *fmt, va_list args) {
+ time_t rawtime;
+ time(&rawtime);
+ char timestr[1024];
+ ctime_r(&rawtime, timestr);
+ timestr[strlen(timestr)-1] = '\0';
+
char output[1024];
int ret = 0;
switch (level) {
case LOG_SEVERE:
- snprintf(output, 1024, "%s: %s", severe, fmt);
+ snprintf(output, 1024, "%s %s: %s", timestr, severe, fmt);
ret = vfprintf(out_stream, output, args);
break;
case LOG_WARNING:
- snprintf(output, 1024, "%s: %s", warn, fmt);
+ snprintf(output, 1024, "%s %s: %s", timestr, warn, fmt);
ret = vfprintf(out_stream, output, args);
break;
case LOG_INFO:
- snprintf(output, 1024, "%s: %s", info, fmt);
+ snprintf(output, 1024, "%s %s: %s", timestr, info, fmt);
ret = vfprintf(out_stream, output, args);
break;
case LOG_DEBUG:
- snprintf(output, 1024, "%s: %s", debug, fmt);
+ snprintf(output, 1024, "%s %s: %s", timestr, debug, fmt);
ret = vfprintf(out_stream, output, args);
break;
}
+ fflush(out_stream);
return ret;
}
@@ -49,3 +58,7 @@ int log_msg(int level, const char *fmt, ...) {
return done;
}
+
+void close_logfile(void) {
+ fclose(out_stream);
+}