From b41d4947aba55fe2dd3d7ea055d472499ce253ee Mon Sep 17 00:00:00 2001 From: Christian Giese Date: Wed, 19 Oct 2022 13:39:21 +0000 Subject: [PATCH] add log message buffer The log message buffer is used to display in the ncurses log window even those messages logged before ncurses has started. --- code/bngblaster/src/bbl.c | 7 +++- code/bngblaster/src/bbl_interactive.c | 46 +++++++++++++++++++++++++++ code/bngblaster/src/bbl_interactive.h | 3 ++ code/common/src/logging.c | 17 ++++++---- code/common/src/logging.h | 22 ++++++++++++- 5 files changed, 87 insertions(+), 8 deletions(-) diff --git a/code/bngblaster/src/bbl.c b/code/bngblaster/src/bbl.c index 00c1b480..100a067a 100644 --- a/code/bngblaster/src/bbl.c +++ b/code/bngblaster/src/bbl.c @@ -23,7 +23,11 @@ bbl_ctx_s *g_ctx = NULL; /* Global Variables */ -bool g_interactive = false; /* interactive mode using ncurses */ +bool g_interactive = false; /* interactive mode using ncurses */ + +uint8_t g_log_buf_cur = 0; +char *g_log_buf = NULL; + bool g_init_phase = true; bool g_traffic = true; bool g_banner = true; @@ -444,6 +448,7 @@ main(int argc, char *argv[]) break; case 'I': interactive = true; + bbl_interactive_log_buf_init(); break; case 'S': g_ctx->ctrl_socket_path = optarg; diff --git a/code/bngblaster/src/bbl_interactive.c b/code/bngblaster/src/bbl_interactive.c index b7827d59..e8ca973d 100644 --- a/code/bngblaster/src/bbl_interactive.c +++ b/code/bngblaster/src/bbl_interactive.c @@ -57,6 +57,51 @@ uint32_t g_session_selected = 1; extern const char banner[]; +/* + * The log message buffer is used to display in the ncurses log window + * even those messages logged before ncurses has started. + */ + +extern char *g_log_buf; +extern uint8_t g_log_buf_cur; + +void +bbl_interactive_log_buf_init() +{ + if(g_log_buf) { + free(g_log_buf); + } + g_log_buf = calloc(LOG_BUF_LINES, LOG_BUF_STR_LEN); + g_log_buf_cur = 0; +} + +static void +bbl_interactive_log_buf_free() +{ + int i = 0; + char *line = NULL; + if(!g_log_buf) { + return; + } + for(i = g_log_buf_cur; i < LOG_BUF_LINES; i++) { + line = g_log_buf+(i*LOG_BUF_STR_LEN); + if(!strnlen(line, LOG_BUF_STR_LEN)) { + break; + } + wprintw(log_win, "%s", line); + } + for(i = 0; i < g_log_buf_cur; i++) { + line = g_log_buf+(i*LOG_BUF_STR_LEN); + if(!strnlen(line, LOG_BUF_STR_LEN)) { + break; + } + wprintw(log_win, "%s", line); + } + wrefresh(log_win); + free(g_log_buf); + g_log_buf = NULL; +} + /* * Format a progress bar. */ @@ -680,6 +725,7 @@ bbl_interactive_init() bbl_interactive_init_window(); wrefresh(stats_win); + bbl_interactive_log_buf_free(); g_interactive = true; } diff --git a/code/bngblaster/src/bbl_interactive.h b/code/bngblaster/src/bbl_interactive.h index 4f64d660..bc1025c7 100644 --- a/code/bngblaster/src/bbl_interactive.h +++ b/code/bngblaster/src/bbl_interactive.h @@ -11,6 +11,9 @@ #ifndef __BBL_INTERACTIVE_H__ #define __BBL_INTERACTIVE_H__ +void +bbl_interactive_log_buf_init(); + void bbl_interactive_init(); diff --git a/code/common/src/logging.c b/code/common/src/logging.c index a6770fb7..aecdb19e 100644 --- a/code/common/src/logging.c +++ b/code/common/src/logging.c @@ -20,7 +20,8 @@ char *g_log_file = NULL; * Format the logging timestamp. */ char * -log_format_timestamp(void) { +log_format_timestamp(void) +{ static char ts_str[sizeof("Jun 19 08:07:13.711541")]; struct timespec now; struct tm tm; @@ -39,7 +40,8 @@ log_format_timestamp(void) { * Enable logging. */ void -log_enable(char *log_name) { +log_enable(char *log_name) +{ int idx; if(!log_name) { return; @@ -57,7 +59,8 @@ log_enable(char *log_name) { * Open log file. */ void -log_open() { +log_open() +{ if(!g_log_file) { return; } @@ -68,7 +71,8 @@ log_open() { * Close log file. */ void -log_close() { +log_close() +{ if(g_log_fp) { fclose(g_log_fp); g_log_fp = NULL; @@ -79,7 +83,8 @@ log_close() { * Return log usage string. */ char * -log_usage() { +log_usage() +{ static char buf[128]; struct keyval_ *ptr; int len = 0; @@ -90,4 +95,4 @@ log_usage() { ptr++; } return buf; -} +} \ No newline at end of file diff --git a/code/common/src/logging.h b/code/common/src/logging.h index 8200dd39..8a25ef84 100644 --- a/code/common/src/logging.h +++ b/code/common/src/logging.h @@ -13,6 +13,7 @@ #include "common.h" extern char *g_log_file; + extern FILE *g_log_fp; extern keyval_t log_names[]; @@ -55,6 +56,12 @@ struct __attribute__((__packed__)) log_id_ #ifdef NCURSES_ENABLED extern bool g_interactive; /* interactive mode using ncurses */ +#define LOG_BUF_STR_LEN 256 +#define LOG_BUF_LINES 128 + +extern char *g_log_buf; +extern uint8_t g_log_buf_cur; + #define LOG(log_id_, fmt_, ...) \ do { \ if(g_log_fp) { \ @@ -65,11 +72,17 @@ extern bool g_interactive; /* interactive mode using ncurses */ if(g_interactive) { \ if (log_id[log_id_].enable) { \ wprintw(log_win, "%s "fmt_, log_format_timestamp(), ##__VA_ARGS__); \ - wrefresh(log_win); \ + wrefresh(log_win); \ } \ } else { \ if (log_id[log_id_].enable) { \ fprintf(stdout, "%s "fmt_, log_format_timestamp(), ##__VA_ARGS__); \ + if(g_log_buf) { \ + snprintf(g_log_buf+((g_log_buf_cur++)*LOG_BUF_STR_LEN), LOG_BUF_STR_LEN, "%s "fmt_, log_format_timestamp(), ##__VA_ARGS__); \ + if(g_log_buf_cur >= LOG_BUF_LINES) { \ + g_log_buf_cur = 0; \ + } \ + } \ } \ } \ } while(0) @@ -89,9 +102,16 @@ extern bool g_interactive; /* interactive mode using ncurses */ } else { \ if (log_id[log_id_].enable) { \ fprintf(stdout, "%s "fmt_, log_format_timestamp()); \ + if(g_log_buf) { \ + snprintf(g_log_buf+((g_log_buf_cur++)*LOG_BUF_STR_LEN), LOG_BUF_STR_LEN, "%s "fmt_, log_format_timestamp()); \ + if(g_log_buf_cur >= LOG_BUF_LINES) { \ + g_log_buf_cur = 0; \ + } \ + } \ } \ } \ } while(0) + #else #define LOG(log_id_, fmt_, ...) \ do { \