From 9d011cc13445d77855ba2b47933ac38870e90cec Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Tue, 20 Nov 2018 15:23:49 +0200 Subject: [PATCH] shared/shell: Add option to print to monitor This adds option -m/--monitor which send output to btmon using libshared bt_log: = bluetoothctl: power on = bluetoothctl: Changing power on succeeded Change-Id: If99dc6a7090ed9b477beafe4b90fa59f357fb8b3 Signed-off-by: himanshu --- src/shared/shell.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/shared/shell.c b/src/shared/shell.c index 2410464..89a2e82 100644 --- a/src/shared/shell.c +++ b/src/shared/shell.c @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -45,6 +46,7 @@ #include "src/shared/util.h" #include "src/shared/queue.h" #include "src/shared/shell.h" +#include "src/shared/log.h" #define CMD_LENGTH 48 #define print_text(color, fmt, args...) \ @@ -70,6 +72,7 @@ static struct { int argc; char **argv; bool mode; + bool monitor; int timeout; struct io *input; @@ -521,6 +524,12 @@ void bt_shell_printf(const char *fmt, ...) vprintf(fmt, args); va_end(args); + if (data.monitor) { + va_start(args, fmt); + bt_log_vprintf(0xffff, data.name, LOG_INFO, fmt, args); + va_end(args); + } + if (save_input) { if (!data.saved_prompt) rl_restore_prompt(); @@ -613,6 +622,9 @@ static void rl_handler(char *input) if (history_search(input, -1)) add_history(input); + if (data.monitor) + bt_log_printf(0xffff, data.name, LOG_INFO, "%s", input); + if (wordexp(input, &w, WRDE_NOCMD)) goto done; @@ -988,6 +1000,7 @@ static const struct option main_options[] = { { "version", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { "timeout", required_argument, 0, 't' }, + { "monitor", no_argument, 0, 'm' }, }; static void usage(int argc, char **argv, const struct bt_shell_opt *opt) @@ -1003,7 +1016,8 @@ static void usage(int argc, char **argv, const struct bt_shell_opt *opt) for (i = 0; opt && opt->options[i].name; i++) printf("\t--%s \t%s\n", opt->options[i].name, opt->help[i]); - printf("\t--timeout \tTimeout in seconds for non-interactive mode\n" + printf("\t--monitor \tEnable monitor output\n" + "\t--timeout \tTimeout in seconds for non-interactive mode\n" "\t--version \tDisplay version\n" "\t--help \t\tDisplay help\n"); } @@ -1022,9 +1036,9 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt) if (opt) { memcpy(options + offset, opt->options, sizeof(struct option) * opt->optno); - snprintf(optstr, sizeof(optstr), "+hvt:%s", opt->optstr); + snprintf(optstr, sizeof(optstr), "+mhvt:%s", opt->optstr); } else - snprintf(optstr, sizeof(optstr), "+hvt:"); + snprintf(optstr, sizeof(optstr), "+mhvt:"); data.name = strrchr(argv[0], '/'); if (!data.name) @@ -1047,6 +1061,13 @@ void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt) case 't': data.timeout = atoi(optarg); break; + case 'm': + data.monitor = true; + if (bt_log_open() < 0) { + data.monitor = false; + printf("Unable to open logging channel\n"); + } + break; default: if (index < 0) { for (index = 0; options[index].val; index++) { @@ -1131,6 +1152,9 @@ void bt_shell_cleanup(void) data.envs = NULL; } + if (data.monitor) + bt_log_close(); + rl_cleanup(); data.init = false; -- 2.7.4