diff --git a/include/libwebsockets/lws-misc.h b/include/libwebsockets/lws-misc.h index f58563b40..6bffd85d4 100644 --- a/include/libwebsockets/lws-misc.h +++ b/include/libwebsockets/lws-misc.h @@ -269,6 +269,22 @@ lws_parse_uri(char *p, const char **prot, const char **ads, int *port, LWS_VISIBLE LWS_EXTERN const char * lws_cmdline_option(int argc, const char **argv, const char *val); +/** + * lws_cmdline_option_handle_builtin(): apply standard cmdline options + * + * \param argc: count of argument strings + * \param argv: argument strings + * \param info: context creation info + * + * Applies standard options to the context creation info to save them having + * to be (unevenly) copied into the minimal examples. + * + * Applies default log levels that can be overriden by -d + */ +LWS_VISIBLE LWS_EXTERN void +lws_cmdline_option_handle_builtin(int argc, const char **argv, + struct lws_context_creation_info *info); + /** * lws_now_secs(): return seconds since 1970-1-1 */ diff --git a/lib/core/libwebsockets.c b/lib/core/libwebsockets.c index 738c17b07..79c55b8f9 100644 --- a/lib/core/libwebsockets.c +++ b/lib/core/libwebsockets.c @@ -987,6 +987,8 @@ lws_cmdline_option(int argc, const char **argv, const char *val) return argv[c + 1]; } + if (argv[c][n] == '=') + return &argv[c][n + 1]; return argv[c] + n; } } @@ -994,6 +996,42 @@ lws_cmdline_option(int argc, const char **argv, const char *val) return NULL; } +static const char * const builtins[] = { + "-d", + "--udp-tx-loss", + "--udp-rx-loss" +}; + +void +lws_cmdline_option_handle_builtin(int argc, const char **argv, + struct lws_context_creation_info *info) +{ + const char *p; + int n, m, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE; + + for (n = 0; n < (int)LWS_ARRAY_SIZE(builtins); n++) { + p = lws_cmdline_option(argc, argv, builtins[n]); + if (!p) + continue; + + m = atoi(p); + + switch (n) { + case 0: + logs = m; + break; + case 1: + info->udp_loss_sim_tx_pc = m; + break; + case 2: + info->udp_loss_sim_rx_pc = m; + break; + } + } + + lws_set_log_level(logs, NULL); +} + const lws_humanize_unit_t humanize_schema_si[] = { { "Pi ", LWS_PI }, { "Ti ", LWS_TI }, { "Gi ", LWS_GI }, diff --git a/minimal-examples/http-client/minimal-http-client/minimal-http-client.c b/minimal-examples/http-client/minimal-http-client/minimal-http-client.c index 06c82eb01..63dc6ee61 100644 --- a/minimal-examples/http-client/minimal-http-client/minimal-http-client.c +++ b/minimal-examples/http-client/minimal-http-client/minimal-http-client.c @@ -204,30 +204,19 @@ int main(int argc, const char **argv) lws_state_notify_link_t *na[] = { ¬ifier, NULL }; struct lws_context_creation_info info; struct lws_context *context; - const char *p; struct args args; - int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE - /* - * For LLL_ verbosity above NOTICE to be built into lws, - * lws must have been configured and built with - * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE - * - * | LLL_INFO | LLL_PARSER | LLL_HEADER | LLL_EXT | - * LLL_CLIENT | LLL_LATENCY | LLL_DEBUG - */ ; + int n = 0; args.argc = argc; args.argv = argv; signal(SIGINT, sigint_handler); - if ((p = lws_cmdline_option(argc, argv, "-d"))) - logs = atoi(p); + memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ + lws_cmdline_option_handle_builtin(argc, argv, &info); - lws_set_log_level(logs, NULL); lwsl_user("LWS minimal http client [-d] [-l] [--h1]\n"); - memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */ info.protocols = protocols;