0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-09 12:48:43 +00:00

CLI: Dropping the mechanism of echoing log messages

This is better done by running tail -f on a logfile.
This commit is contained in:
Maria Matejka 2023-08-15 17:05:58 +02:00
parent 8638c6f0b3
commit 88307c31c5
6 changed files with 1 additions and 148 deletions

View File

@ -160,43 +160,6 @@ cli_printf(cli *c, int code, char *msg, ...)
memcpy(cli_alloc_out(c, size), buf, size);
}
static void
cli_copy_message(cli *c)
{
byte *p, *q;
uint cnt = 2;
if (c->ring_overflow)
{
byte buf[64];
int n = bsprintf(buf, "<%d messages lost>\n", c->ring_overflow);
c->ring_overflow = 0;
memcpy(cli_alloc_out(c, n), buf, n);
}
p = c->ring_read;
while (*p)
{
cnt++;
p++;
if (p == c->ring_end)
p = c->ring_buf;
ASSERT(p != c->ring_write);
}
c->async_msg_size += cnt;
q = cli_alloc_out(c, cnt);
*q++ = '+';
p = c->ring_read;
do
{
*q = *p++;
if (p == c->ring_end)
p = c->ring_buf;
}
while (*q++);
c->ring_read = p;
q[-1] = '\n';
}
static void
cli_hello(cli *c)
{
@ -283,10 +246,6 @@ cli_event(void *data)
cli *c = data;
int err;
while (c->ring_read != c->ring_write &&
c->async_msg_size < CLI_MAX_ASYNC_QUEUE)
cli_copy_message(c);
if (c->tx_pos)
;
else if (c->cont)
@ -335,80 +294,12 @@ cli_kick(cli *c)
static list cli_log_hooks;
static int cli_log_inited;
void
cli_set_log_echo(cli *c, uint mask, uint size)
{
if (c->ring_buf)
{
mb_free(c->ring_buf);
c->ring_buf = c->ring_end = c->ring_read = c->ring_write = NULL;
rem_node(&c->n);
}
c->log_mask = mask;
if (mask && size)
{
c->ring_buf = mb_alloc(c->pool, size);
c->ring_end = c->ring_buf + size;
c->ring_read = c->ring_write = c->ring_buf;
add_tail(&cli_log_hooks, &c->n);
c->log_threshold = size / 8;
}
c->ring_overflow = 0;
}
void
cli_echo(uint class, byte *msg)
{
unsigned len, free, i, l;
cli *c;
byte *m;
if (!cli_log_inited || EMPTY_LIST(cli_log_hooks))
return;
len = strlen(msg) + 1;
WALK_LIST(c, cli_log_hooks)
{
if (!(c->log_mask & (1 << class)))
continue;
if (c->ring_read <= c->ring_write)
free = (c->ring_end - c->ring_buf) - (c->ring_write - c->ring_read + 1);
else
free = c->ring_read - c->ring_write - 1;
if ((len > free) ||
(free < c->log_threshold && class < (unsigned) L_INFO[0]))
{
c->ring_overflow++;
continue;
}
if (c->ring_read == c->ring_write)
ev_schedule(c->event);
m = msg;
l = len;
while (l)
{
if (c->ring_read <= c->ring_write)
i = c->ring_end - c->ring_write;
else
i = c->ring_read - c->ring_write;
if (i > l)
i = l;
memcpy(c->ring_write, m, i);
m += i;
l -= i;
c->ring_write += i;
if (c->ring_write == c->ring_end)
c->ring_write = c->ring_buf;
}
}
}
/* Hack for scheduled undo notification */
extern cli *cmd_reconfig_stored_cli;
void
cli_free(cli *c)
{
cli_set_log_echo(c, 0, 0);
int defer = 0;
if (c->cleanup)
defer = c->cleanup(c);

View File

@ -40,9 +40,6 @@ typedef struct cli {
int last_reply;
int restricted; /* CLI is restricted to read-only commands */
struct linpool *parser_pool; /* Pool used during parsing */
byte *ring_buf; /* Ring buffer for asynchronous messages */
byte *ring_end, *ring_read, *ring_write; /* Pointers to the ring buffer */
uint ring_overflow; /* Counter of ring overflows */
uint log_mask; /* Mask of allowed message levels */
uint log_threshold; /* When free < log_threshold, store only important messages */
uint async_msg_size; /* Total size of async messages queued in tx_buf */
@ -57,7 +54,6 @@ extern struct cli *this_cli; /* Used during parsing */
void cli_printf(cli *, int, char *, ...);
#define cli_msg(x...) cli_printf(this_cli, x)
void cli_set_log_echo(cli *, uint mask, uint size);
static inline void cli_separator(cli *c)
{ if (c->last_reply) cli_printf(c, -c->last_reply, ""); };
@ -69,7 +65,6 @@ void cli_init(void);
void cli_free(cli *);
void cli_kick(cli *);
void cli_written(cli *);
void cli_echo(uint class, byte *msg);
static inline int cli_access_restricted(void)
{

View File

@ -143,7 +143,7 @@ CF_ENUM_PX(T_ENUM_AF, AF_, AFI_, IPV4, IPV6)
%type <s> optproto
%type <ra> r_args
%type <sd> sym_args
%type <i> proto_start echo_mask echo_size debug_mask debug_list debug_flag mrtdump_mask mrtdump_list mrtdump_flag export_mode limit_action net_type tos password_algorithm
%type <i> proto_start debug_mask debug_list debug_flag mrtdump_mask mrtdump_list mrtdump_flag export_mode limit_action net_type tos password_algorithm
%type <ps> proto_patt proto_patt2
%type <cc> channel_start proto_channel
%type <cl> limit_spec
@ -892,26 +892,6 @@ CF_CLI(DUMP FILTER ALL,,, [[Dump all filters in linearized form]])
CF_CLI(EVAL, term, <expr>, [[Evaluate an expression]])
{ cmd_eval(f_linearize($2, 1)); } ;
CF_CLI_HELP(ECHO, ..., [[Control echoing of log messages]])
CF_CLI(ECHO, echo_mask echo_size, (all | off | { debug|trace|info|remote|warning|error|auth [, ...] }) [<buffer-size>], [[Control echoing of log messages]]) {
cli_set_log_echo(this_cli, $2, $3);
cli_msg(0, "");
} ;
echo_mask:
ALL { $$ = ~0; }
| OFF { $$ = 0; }
| '{' log_mask_list '}' { $$ = $2; }
;
echo_size:
/* empty */ { $$ = 4096; }
| NUM {
if ($1 < 256 || $1 > 65536) cf_error("Invalid log buffer size");
$$ = $1;
}
;
CF_CLI(DISABLE, proto_patt opttext, (<protocol> | \"<pattern>\" | all) [message], [[Disable protocol]])
{ proto_apply_cmd($2, proto_cmd_disable, 1, (uintptr_t) $3); } ;
CF_CLI(ENABLE, proto_patt opttext, (<protocol> | \"<pattern>\" | all) [message], [[Enable protocol]])

View File

@ -46,11 +46,6 @@ static pthread_mutex_t log_mutex;
static inline void log_lock(void) { pthread_mutex_lock(&log_mutex); }
static inline void log_unlock(void) { pthread_mutex_unlock(&log_mutex); }
static pthread_t main_thread;
void main_thread_init(void) { main_thread = pthread_self(); }
static int main_thread_self(void) { return pthread_equal(pthread_self(), main_thread); }
#ifdef HAVE_SYSLOG_H
#include <sys/syslog.h>
@ -195,10 +190,6 @@ log_commit(int class, buffer *buf)
}
log_unlock();
/* cli_echo is not thread-safe, so call it just from the main thread */
if (main_thread_self())
cli_echo(class, buf->start);
buf->pos = buf->start;
}

View File

@ -951,9 +951,6 @@ main(int argc, char **argv)
dup2(0, 2);
}
main_thread_init();
write_pid_file();
signal_init();

View File

@ -125,7 +125,6 @@ void krt_io_init(void);
/* log.c */
void main_thread_init(void);
void log_init_debug(char *); /* Initialize debug dump to given file (NULL=stderr, ""=off) */
void log_switch(int initial, list *l, const char *);