From f9a61faaa29ede4de92de06dd42a21bb9efc5911 Mon Sep 17 00:00:00 2001 From: Alberto Fanjul Date: Tue, 8 Nov 2022 08:34:45 +0100 Subject: [PATCH] Do not prefix command output with time --- src/ctl/ctl-cli.c | 55 +++++++++++++++++++++++------------ src/ctl/ctl.h | 4 ++- src/ctl/sinkctl.c | 60 +++++++++++++++++++------------------- src/ctl/wifictl.c | 74 +++++++++++++++++++++++------------------------ 4 files changed, 106 insertions(+), 87 deletions(-) diff --git a/src/ctl/ctl-cli.c b/src/ctl/ctl-cli.c index 654b434..3e77921 100644 --- a/src/ctl/ctl-cli.c +++ b/src/ctl/ctl-cli.c @@ -57,7 +57,7 @@ static bool is_cli(void) return cli_rl; } -void cli_printv(const char *fmt, va_list args) +void cli_printv(const char *fmt, bool prefix_time, va_list args) { SHL_PROTECT_ERRNO; _shl_free_ char *line = NULL; @@ -76,6 +76,22 @@ void cli_printv(const char *fmt, va_list args) rl_redisplay(); } + if (prefix_time) { + cli_printf_time_prefix(); + } + + vprintf(fmt, args); + + if (async) { + rl_restore_prompt(); + rl_replace_line(line, 0); + rl_point = point; + rl_redisplay(); + } +} + +void cli_printf_time_prefix(const char *fmt, va_list args) +{ long long sec, usec; time_t now; struct tm *timeinfo; @@ -106,15 +122,16 @@ void cli_printv(const char *fmt, va_list args) printf("[%s] ", buffer); else if (log__have_time()) printf("[%.4lld.%.6lld] ", sec, usec); +} - vprintf(fmt, args); +void cli_command_printf(const char *fmt, ...) +{ + SHL_PROTECT_ERRNO; + va_list args; - if (async) { - rl_restore_prompt(); - rl_replace_line(line, 0); - rl_point = point; - rl_redisplay(); - } + va_start(args, fmt); + cli_printv(fmt, false, args); + va_end(args); } void cli_printf(const char *fmt, ...) @@ -123,7 +140,7 @@ void cli_printf(const char *fmt, ...) va_list args; va_start(args, fmt); - cli_printv(fmt, args); + cli_printv(fmt, true, args); va_end(args); } @@ -131,7 +148,7 @@ int cli_help(const struct cli_cmd *cmds, int whitespace) { unsigned int i; - cli_printf("Available commands:\n"); + cli_command_printf("Available commands:\n"); for (i = 0; cmds[i].cmd; ++i) { if (!cmds[i].desc) @@ -141,11 +158,11 @@ int cli_help(const struct cli_cmd *cmds, int whitespace) if (!is_cli() && cmds[i].cli_cmp == CLI_Y) continue; - cli_printf(" %s %-*s %s\n", - cmds[i].cmd, - (int)(whitespace - strlen(cmds[i].cmd)), - cmds[i].args ? : "", - cmds[i].desc ? : ""); + cli_command_printf(" %s %-*s %s\n", + cmds[i].cmd, + (int)(whitespace - strlen(cmds[i].cmd)), + cmds[i].args ? : "", + cmds[i].desc ? : ""); } return 0; @@ -174,21 +191,21 @@ int cli_do(const struct cli_cmd *cmds, char **args, unsigned int n) switch (cmds[i].argc_cmp) { case CLI_EQUAL: if (n != cmds[i].argc) { - cli_printf("Invalid number of arguments\n"); + cli_command_printf("Invalid number of arguments\n"); return -EINVAL; } break; case CLI_MORE: if (n < cmds[i].argc) { - cli_printf("too few arguments\n"); + cli_command_printf("too few arguments\n"); return -EINVAL; } break; case CLI_LESS: if (n > cmds[i].argc) { - cli_printf("too many arguments\n"); + cli_command_printf("too many arguments\n"); return -EINVAL; } @@ -234,7 +251,7 @@ static void cli_handler_fn(char *input) if (r != -EAGAIN) return; - cli_printf("Command not found\n"); + cli_command_printf("Command not found\n"); } static int cli_stdin_fn(sd_event_source *source, diff --git a/src/ctl/ctl.h b/src/ctl/ctl.h index 02220bc..2ddf8fb 100644 --- a/src/ctl/ctl.h +++ b/src/ctl/ctl.h @@ -123,8 +123,10 @@ bool ctl_sink_is_closed(struct ctl_sink *s); /* CLI handling */ extern unsigned int cli_max_sev; -void cli_printv(const char *fmt, va_list args); +void cli_printv(const char *fmt, bool prefix_time, va_list args); +void cli_printf_time_prefix(); void cli_printf(const char *fmt, ...); +void cli_command_printf(const char *fmt, ...); #define cli_log(_fmt, ...) \ cli_printf(_fmt "\n", ##__VA_ARGS__) diff --git a/src/ctl/sinkctl.c b/src/ctl/sinkctl.c index 9781bc6..93a1bc9 100644 --- a/src/ctl/sinkctl.c +++ b/src/ctl/sinkctl.c @@ -91,28 +91,28 @@ static int cmd_list(char **args, unsigned int n) /* list links */ - cli_printf("%6s %-24s %-30s %-10s\n", - "LINK", "INTERFACE", "FRIENDLY-NAME", "MANAGED"); + cli_command_printf("%6s %-24s %-30s %-10s\n", + "LINK", "INTERFACE", "FRIENDLY-NAME", "MANAGED"); shl_dlist_for_each(i, &wifi->links) { l = link_from_dlist(i); ++link_cnt; - cli_printf("%6s %-24s %-30s %-10s\n", - l->label, - shl_isempty(l->ifname) ? - "" : l->ifname, + cli_command_printf("%6s %-24s %-30s %-10s\n", + l->label, + shl_isempty(l->ifname) ? + "" : l->ifname, shl_isempty(l->friendly_name) ? "" : l->friendly_name, l->managed ? "yes": "no"); } - cli_printf("\n"); + cli_command_printf("\n"); /* list peers */ - cli_printf("%6s %-24s %-30s %-10s\n", - "LINK", "PEER-ID", "FRIENDLY-NAME", "CONNECTED"); + cli_command_printf("%6s %-24s %-30s %-10s\n", + "LINK", "PEER-ID", "FRIENDLY-NAME", "CONNECTED"); shl_dlist_for_each(i, &wifi->links) { l = link_from_dlist(i); @@ -121,16 +121,16 @@ static int cmd_list(char **args, unsigned int n) p = peer_from_dlist(j); ++peer_cnt; - cli_printf("%6s %-24s %-30s %-10s\n", - p->l->label, - p->label, - shl_isempty(p->friendly_name) ? + cli_command_printf("%6s %-24s %-30s %-10s\n", + p->l->label, + p->label, + shl_isempty(p->friendly_name) ? "" : p->friendly_name, p->connected ? "yes" : "no"); } } - cli_printf("\n %u peers and %u links listed.\n", peer_cnt, link_cnt); + cli_command_printf("\n %u peers and %u links listed.\n", peer_cnt, link_cnt); return 0; } @@ -155,34 +155,34 @@ static int cmd_show(char **args, unsigned int n) } if (l) { - cli_printf("Link=%s\n", l->label); + cli_command_printf("Link=%s\n", l->label); if (l->ifindex > 0) - cli_printf("InterfaceIndex=%u\n", l->ifindex); + cli_command_printf("InterfaceIndex=%u\n", l->ifindex); if (l->ifname && *l->ifname) - cli_printf("InterfaceName=%s\n", l->ifname); + cli_command_printf("InterfaceName=%s\n", l->ifname); if (l->friendly_name && *l->friendly_name) - cli_printf("FriendlyName=%s\n", l->friendly_name); - cli_printf("P2PScanning=%d\n", l->p2p_scanning); + cli_command_printf("FriendlyName=%s\n", l->friendly_name); + cli_command_printf("P2PScanning=%d\n", l->p2p_scanning); if (l->wfd_subelements && *l->wfd_subelements) - cli_printf("WfdSubelements=%s\n", l->wfd_subelements); - cli_printf("Managed=%d\n", l->managed); + cli_command_printf("WfdSubelements=%s\n", l->wfd_subelements); + cli_command_printf("Managed=%d\n", l->managed); } else if (p) { - cli_printf("Peer=%s\n", p->label); + cli_command_printf("Peer=%s\n", p->label); if (p->p2p_mac && *p->p2p_mac) - cli_printf("P2PMac=%s\n", p->p2p_mac); + cli_command_printf("P2PMac=%s\n", p->p2p_mac); if (p->friendly_name && *p->friendly_name) - cli_printf("FriendlyName=%s\n", p->friendly_name); - cli_printf("Connected=%d\n", p->connected); + cli_command_printf("FriendlyName=%s\n", p->friendly_name); + cli_command_printf("Connected=%d\n", p->connected); if (p->interface && *p->interface) - cli_printf("Interface=%s\n", p->interface); + cli_command_printf("Interface=%s\n", p->interface); if (p->local_address && *p->local_address) - cli_printf("LocalAddress=%s\n", p->local_address); + cli_command_printf("LocalAddress=%s\n", p->local_address); if (p->remote_address && *p->remote_address) - cli_printf("RemoteAddress=%s\n", p->remote_address); + cli_command_printf("RemoteAddress=%s\n", p->remote_address); if (p->wfd_subelements && *p->wfd_subelements) - cli_printf("WfdSubelements=%s\n", p->wfd_subelements); + cli_command_printf("WfdSubelements=%s\n", p->wfd_subelements); } else { - cli_printf("Show what?\n"); + cli_command_printf("Show what?\n"); return 0; } diff --git a/src/ctl/wifictl.c b/src/ctl/wifictl.c index 35f0f95..5b511fb 100644 --- a/src/ctl/wifictl.c +++ b/src/ctl/wifictl.c @@ -52,27 +52,27 @@ static int cmd_list(char **args, unsigned int n) /* list links */ - cli_printf("%6s %-24s %-30s %-10s\n", + cli_command_printf("%6s %-24s %-30s %-10s\n", "LINK", "INTERFACE", "FRIENDLY-NAME", "MANAGED"); shl_dlist_for_each(i, &wifi->links) { l = link_from_dlist(i); ++link_cnt; - cli_printf("%6s %-24s %-30s %-10s\n", - l->label, - shl_isempty(l->ifname) ? - "" : l->ifname, - shl_isempty(l->friendly_name) ? - "" : l->friendly_name, - l->managed ? "yes": "no"); + cli_command_printf("%6s %-24s %-30s %-10s\n", + l->label, + shl_isempty(l->ifname) ? + "" : l->ifname, + shl_isempty(l->friendly_name) ? + "" : l->friendly_name, + l->managed ? "yes": "no"); } - cli_printf("\n"); + cli_command_printf("\n"); /* list peers */ - cli_printf("%6s %-24s %-30s %-10s\n", + cli_command_printf("%6s %-24s %-30s %-10s\n", "LINK", "PEER-ID", "FRIENDLY-NAME", "CONNECTED"); shl_dlist_for_each(i, &wifi->links) { @@ -82,16 +82,16 @@ static int cmd_list(char **args, unsigned int n) p = peer_from_dlist(j); ++peer_cnt; - cli_printf("%6s %-24s %-30s %-10s\n", - p->l->label, - p->label, - shl_isempty(p->friendly_name) ? - "" : p->friendly_name, - p->connected ? "yes" : "no"); + cli_command_printf("%6s %-24s %-30s %-10s\n", + p->l->label, + p->label, + shl_isempty(p->friendly_name) ? + "" : p->friendly_name, + p->connected ? "yes" : "no"); } } - cli_printf("\n %u peers and %u links listed.\n", peer_cnt, link_cnt); + cli_command_printf("\n %u peers and %u links listed.\n", peer_cnt, link_cnt); return 0; } @@ -148,34 +148,34 @@ static int cmd_show(char **args, unsigned int n) } if (l) { - cli_printf("Link=%s\n", l->label); + cli_command_printf("Link=%s\n", l->label); if (l->ifindex > 0) - cli_printf("InterfaceIndex=%u\n", l->ifindex); + cli_command_printf("InterfaceIndex=%u\n", l->ifindex); if (l->ifname && *l->ifname) - cli_printf("InterfaceName=%s\n", l->ifname); + cli_command_printf("InterfaceName=%s\n", l->ifname); if (l->friendly_name && *l->friendly_name) - cli_printf("FriendlyName=%s\n", l->friendly_name); - cli_printf("P2PScanning=%d\n", l->p2p_scanning); + cli_command_printf("FriendlyName=%s\n", l->friendly_name); + cli_command_printf("P2PScanning=%d\n", l->p2p_scanning); if (l->wfd_subelements && *l->wfd_subelements) - cli_printf("WfdSubelements=%s\n", l->wfd_subelements); - cli_printf("Managed=%d\n", l->managed); + cli_command_printf("WfdSubelements=%s\n", l->wfd_subelements); + cli_command_printf("Managed=%d\n", l->managed); } else if (p) { - cli_printf("Peer=%s\n", p->label); + cli_command_printf("Peer=%s\n", p->label); if (p->p2p_mac && *p->p2p_mac) - cli_printf("P2PMac=%s\n", p->p2p_mac); + cli_command_printf("P2PMac=%s\n", p->p2p_mac); if (p->friendly_name && *p->friendly_name) - cli_printf("FriendlyName=%s\n", p->friendly_name); - cli_printf("Connected=%d\n", p->connected); + cli_command_printf("FriendlyName=%s\n", p->friendly_name); + cli_command_printf("Connected=%d\n", p->connected); if (p->interface && *p->interface) - cli_printf("Interface=%s\n", p->interface); + cli_command_printf("Interface=%s\n", p->interface); if (p->local_address && *p->local_address) - cli_printf("LocalAddress=%s\n", p->local_address); + cli_command_printf("LocalAddress=%s\n", p->local_address); if (p->remote_address && *p->remote_address) - cli_printf("RemoteAddress=%s\n", p->remote_address); + cli_command_printf("RemoteAddress=%s\n", p->remote_address); if (p->wfd_subelements && *p->wfd_subelements) - cli_printf("WfdSubelements=%s\n", p->wfd_subelements); + cli_command_printf("WfdSubelements=%s\n", p->wfd_subelements); } else { - cli_printf("Show what?\n"); + cli_command_printf("Show what?\n"); return 0; } @@ -192,7 +192,7 @@ static int cmd_set_friendly_name(char **args, unsigned int n) const char *name; if (n < 1) { - cli_printf("To what?\n"); + cli_command_printf("To what?\n"); return 0; } @@ -228,7 +228,7 @@ static int cmd_set_managed(char **args, unsigned int n) bool managed = true; if (n < 1) { - cli_printf("To what?\n"); + cli_command_printf("To what?\n"); return 0; } @@ -310,7 +310,7 @@ static int cmd_connect(char **args, unsigned int n) const char *prov, *pin; if (n < 1) { - cli_printf("To whom?\n"); + cli_command_printf("To whom?\n"); return 0; } @@ -353,7 +353,7 @@ static int cmd_disconnect(char **args, unsigned int n) struct ctl_peer *p; if (n < 1) { - cli_printf("From whom?\n"); + cli_command_printf("From whom?\n"); return 0; }