]> git.ipfire.org Git - fireperf.git/commitdiff
tui: Add a log window to show log messages
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Sep 2024 11:52:03 +0000 (11:52 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 28 Sep 2024 11:52:03 +0000 (11:52 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/tui.c

index ef23556001d4ebd2e3929166ba3189cce8784a55..2dc4ffd7a97c833a554f900bc3cfbeccfabac519 100644 (file)
--- a/src/tui.c
+++ b/src/tui.c
@@ -27,6 +27,7 @@
 #include <ncurses.h>
 
 #include "ctx.h"
+#include "logging.h"
 #include "stats.h"
 #include "tui.h"
 #include "util.h"
@@ -49,10 +50,50 @@ struct fireperf_tui {
        // The graph inside the frame
        WINDOW* graph;
 
+       // The log window
+       WINDOW* log;
+
        // The status bar
        WINDOW* status;
 };
 
+void fireperf_tui_log(void* data, int priority, const char* file,
+               int line, const char* fn, const char* format, va_list args) {
+       struct fireperf_tui* tui = data;
+       char buffer[1024];
+       ssize_t length;
+       int rows;
+       int cols;
+
+       // Fetch the dimensions of the log window
+       getmaxyx(tui->log, rows, cols);
+
+       // We leave one character free on the left and the right
+       cols -= 2;
+
+       // Format the message
+       length = vsnprintf(buffer, sizeof(buffer), format, args);
+       if (length < 0)
+               return;
+
+       // Scroll down the previous content to add an extra line
+       scroll(tui->log);
+
+       // Remove the trailing newline
+       length--;
+
+       // If the message is long
+       if (length > cols)
+               length = cols;
+
+       // Write the message
+       mvwaddnstr(tui->log, rows - 1, 1, buffer, length);
+
+       wrefresh(tui->log);
+
+       return;
+}
+
 static const struct fireperf_stats* fireperf_tui_get_stats(struct fireperf_tui* tui, int i) {
        return &tui->stats[(tui->s - i) % MAX_STATS];
 }
@@ -78,7 +119,7 @@ static int fireperf_tui_setup_graph(struct fireperf_tui* tui) {
        int r;
 
        // Create the graph
-       tui->graph = newwin(LINES - 3, COLS - 3, 1, 1);
+       tui->graph = newwin(LINES - 13, COLS - 3, 1, 1);
        if (!tui->graph)
                return 1;
 
@@ -92,7 +133,7 @@ static int fireperf_tui_setup_frame(struct fireperf_tui* tui) {
        int r;
 
        // Create the window
-       tui->frame = newwin(LINES - 1, COLS, 0, 0);
+       tui->frame = newwin(LINES - 11, COLS, 0, 0);
        if (!tui->frame)
                return 1;
 
@@ -111,7 +152,6 @@ static int fireperf_tui_setup_frame(struct fireperf_tui* tui) {
        return 0;
 }
 
-
 static int fireperf_tui_update_status(struct fireperf_tui* tui) {
        const struct fireperf_stats* stats = NULL;
        char buffer[32];
@@ -191,6 +231,23 @@ ERROR:
        return 0;
 }
 
+static int fireperf_tui_setup_log(struct fireperf_tui* tui) {
+       int r;
+
+       // Create the window
+       tui->log = newwin(10, COLS, LINES - 11, 0);
+       if (!tui->log)
+               return 1;
+
+       // Enable scrolling in the window
+       scrollok(tui->log, 1);
+
+       // Refresh
+       wrefresh(tui->log);
+
+       return 0;
+}
+
 static int fireperf_tui_setup_status(struct fireperf_tui* tui) {
        int r;
 
@@ -259,6 +316,11 @@ static int fireperf_tui_setup(struct fireperf_tui* tui) {
        if (r)
                return r;
 
+       // Setup the log window
+       r = fireperf_tui_setup_log(tui);
+       if (r)
+               return r;
+
        // Setup the status bar
        r = fireperf_tui_setup_status(tui);
        if (r)
@@ -287,6 +349,9 @@ int fireperf_tui_init(struct fireperf_tui** tui, struct fireperf_ctx* ctx) {
        if (r)
                return r;
 
+       // Hijack log messages
+       fireperf_set_log_callback(ctx, fireperf_tui_log, t);
+
        // Return the TUI
        *tui = t;