From 4471e0c6cdba55e554e5c4ae4cd620afea2dc1f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Sat, 4 Jul 2015 15:43:45 +0200 Subject: [PATCH] degesch: periodically flush logs to disk --- degesch.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/degesch.c b/degesch.c index 193b898..d5ae36a 100644 --- a/degesch.c +++ b/degesch.c @@ -1302,6 +1302,8 @@ struct app_context struct poller_fd tty_event; ///< Terminal input event struct poller_fd signal_event; ///< Signal FD event + struct poller_timer flush_timer; ///< Flush all open files (e.g. logs) + struct poller poller; ///< Manages polled descriptors bool quitting; ///< User requested quitting bool polling; ///< The event loop is running @@ -8498,6 +8500,20 @@ on_tty_readable (const struct pollfd *fd, struct app_context *ctx) input_on_readable (&ctx->input); } +static void +rearm_flush_timer (struct app_context *ctx) +{ + poller_timer_set (&ctx->flush_timer, 60 * 1000); +} + +static void +on_flush_timer (struct app_context *ctx) +{ + // I guess we don't need to do anything more complicated + fflush (NULL); + rearm_flush_timer (ctx); +} + static void init_poller_events (struct app_context *ctx) { @@ -8510,6 +8526,11 @@ init_poller_events (struct app_context *ctx) ctx->tty_event.dispatcher = (poller_fd_fn) on_tty_readable; ctx->tty_event.user_data = ctx; poller_fd_set (&ctx->tty_event, POLLIN); + + poller_timer_init (&ctx->flush_timer, &ctx->poller); + ctx->flush_timer.dispatcher = (poller_timer_fn) on_flush_timer; + ctx->flush_timer.user_data = ctx; + rearm_flush_timer (ctx); } // --- Main program ------------------------------------------------------------