1
0
Fork 0
mirror of https://github.com/albfan/miraclecast.git synced 2025-03-09 23:38:56 +00:00

Do not prefix command output with time

This commit is contained in:
Alberto Fanjul 2022-11-08 08:34:45 +01:00
parent 8cd144271a
commit f9a61faaa2
4 changed files with 106 additions and 87 deletions

View file

@ -57,7 +57,7 @@ static bool is_cli(void)
return cli_rl; 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_PROTECT_ERRNO;
_shl_free_ char *line = NULL; _shl_free_ char *line = NULL;
@ -76,6 +76,22 @@ void cli_printv(const char *fmt, va_list args)
rl_redisplay(); 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; long long sec, usec;
time_t now; time_t now;
struct tm *timeinfo; struct tm *timeinfo;
@ -106,15 +122,16 @@ void cli_printv(const char *fmt, va_list args)
printf("[%s] ", buffer); printf("[%s] ", buffer);
else if (log__have_time()) else if (log__have_time())
printf("[%.4lld.%.6lld] ", sec, usec); printf("[%.4lld.%.6lld] ", sec, usec);
}
vprintf(fmt, args); void cli_command_printf(const char *fmt, ...)
{
SHL_PROTECT_ERRNO;
va_list args;
if (async) { va_start(args, fmt);
rl_restore_prompt(); cli_printv(fmt, false, args);
rl_replace_line(line, 0); va_end(args);
rl_point = point;
rl_redisplay();
}
} }
void cli_printf(const char *fmt, ...) void cli_printf(const char *fmt, ...)
@ -123,7 +140,7 @@ void cli_printf(const char *fmt, ...)
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
cli_printv(fmt, args); cli_printv(fmt, true, args);
va_end(args); va_end(args);
} }
@ -131,7 +148,7 @@ int cli_help(const struct cli_cmd *cmds, int whitespace)
{ {
unsigned int i; unsigned int i;
cli_printf("Available commands:\n"); cli_command_printf("Available commands:\n");
for (i = 0; cmds[i].cmd; ++i) { for (i = 0; cmds[i].cmd; ++i) {
if (!cmds[i].desc) if (!cmds[i].desc)
@ -141,7 +158,7 @@ int cli_help(const struct cli_cmd *cmds, int whitespace)
if (!is_cli() && cmds[i].cli_cmp == CLI_Y) if (!is_cli() && cmds[i].cli_cmp == CLI_Y)
continue; continue;
cli_printf(" %s %-*s %s\n", cli_command_printf(" %s %-*s %s\n",
cmds[i].cmd, cmds[i].cmd,
(int)(whitespace - strlen(cmds[i].cmd)), (int)(whitespace - strlen(cmds[i].cmd)),
cmds[i].args ? : "", cmds[i].args ? : "",
@ -174,21 +191,21 @@ int cli_do(const struct cli_cmd *cmds, char **args, unsigned int n)
switch (cmds[i].argc_cmp) { switch (cmds[i].argc_cmp) {
case CLI_EQUAL: case CLI_EQUAL:
if (n != cmds[i].argc) { if (n != cmds[i].argc) {
cli_printf("Invalid number of arguments\n"); cli_command_printf("Invalid number of arguments\n");
return -EINVAL; return -EINVAL;
} }
break; break;
case CLI_MORE: case CLI_MORE:
if (n < cmds[i].argc) { if (n < cmds[i].argc) {
cli_printf("too few arguments\n"); cli_command_printf("too few arguments\n");
return -EINVAL; return -EINVAL;
} }
break; break;
case CLI_LESS: case CLI_LESS:
if (n > cmds[i].argc) { if (n > cmds[i].argc) {
cli_printf("too many arguments\n"); cli_command_printf("too many arguments\n");
return -EINVAL; return -EINVAL;
} }
@ -234,7 +251,7 @@ static void cli_handler_fn(char *input)
if (r != -EAGAIN) if (r != -EAGAIN)
return; return;
cli_printf("Command not found\n"); cli_command_printf("Command not found\n");
} }
static int cli_stdin_fn(sd_event_source *source, static int cli_stdin_fn(sd_event_source *source,

View file

@ -123,8 +123,10 @@ bool ctl_sink_is_closed(struct ctl_sink *s);
/* CLI handling */ /* CLI handling */
extern unsigned int cli_max_sev; 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_printf(const char *fmt, ...);
void cli_command_printf(const char *fmt, ...);
#define cli_log(_fmt, ...) \ #define cli_log(_fmt, ...) \
cli_printf(_fmt "\n", ##__VA_ARGS__) cli_printf(_fmt "\n", ##__VA_ARGS__)

View file

@ -91,14 +91,14 @@ static int cmd_list(char **args, unsigned int n)
/* list links */ /* list links */
cli_printf("%6s %-24s %-30s %-10s\n", cli_command_printf("%6s %-24s %-30s %-10s\n",
"LINK", "INTERFACE", "FRIENDLY-NAME", "MANAGED"); "LINK", "INTERFACE", "FRIENDLY-NAME", "MANAGED");
shl_dlist_for_each(i, &wifi->links) { shl_dlist_for_each(i, &wifi->links) {
l = link_from_dlist(i); l = link_from_dlist(i);
++link_cnt; ++link_cnt;
cli_printf("%6s %-24s %-30s %-10s\n", cli_command_printf("%6s %-24s %-30s %-10s\n",
l->label, l->label,
shl_isempty(l->ifname) ? shl_isempty(l->ifname) ?
"<unknown>" : l->ifname, "<unknown>" : l->ifname,
@ -107,11 +107,11 @@ static int cmd_list(char **args, unsigned int n)
l->managed ? "yes": "no"); l->managed ? "yes": "no");
} }
cli_printf("\n"); cli_command_printf("\n");
/* list peers */ /* list peers */
cli_printf("%6s %-24s %-30s %-10s\n", cli_command_printf("%6s %-24s %-30s %-10s\n",
"LINK", "PEER-ID", "FRIENDLY-NAME", "CONNECTED"); "LINK", "PEER-ID", "FRIENDLY-NAME", "CONNECTED");
shl_dlist_for_each(i, &wifi->links) { shl_dlist_for_each(i, &wifi->links) {
@ -121,7 +121,7 @@ static int cmd_list(char **args, unsigned int n)
p = peer_from_dlist(j); p = peer_from_dlist(j);
++peer_cnt; ++peer_cnt;
cli_printf("%6s %-24s %-30s %-10s\n", cli_command_printf("%6s %-24s %-30s %-10s\n",
p->l->label, p->l->label,
p->label, p->label,
shl_isempty(p->friendly_name) ? shl_isempty(p->friendly_name) ?
@ -130,7 +130,7 @@ static int cmd_list(char **args, unsigned int n)
} }
} }
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; return 0;
} }
@ -155,34 +155,34 @@ static int cmd_show(char **args, unsigned int n)
} }
if (l) { if (l) {
cli_printf("Link=%s\n", l->label); cli_command_printf("Link=%s\n", l->label);
if (l->ifindex > 0) if (l->ifindex > 0)
cli_printf("InterfaceIndex=%u\n", l->ifindex); cli_command_printf("InterfaceIndex=%u\n", l->ifindex);
if (l->ifname && *l->ifname) 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) if (l->friendly_name && *l->friendly_name)
cli_printf("FriendlyName=%s\n", l->friendly_name); cli_command_printf("FriendlyName=%s\n", l->friendly_name);
cli_printf("P2PScanning=%d\n", l->p2p_scanning); cli_command_printf("P2PScanning=%d\n", l->p2p_scanning);
if (l->wfd_subelements && *l->wfd_subelements) if (l->wfd_subelements && *l->wfd_subelements)
cli_printf("WfdSubelements=%s\n", l->wfd_subelements); cli_command_printf("WfdSubelements=%s\n", l->wfd_subelements);
cli_printf("Managed=%d\n", l->managed); cli_command_printf("Managed=%d\n", l->managed);
} else if (p) { } 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) 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) if (p->friendly_name && *p->friendly_name)
cli_printf("FriendlyName=%s\n", p->friendly_name); cli_command_printf("FriendlyName=%s\n", p->friendly_name);
cli_printf("Connected=%d\n", p->connected); cli_command_printf("Connected=%d\n", p->connected);
if (p->interface && *p->interface) 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) 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) 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) 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 { } else {
cli_printf("Show what?\n"); cli_command_printf("Show what?\n");
return 0; return 0;
} }

