aboutsummaryrefslogtreecommitdiff
path: root/src/log_file.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2018-05-30 15:43:08 +0200
committerToni Uhlig <matzeton@googlemail.com>2018-05-30 15:43:08 +0200
commit1c9022f075387e86269a79ac71f61dd4ac32c421 (patch)
tree535773b5db20511503bde730cf78339e18b894fd /src/log_file.c
parent0dc05d5eaf474c30bcb71f77a2c34f521452c0b5 (diff)
POTD skeleton #83.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src/log_file.c')
-rw-r--r--src/log_file.c171
1 files changed, 171 insertions, 0 deletions
diff --git a/src/log_file.c b/src/log_file.c
new file mode 100644
index 0000000..f0a78f2
--- /dev/null
+++ b/src/log_file.c
@@ -0,0 +1,171 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdarg.h>
+#include <fcntl.h>
+#include <assert.h>
+
+#include "log_file.h"
+
+char *log_file = NULL;
+static FILE *flog = NULL;
+
+
+int log_open_file(void)
+{
+ if (!log_file) {
+ fprintf(stderr, "%s\n", "The path to the logfile was not set.");
+ return 1;
+ }
+
+ flog = fopen(log_file, "a+");
+ if (!flog) {
+ fprintf(stderr, "Could not open '%s' for writing: %s\n",
+ log_file, strerror(errno));
+ return 1;
+ }
+
+ if (setvbuf(flog, NULL, _IOLBF, BUFSIZ)) {
+ log_close_file();
+ return 1;
+ }
+
+ return 0;
+}
+
+void log_close_file(void)
+{
+ fclose(flog);
+ flog = NULL;
+}
+
+void log_fmt_file(log_priority prio, const char *fmt, ...)
+{
+ pid_t my_pid;
+ char out[LOGMSG_MAXLEN+1] = {0};
+ va_list arglist;
+
+ if (prio < log_prio)
+ return;
+ assert(fmt);
+ va_start(arglist, fmt);
+ assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 );
+ va_end(arglist);
+
+ my_pid = getpid();
+ switch (prio) {
+ case DEBUG:
+ fprintf(flog, "[DEBUG] [%d] %s\n", my_pid, out);
+ break;
+ case NOTICE:
+ fprintf(flog, "[NOTICE] [%d] %s\n", my_pid, out);
+ break;
+ case WARNING:
+ fprintf(flog, "[WARNING][%d] %s\n", my_pid, out);
+ break;
+ case ERROR:
+ fprintf(flog, "[ERROR] [%d] %s\n", my_pid, out);
+ break;
+ case CMD:
+ fprintf(flog, "[CMD] [%d] %s\n", my_pid, out);
+ break;
+ }
+}
+
+void log_fmtex_file(log_priority prio, const char *srcfile,
+ size_t line, const char *fmt, ...)
+{
+ pid_t my_pid;
+ char out[LOGMSG_MAXLEN+1] = {0};
+ va_list arglist;
+
+ if (prio < log_prio)
+ return;
+ assert(fmt);
+ va_start(arglist, fmt);
+ assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 );
+ va_end(arglist);
+
+ my_pid = getpid();
+ switch (prio) {
+ case DEBUG:
+ fprintf(flog, "[DEBUG] [%d] %s.%lu: %s\n", my_pid, srcfile,
+ line, out);
+ break;
+ case NOTICE:
+ fprintf(flog, "[NOTICE] [%d] %s.%lu: %s\n",
+ my_pid, srcfile, line, out);
+ break;
+ case WARNING:
+ fprintf(flog, "[WARNING][%d] %s.%lu: %s\n",
+ my_pid, srcfile, line, out);
+ break;
+ case ERROR:
+ fprintf(flog, "[ERROR] [%d] %s.%lu: %s\n",
+ my_pid, srcfile, line, out);
+ break;
+ case CMD:
+ break;
+ }
+}
+
+void log_fmtexerr_file(log_priority prio, const char *srcfile,
+ size_t line, const char *fmt, ...)
+{
+ pid_t my_pid;
+ int saved_errno = errno;
+ char out[LOGMSG_MAXLEN+1] = {0};
+ va_list arglist;
+
+ if (prio < log_prio)
+ return;
+ assert(fmt);
+ va_start(arglist, fmt);
+ assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 );
+ va_end(arglist);
+
+ my_pid = getpid();
+ switch (prio) {
+ case DEBUG:
+ if (saved_errno)
+ fprintf(flog, "[DEBUG] [%d] %s.%lu: %s failed: %s\n",
+ my_pid, srcfile, line, out,
+ strerror(saved_errno));
+ else
+ fprintf(flog, "[DEBUG] [%d] %s.%lu: %s failed\n",
+ my_pid, srcfile, line, out);
+ break;
+ case NOTICE:
+ if (saved_errno)
+ fprintf(flog, "[NOTICE] [%d] %s.%lu: %s failed: %s\n",
+ my_pid, srcfile,
+ line, out, strerror(saved_errno));
+ else
+ fprintf(flog, "[NOTICE] [%d] %s.%lu: %s failed\n",
+ my_pid, srcfile,
+ line, out);
+ break;
+ case WARNING:
+ if (saved_errno)
+ fprintf(flog, "[WARNING][%d] %s.%lu: %s failed: %s\n",
+ my_pid, srcfile,
+ line, out, strerror(saved_errno));
+ else
+ fprintf(flog, "[WARNING][%d] %s.%lu: %s failed\n",
+ my_pid, srcfile,
+ line, out);
+ break;
+ case ERROR:
+ if (saved_errno)
+ fprintf(flog, "[ERROR] [%d] %s.%lu: %s failed: %s\n",
+ my_pid, srcfile,
+ line, out, strerror(saved_errno));
+ else
+ fprintf(flog, "[ERROR] [%d] %s.%lu: %s failed\n",
+ my_pid, srcfile,
+ line, out);
+ break;
+ case CMD:
+ break;
+ }
+}