1
0
mirror of https://github.com/rtbrick/bngblaster.git synced 2024-05-06 15:54:57 +00:00

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.
This commit is contained in:
Christian Giese
2022-10-19 13:39:21 +00:00
committed by Christian Giese
parent 842d0103e4
commit b41d4947ab
5 changed files with 87 additions and 8 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -11,6 +11,9 @@
#ifndef __BBL_INTERACTIVE_H__
#define __BBL_INTERACTIVE_H__
void
bbl_interactive_log_buf_init();
void
bbl_interactive_init();

View File

@@ -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;
}
}

View File

@@ -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 { \