View file

@ -52,14 +52,14 @@ static int cmd_list(char **args, unsigned int n)
/* list links */ /* list links */
cli_printf("%6s %-24s %-30s %-10s\n", cli_command_printf("%6s %-24s %-30s %-10s\n",
"LINK", "INTERFACE", "FRIENDLY-NAME", "MANAGED"); "LINK", "INTERFACE", "FRIENDLY-NAME", "MANAGED");
shl_dlist_for_each(i, &wifi->links) { shl_dlist_for_each(i, &wifi->links) {
l = link_from_dlist(i); l = link_from_dlist(i);
++link_cnt; ++link_cnt;
cli_printf("%6s %-24s %-30s %-10s\n", cli_command_printf("%6s %-24s %-30s %-10s\n",
l->label, l->label,
shl_isempty(l->ifname) ? shl_isempty(l->ifname) ?
"<unknown>" : l->ifname, "<unknown>" : l->ifname,
@ -68,11 +68,11 @@ static int cmd_list(char **args, unsigned int n)
l->managed ? "yes": "no"); l->managed ? "yes": "no");
} }
cli_printf("\n"); cli_command_printf("\n");
/* list peers */ /* list peers */
cli_printf("%6s %-24s %-30s %-10s\n", cli_command_printf("%6s %-24s %-30s %-10s\n",
"LINK", "PEER-ID", "FRIENDLY-NAME", "CONNECTED"); "LINK", "PEER-ID", "FRIENDLY-NAME", "CONNECTED");
shl_dlist_for_each(i, &wifi->links) { shl_dlist_for_each(i, &wifi->links) {
@ -82,7 +82,7 @@ static int cmd_list(char **args, unsigned int n)
p = peer_from_dlist(j); p = peer_from_dlist(j);
++peer_cnt; ++peer_cnt;
cli_printf("%6s %-24s %-30s %-10s\n", cli_command_printf("%6s %-24s %-30s %-10s\n",
p->l->label, p->l->label,
p->label, p->label,
shl_isempty(p->friendly_name) ? shl_isempty(p->friendly_name) ?
@ -91,7 +91,7 @@ static int cmd_list(char **args, unsigned int n)
} }
} }
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; return 0;
} }
@ -148,34 +148,34 @@ static int cmd_show(char **args, unsigned int n)
} }
if (l) { if (l) {
cli_printf("Link=%s\n", l->label); cli_command_printf("Link=%s\n", l->label);
if (l->ifindex > 0) if (l->ifindex > 0)
cli_printf("InterfaceIndex=%u\n", l->ifindex); cli_command_printf("InterfaceIndex=%u\n", l->ifindex);
if (l->ifname && *l->ifname) 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) if (l->friendly_name && *l->friendly_name)
cli_printf("FriendlyName=%s\n", l->friendly_name); cli_command_printf("FriendlyName=%s\n", l->friendly_name);
cli_printf("P2PScanning=%d\n", l->p2p_scanning); cli_command_printf("P2PScanning=%d\n", l->p2p_scanning);
if (l->wfd_subelements && *l->wfd_subelements) if (l->wfd_subelements && *l->wfd_subelements)
cli_printf("WfdSubelements=%s\n", l->wfd_subelements); cli_command_printf("WfdSubelements=%s\n", l->wfd_subelements);
cli_printf("Managed=%d\n", l->managed); cli_command_printf("Managed=%d\n", l->managed);
} else if (p) { } 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) 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) if (p->friendly_name && *p->friendly_name)
cli_printf("FriendlyName=%s\n", p->friendly_name); cli_command_printf("FriendlyName=%s\n", p->friendly_name);
cli_printf("Connected=%d\n", p->connected); cli_command_printf("Connected=%d\n", p->connected);
if (p->interface && *p->interface) 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) 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) 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) 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 { } else {
cli_printf("Show what?\n"); cli_command_printf("Show what?\n");
return 0; return 0;
} }
@ -192,7 +192,7 @@ static int cmd_set_friendly_name(char **args, unsigned int n)
const char *name; const char *name;
if (n < 1) { if (n < 1) {
cli_printf("To what?\n"); cli_command_printf("To what?\n");
return 0; return 0;
} }
@ -228,7 +228,7 @@ static int cmd_set_managed(char **args, unsigned int n)
bool managed = true; bool managed = true;
if (n < 1) { if (n < 1) {
cli_printf("To what?\n"); cli_command_printf("To what?\n");
return 0; return 0;
} }
@ -310,7 +310,7 @@ static int cmd_connect(char **args, unsigned int n)
const char *prov, *pin; const char *prov, *pin;
if (n < 1) { if (n < 1) {
cli_printf("To whom?\n"); cli_command_printf("To whom?\n");
return 0; return 0;
} }
@ -353,7 +353,7 @@ static int cmd_disconnect(char **args, unsigned int n)
struct ctl_peer *p; struct ctl_peer *p;
if (n < 1) { if (n < 1) {
cli_printf("From whom?\n"); cli_command_printf("From whom?\n");
return 0; return 0;
} }