commit ae7992b29f87e169510658b05c99e371d459d544
parent ee2ba3e29af85489041255d3454c45409f178f34
Author: lumidify <nobody@lumidify.org>
Date:   Tue, 29 Dec 2020 22:41:44 +0100
Improve logging
Diffstat:
4 files changed, 48 insertions(+), 11 deletions(-)
diff --git a/button.c b/button.c
@@ -117,8 +117,7 @@ ltk_button_ini_handler(ltk_window *window, const char *prop, const char *value) 
 		ltk_color_create(window->dpy, window->screen, window->cm,
 		    value, &theme.text_color);
 	} else {
-		/* FIXME: implement this function...
-		(void)ltk_log_warn("Unknown property \"%s\" for button style.\n", prop);*/
+		ltk_warn("Unknown property \"%s\" for button style.\n", prop);
 	}
 }
 
diff --git a/ltk.h b/ltk.h
@@ -24,6 +24,9 @@
 #ifndef _LTK_H_
 #define _LTK_H_
 
+#include <stdarg.h>
+
+/* FIXME: standardize the includes... */
 /* Requires the following includes: <X11/Xlib.h>, <X11/Xutil.h> */
 
 #include "khash.h"
@@ -132,7 +135,8 @@ typedef struct ltk_window {
 } ltk_window;
 
 void ltk_window_invalidate_rect(ltk_window *window, ltk_rect rect);
-void ltk_fatal(const char *msg);
+void ltk_warn(const char *format, ...);
+void ltk_fatal(const char *format, ...);
 int ltk_create_xcolor(ltk_window *window, const char *hex, XColor *col);
 void ltk_queue_event(ltk_window *window, ltk_event_type type, const char *id, const char *data);
 int ltk_collide_rect(ltk_rect rect, int x, int y);
diff --git a/ltkd.c b/ltkd.c
@@ -127,13 +127,13 @@ int main(int argc, char *argv[]) {
 
 	ltk_dir = ltk_setup_directory();
 	if (!ltk_dir) ltk_fatal("Unable to setup ltk directory.\n");
+	ltk_logfile = open_log(ltk_dir);
+	if (!ltk_logfile) ltk_fatal("Unable to open log file.\n");
 
 	/* FIXME: set window size properly - I only run it in a tiling WM
 	   anyways, so it doesn't matter, but still... */
 	main_window = ltk_create_window(title, 0, 0, 500, 500);
 
-	ltk_logfile = open_log(ltk_dir);
-	if (!ltk_logfile) ltk_fatal("Unable to open log file.\n");
 	sock_path = get_sock_path(ltk_dir, main_window->xwindow);
 	if (!sock_path) ltk_fatal("Unable to allocate memory for socket path.\n");
 
@@ -263,6 +263,8 @@ daemonize(void) {
 	struct sigaction sa;
 
 	fflush(stdout);
+	fflush(stderr);
+	fflush(ltk_logfile);
 
 	if ((pid = fork()) < 0)
 		ltk_fatal("Can't fork.\n");
@@ -402,16 +404,45 @@ ltk_window_invalidate_rect(ltk_window *window, ltk_rect rect) {
 		window->dirty_rect = ltk_rect_union(rect, window->dirty_rect);
 }
 
-void
-ltk_fatal(const char *msg) {
+static void
+print_log(const char *mode, const char *format, va_list args) {
+	char logtime[25]; /* FIXME: This should always be big enough, right? */
 	FILE *errfile = ltk_logfile;
+	time_t clock;
+	struct tm *timeptr;
+
+	time(&clock);
+	timeptr = localtime(&clock);
+	strftime(&logtime, 25, "%Y-%m-%d %H:%M:%S", timeptr);
+
 	if (!errfile)
 		errfile = stderr;
-	(void)fprintf(errfile, msg);
-	/* FIXME: cleanup gui stuff too (need window for that) */
+
+	if (main_window)
+		fprintf(errfile, "%s ltkd(%d) %s: ", logtime, main_window->xwindow, mode);
+	else
+		fprintf(errfile, "%s ltkd(?) %s: ", logtime, mode);
+	vfprintf(errfile, format, args);
+}
+
+void
+ltk_warn(const char *format, ...) {
+	va_list args;
+	va_start(args, format);
+	print_log("Warning", format, args);
+	va_end(args);
+}
+
+void
+ltk_fatal(const char *format, ...) {
+	va_list args;
+	va_start(args, format);
+	print_log("Fatal", format, args);
+	va_end(args);
 	ltk_cleanup_nongui();
 	if (main_window)
 		ltk_cleanup_gui(main_window);
+
 	exit(1);
 };
 
@@ -609,8 +640,7 @@ ltk_load_theme(ltk_window *window, const char *path) {
 	ltk_window_setup_theme_defaults(window);
 	ltk_button_setup_theme_defaults(window);
 	if (ini_parse(path, ltk_ini_handler, window) < 0) {
-		/* FIXME: implement this function...
-		ltk_log_warn("Can't load theme.\n"); */
+		ltk_warn("Can't load theme.\n");
 	}
 }
 
diff --git a/test.sh b/test.sh
@@ -8,6 +8,10 @@
 
 export LTKDIR="`pwd`/.ltk"
 ltk_id=`./ltkd -t "Cool Window"`
+if [ $? -ne 0 ]; then
+	echo "Unable to start ltkd." >&2
+	exit 1
+fi
 
 cat test.gui | ./ltkc $ltk_id | while read cmd
 do