From a468f581285a42142546e94325c253f7293c677d Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Mon, 1 Aug 2022 11:19:32 +0100 Subject: [PATCH] Use int debug level --- examples/captive-dns-server/main.c | 2 +- examples/device-dashboard/main.c | 2 +- examples/esp32/device-dashboard/main/main.c | 2 +- examples/esp32/uart-bridge/main/cli.c | 2 +- examples/esp32/uart-bridge/main/main.c | 2 +- examples/esp8266/src/main/main.c | 2 +- examples/file-upload-html-form/main.c | 2 +- examples/file-upload-multiple-posts/main.c | 2 +- examples/file-upload-single-post/main.c | 2 +- examples/http-client/main.c | 4 +-- examples/http-restful-server/main.c | 4 +-- examples/http-reverse-proxy/main.c | 8 ++--- examples/http-server/main.c | 6 ++-- examples/http-streaming-client/main.c | 2 +- examples/json-rpc-over-websocket/main.c | 6 ++-- examples/mip-pcap/main.c | 6 ++-- examples/mqtt-over-ws-client/main.c | 8 ++--- examples/multi-threaded/main.c | 2 +- examples/sntp-time-sync/main.c | 6 ++-- examples/socks5-server/main.c | 2 +- examples/stm32/nucleo-f746zg-baremetal/main.c | 6 ++-- examples/stm32/stm32-freertos-tcp/main.c | 2 +- examples/tcp/main.c | 4 +-- examples/timers/main.c | 8 ++--- examples/uart-bridge/main.c | 2 +- examples/uart-bridge/net.c | 2 +- examples/websocket-client/main.c | 2 +- examples/webui-login/main.c | 2 +- examples/webui-plain/main.c | 2 +- examples/webui-push-ws/main.c | 6 ++-- examples/webui-rest/main.c | 2 +- examples/zephyr/http-client/src/main.c | 3 +- examples/zephyr/http-server/src/main.c | 3 +- examples/zephyr/mqtt-aws-client/src/main.c | 3 +- examples/zephyr/websocket-server/src/main.c | 4 +-- mongoose.c | 36 +++++++------------ mongoose.h | 2 +- src/log.c | 26 ++++---------- src/log.h | 2 +- src/sock.c | 10 +++--- test/fuzz.c | 2 +- test/unit_test.c | 2 +- 42 files changed, 89 insertions(+), 114 deletions(-) diff --git a/examples/captive-dns-server/main.c b/examples/captive-dns-server/main.c index a38182b9..e69e501b 100644 --- a/examples/captive-dns-server/main.c +++ b/examples/captive-dns-server/main.c @@ -48,7 +48,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { int main(void) { struct mg_mgr mgr; - mg_log_set("3"); // Set log level + mg_log_set(MG_LL_DEBUG); // Set log level mg_mgr_init(&mgr); // Initialise event manager mg_listen(&mgr, s_listen_url, fn, NULL); // Start DNS server for (;;) mg_mgr_poll(&mgr, 1000); // Event loop diff --git a/examples/device-dashboard/main.c b/examples/device-dashboard/main.c index 87dcfa11..a60c6739 100644 --- a/examples/device-dashboard/main.c +++ b/examples/device-dashboard/main.c @@ -8,7 +8,7 @@ void device_dashboard_fn(struct mg_connection *, int, void *, void *); int main(void) { struct mg_mgr mgr; - mg_log_set("2"); // Set to 3 for debug, to 4 for very verbose level + mg_log_set(MG_LL_DEBUG); // Set debug log level mg_mgr_init(&mgr); mg_http_listen(&mgr, s_listening_url, device_dashboard_fn, &mgr); MG_INFO(("Listening on %s", s_listening_url)); diff --git a/examples/esp32/device-dashboard/main/main.c b/examples/esp32/device-dashboard/main/main.c index 13fd6fa9..e221beb0 100644 --- a/examples/esp32/device-dashboard/main/main.c +++ b/examples/esp32/device-dashboard/main/main.c @@ -27,7 +27,7 @@ void app_main(void) { // Connected to WiFi, now start HTTP server struct mg_mgr mgr; - mg_log_set("3"); + mg_log_set(MG_LL_DEBUG); // Set log level mg_mgr_init(&mgr); MG_INFO(("Mongoose v%s on %s", MG_VERSION, s_listening_url)); mg_http_listen(&mgr, s_listening_url, device_dashboard_fn, &mgr); diff --git a/examples/esp32/uart-bridge/main/cli.c b/examples/esp32/uart-bridge/main/cli.c index 74cd220d..bf938773 100644 --- a/examples/esp32/uart-bridge/main/cli.c +++ b/examples/esp32/uart-bridge/main/cli.c @@ -71,7 +71,7 @@ void cli(uint8_t input_byte) { } else if (strcmp(buf0, "reboot") == 0) { esp_restart(); } else if (strcmp(buf0, "ll") == 0) { - mg_log_set(buf1); + mg_log_set(atoi(buf1)); } else if (strcmp(buf0, "wifi") == 0) { cli_wifi(buf1, buf2); } else { diff --git a/examples/esp32/uart-bridge/main/main.c b/examples/esp32/uart-bridge/main/main.c index a38e125d..0474d105 100644 --- a/examples/esp32/uart-bridge/main/main.c +++ b/examples/esp32/uart-bridge/main/main.c @@ -42,7 +42,7 @@ void app_main(void) { // Connected to WiFi, now start HTTP server struct mg_mgr mgr; mg_mgr_init(&mgr); - mg_log_set("3"); + mg_log_set(MG_LL_DEBUG); // Set log level MG_INFO(("Mongoose v%s on %s", MG_VERSION, s_listening_url)); mg_http_listen(&mgr, s_listening_url, uart_bridge_fn, &mgr); for (;;) mg_mgr_poll(&mgr, 10); // Infinite event loop diff --git a/examples/esp8266/src/main/main.c b/examples/esp8266/src/main/main.c index eefff8a9..125a478b 100644 --- a/examples/esp8266/src/main/main.c +++ b/examples/esp8266/src/main/main.c @@ -34,7 +34,7 @@ static void cb2(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { // Called after we're connected to WiFi network static void run_mongoose(void) { struct mg_mgr mgr; - mg_log_set("3"); + mg_log_set(MG_LL_DEBUG); // Set log level mg_mgr_init(&mgr); mg_http_listen(&mgr, SERVER_URL, cb, &mgr); // Listening server mg_http_connect(&mgr, CLIENT_URL, cb2, &mgr); // Example client diff --git a/examples/file-upload-html-form/main.c b/examples/file-upload-html-form/main.c index 21443a01..ec243929 100644 --- a/examples/file-upload-html-form/main.c +++ b/examples/file-upload-html-form/main.c @@ -41,7 +41,7 @@ int main(void) { struct mg_mgr mgr; mg_mgr_init(&mgr); - mg_log_set("3"); + mg_log_set(MG_LL_DEBUG); // Set log level mg_http_listen(&mgr, "http://localhost:8000", cb, NULL); for (;;) mg_mgr_poll(&mgr, 50); diff --git a/examples/file-upload-multiple-posts/main.c b/examples/file-upload-multiple-posts/main.c index 8dc36338..5476c466 100644 --- a/examples/file-upload-multiple-posts/main.c +++ b/examples/file-upload-multiple-posts/main.c @@ -30,7 +30,7 @@ int main(void) { struct mg_timer t1; mg_mgr_init(&mgr); - mg_log_set("3"); + mg_log_set(MG_LL_DEBUG); // Set log level mg_http_listen(&mgr, "http://localhost:8000", cb, NULL); for (;;) mg_mgr_poll(&mgr, 50); diff --git a/examples/file-upload-single-post/main.c b/examples/file-upload-single-post/main.c index b8fb26db..9472354d 100644 --- a/examples/file-upload-single-post/main.c +++ b/examples/file-upload-single-post/main.c @@ -37,7 +37,7 @@ int main(void) { struct mg_mgr mgr; mg_mgr_init(&mgr); - mg_log_set("3"); + mg_log_set(MG_LL_DEBUG); // Set debug log level mg_http_listen(&mgr, "http://localhost:8000", cb, NULL); for (;;) mg_mgr_poll(&mgr, 50); diff --git a/examples/http-client/main.c b/examples/http-client/main.c index bddd12c7..03985e42 100644 --- a/examples/http-client/main.c +++ b/examples/http-client/main.c @@ -5,7 +5,7 @@ // print the response and exit. // You can change `s_url` from the command line by executing: ./example YOUR_URL // -// To enable SSL/TLS, make SSL=OPENSSL or make SSL=MBEDTLS +// To enable SSL/TLS, make SSL=OPENSSL or make SSL=MBEDTLS #include "mongoose.h" @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) { struct mg_mgr mgr; // Event manager bool done = false; // Event handler flips it to true if (argc > 1) s_url = argv[1]; // Use URL provided in the command line - mg_log_set(log_level); // Set to 0 to disable debug + mg_log_set(atoi(log_level)); // Set to 0 to disable debug mg_mgr_init(&mgr); // Initialise event manager mg_http_connect(&mgr, s_url, fn, &done); // Create client connection while (!done) mg_mgr_poll(&mgr, 50); // Event manager loops until 'done' diff --git a/examples/http-restful-server/main.c b/examples/http-restful-server/main.c index 9f4df1fd..af17e25b 100644 --- a/examples/http-restful-server/main.c +++ b/examples/http-restful-server/main.c @@ -9,7 +9,7 @@ // any other URI serves static files from s_root_dir // // To enable SSL/TLS (using self-signed certificates in PEM files), -// 1. make SSL=OPENSSL or make SSL=MBEDTLS +// 1. make SSL=OPENSSL or make SSL=MBEDTLS // 2. curl -k https://127.0.0.1:8443 #include "mongoose.h" @@ -58,7 +58,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { int main(void) { struct mg_mgr mgr; // Event manager - mg_log_set("2"); // Set to 3 to enable debug + mg_log_set(MG_LL_DEBUG); // Set log level mg_mgr_init(&mgr); // Initialise event manager mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener mg_http_listen(&mgr, s_https_addr, fn, (void *) 1); // HTTPS listener diff --git a/examples/http-reverse-proxy/main.c b/examples/http-reverse-proxy/main.c index 1b55019a..ce65fe26 100644 --- a/examples/http-reverse-proxy/main.c +++ b/examples/http-reverse-proxy/main.c @@ -7,11 +7,11 @@ // // To enable SSL/TLS, add SSL=OPENSSL or SSL=MBEDTLS -static const char *s_backend_url = +static const char *s_backend_url = #if defined(MG_ENABLE_MBEDTLS) || defined(MG_ENABLE_OPENSSL) - "https://cesanta.com"; + "https://cesanta.com"; #else - "http://info.cern.ch"; + "http://info.cern.ch"; #endif static const char *s_listen_url = "http://localhost:8000"; @@ -75,7 +75,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { int main(void) { struct mg_mgr mgr; - mg_log_set("3"); // Set log level + mg_log_set(MG_LL_DEBUG); // Set log level mg_mgr_init(&mgr); // Initialise event manager mg_http_listen(&mgr, s_listen_url, fn, NULL); // Start proxy for (;;) mg_mgr_poll(&mgr, 1000); // Event loop diff --git a/examples/http-server/main.c b/examples/http-server/main.c index 319ee253..f2efbc5f 100644 --- a/examples/http-server/main.c +++ b/examples/http-server/main.c @@ -4,7 +4,7 @@ #include #include "mongoose.h" -static const char *s_debug_level = "2"; +static int s_debug_level = MG_LL_INFO; static const char *s_root_dir = "."; static const char *s_listening_address = "http://0.0.0.0:8000"; static const char *s_enable_hexdump = "no"; @@ -44,7 +44,7 @@ static void usage(const char *prog) { " -S PAT - SSI filename pattern, default: '%s'\n" " -d DIR - directory to serve, default: '%s'\n" " -l ADDR - listening address, default: '%s'\n" - " -v LEVEL - debug level, from 0 to 4, default: '%s'\n", + " -v LEVEL - debug level, from 0 to 4, default: %d\n", MG_VERSION, prog, s_enable_hexdump, s_ssi_pattern, s_root_dir, s_listening_address, s_debug_level); exit(EXIT_FAILURE); @@ -67,7 +67,7 @@ int main(int argc, char *argv[]) { } else if (strcmp(argv[i], "-l") == 0) { s_listening_address = argv[++i]; } else if (strcmp(argv[i], "-v") == 0) { - s_debug_level = argv[++i]; + s_debug_level = atoi(argv[++i]); } else { usage(argv[0]); } diff --git a/examples/http-streaming-client/main.c b/examples/http-streaming-client/main.c index eb72363c..e4a0e00f 100644 --- a/examples/http-streaming-client/main.c +++ b/examples/http-streaming-client/main.c @@ -60,7 +60,7 @@ int main(int argc, char *argv[]) { bool done = false; // Event handler flips it to true const char *log_level = getenv("V"); // Log level if (log_level == NULL) log_level = "3"; // If not set, set to DEBUG - mg_log_set(log_level); // Set to 0 to disable debug log + mg_log_set(atoi(log_level)); // Set to 0 to disable debug log if (argc > 1) s_url = argv[1]; // Use URL from command line mg_mgr_init(&mgr); // Initialise event manager mg_http_connect(&mgr, s_url, fn, &done); // Create client connection diff --git a/examples/json-rpc-over-websocket/main.c b/examples/json-rpc-over-websocket/main.c index 8579c4f5..e837c565 100644 --- a/examples/json-rpc-over-websocket/main.c +++ b/examples/json-rpc-over-websocket/main.c @@ -67,9 +67,9 @@ static void timer_fn(void *arg) { } int main(void) { - struct mg_mgr mgr; // Event manager - mg_mgr_init(&mgr); // Init event manager - mg_log_set("3"); + struct mg_mgr mgr; // Event manager + mg_mgr_init(&mgr); // Init event manager + mg_log_set(MG_LL_DEBUG); // Set log level mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT, timer_fn, &mgr); // Init timer // Configure JSON-RPC functions we're going to handle diff --git a/examples/mip-pcap/main.c b/examples/mip-pcap/main.c index 9c06c325..423b016e 100644 --- a/examples/mip-pcap/main.c +++ b/examples/mip-pcap/main.c @@ -68,9 +68,9 @@ int main(int argc, char *argv[]) { const char *bpf = NULL; // "host x.x.x.x or ether host ff:ff:ff:ff:ff:ff"; char errbuf[PCAP_ERRBUF_SIZE] = ""; - struct mg_mgr mgr; // Event manager - mg_mgr_init(&mgr); // Initialise event manager - mg_log_set("3"); // Set debug log level + struct mg_mgr mgr; // Event manager + mg_mgr_init(&mgr); // Initialise event manager + mg_log_set(MG_LL_DEBUG); // Set debug log level mg_timer_add(&mgr, 3000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr); // Parse options diff --git a/examples/mqtt-over-ws-client/main.c b/examples/mqtt-over-ws-client/main.c index b760204e..76107dab 100644 --- a/examples/mqtt-over-ws-client/main.c +++ b/examples/mqtt-over-ws-client/main.c @@ -80,10 +80,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } int main(void) { - struct mg_mgr mgr; // Event manager - bool done = false; // Event handler flips it to true when done - mg_mgr_init(&mgr); // Initialise event manager - mg_log_set("4"); // Set debug log level + struct mg_mgr mgr; // Event manager + bool done = false; // Event handler flips it to true when done + mg_mgr_init(&mgr); // Initialise event manager + mg_log_set(MG_LL_DEBUG); // Set log level mg_ws_connect(&mgr, s_url, fn, &done, NULL); // Create client connection while (done == false) mg_mgr_poll(&mgr, 1000); // Event loop mg_mgr_free(&mgr); // Finished, cleanup diff --git a/examples/multi-threaded/main.c b/examples/multi-threaded/main.c index 603d6562..a74308bf 100644 --- a/examples/multi-threaded/main.c +++ b/examples/multi-threaded/main.c @@ -78,7 +78,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { int main(void) { struct mg_mgr mgr; mg_mgr_init(&mgr); - mg_log_set("3"); + mg_log_set(MG_LL_DEBUG); // Set debug log level mg_http_listen(&mgr, "http://localhost:8000", fn, NULL); // Create listener for (;;) mg_mgr_poll(&mgr, 1000); // Event loop mg_mgr_free(&mgr); // Cleanup diff --git a/examples/sntp-time-sync/main.c b/examples/sntp-time-sync/main.c index a5192b06..8e89e8d2 100644 --- a/examples/sntp-time-sync/main.c +++ b/examples/sntp-time-sync/main.c @@ -42,9 +42,9 @@ static void timer_fn(void *arg) { } int main(void) { - struct mg_mgr mgr; // Event manager - mg_mgr_init(&mgr); // Initialise event manager - mg_log_set("3"); // Set debug log level + struct mg_mgr mgr; // Event manager + mg_mgr_init(&mgr); // Initialise event manager + mg_log_set(MG_LL_DEBUG); // Set log level mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr); for (;;) mg_mgr_poll(&mgr, 300); // Infinite event loop mg_mgr_free(&mgr); // Free manager resources diff --git a/examples/socks5-server/main.c b/examples/socks5-server/main.c index a7d7483b..f3eab398 100644 --- a/examples/socks5-server/main.c +++ b/examples/socks5-server/main.c @@ -164,7 +164,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { int main(void) { struct mg_mgr mgr; // Event manager - mg_log_set("3"); // Set to 0 to disable debug + mg_log_set(MG_LL_DEBUG); // Set log level mg_mgr_init(&mgr); // Initialise event manager mg_listen(&mgr, s_lsn, fn, NULL); // Create client connection while (true) mg_mgr_poll(&mgr, 1000); // Infinite event loop diff --git a/examples/stm32/nucleo-f746zg-baremetal/main.c b/examples/stm32/nucleo-f746zg-baremetal/main.c index 8bd7df48..29cd1d68 100644 --- a/examples/stm32/nucleo-f746zg-baremetal/main.c +++ b/examples/stm32/nucleo-f746zg-baremetal/main.c @@ -124,9 +124,9 @@ int main(void) { RCC->AHB1RSTR |= BIT(25); // ETHMAC force reset RCC->AHB1RSTR &= ~BIT(25); // ETHMAC release reset - struct mg_mgr mgr; // Initialise Mongoose event manager - mg_mgr_init(&mgr); // and attach it to the MIP interface - mg_log_set("2"); + struct mg_mgr mgr; // Initialise Mongoose event manager + mg_mgr_init(&mgr); // and attach it to the MIP interface + mg_log_set(MG_LL_DEBUG); // Set log level mg_timer_add(&mgr, 1000, MG_TIMER_REPEAT, blink_cb, &mgr); mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT, sntp_cb, &mgr); mg_http_listen(&mgr, "http://0.0.0.0:80", fn, NULL); diff --git a/examples/stm32/stm32-freertos-tcp/main.c b/examples/stm32/stm32-freertos-tcp/main.c index 1c918fd8..48086617 100644 --- a/examples/stm32/stm32-freertos-tcp/main.c +++ b/examples/stm32/stm32-freertos-tcp/main.c @@ -4,7 +4,7 @@ #include "device.h" #include "mongoose.h" -static const char *s_debug_level = "4"; +static int s_debug_level = MG_LL_VERBOSE; static const char *s_listening_address = "http://0.0.0.0:80"; // Event handler for the listening connection. diff --git a/examples/tcp/main.c b/examples/tcp/main.c index b9ea76fb..84d1797a 100644 --- a/examples/tcp/main.c +++ b/examples/tcp/main.c @@ -79,8 +79,8 @@ int main(void) { struct mg_mgr mgr; // Event manager struct mg_connection *c; - mg_log_set("2"); // Set to 0 to disable debug - mg_mgr_init(&mgr); // Initialize event manager + mg_log_set(MG_LL_DEBUG); // Set log level + mg_mgr_init(&mgr); // Initialize event manager mg_timer_add(&mgr, 15000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr); // Init timer for demo purposes, 15s c = mg_listen(&mgr, s_lsn, sfn, NULL); // Create server connection diff --git a/examples/timers/main.c b/examples/timers/main.c index ee835c09..65c88009 100644 --- a/examples/timers/main.c +++ b/examples/timers/main.c @@ -42,12 +42,12 @@ static void timer_fn(void *arg) { } int main(void) { - struct mg_mgr mgr; // Event manager - mg_mgr_init(&mgr); // Initialise event manager - mg_log_set("3"); // Set debug log level + struct mg_mgr mgr; // Event manager + mg_mgr_init(&mgr); // Initialise event manager + mg_log_set(MG_LL_DEBUG); // Set log level mg_timer_add(&mgr, 1000, MG_TIMER_REPEAT, timer_fn, &mgr); mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener - for (;;) mg_mgr_poll(&mgr, 500); // Infinite event loop + for (;;) mg_mgr_poll(&mgr, 500); // Infinite event loop mg_mgr_free(&mgr); // Free manager resources return 0; } diff --git a/examples/uart-bridge/main.c b/examples/uart-bridge/main.c index f77f8538..378087d4 100644 --- a/examples/uart-bridge/main.c +++ b/examples/uart-bridge/main.c @@ -8,7 +8,7 @@ void uart_bridge_fn(struct mg_connection *, int, void *, void *); int main(void) { struct mg_mgr mgr; - mg_log_set("2"); // Set to 3 for debug, to 4 for very verbose level + mg_log_set(MG_LL_DEBUG); mg_mgr_init(&mgr); mg_http_listen(&mgr, s_listening_url, uart_bridge_fn, &mgr); MG_INFO(("Listening on %s", s_listening_url)); diff --git a/examples/uart-bridge/net.c b/examples/uart-bridge/net.c index b1d5ffa0..c308b70c 100644 --- a/examples/uart-bridge/net.c +++ b/examples/uart-bridge/net.c @@ -181,7 +181,7 @@ void uart_bridge_fn(struct mg_connection *c, int ev, void *ev_data, s_state.mqtt.url = strdup(DEFAULT_MQTT); mg_timer_add(c->mgr, 20, MG_TIMER_REPEAT, timer_fn, c->mgr); uart_init(s_state.tx, s_state.rx, s_state.baud); - // mg_log_set("3"); + // mg_log_set(MG_LL_DEBUG); // Set log level } else if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/api/hi")) { diff --git a/examples/websocket-client/main.c b/examples/websocket-client/main.c index 3aa847e1..bc21ea7c 100644 --- a/examples/websocket-client/main.c +++ b/examples/websocket-client/main.c @@ -35,7 +35,7 @@ int main(void) { bool done = false; // Event handler flips it to true struct mg_connection *c; // Client connection mg_mgr_init(&mgr); // Initialise event manager - mg_log_set("4"); + mg_log_set(MG_LL_DEBUG); // Set log level c = mg_ws_connect(&mgr, s_url, fn, &done, NULL); // Create client while (c && done == false) mg_mgr_poll(&mgr, 1000); // Wait for echo mg_mgr_free(&mgr); // Deallocate resources diff --git a/examples/webui-login/main.c b/examples/webui-login/main.c index 63ac9207..e651eadf 100644 --- a/examples/webui-login/main.c +++ b/examples/webui-login/main.c @@ -66,7 +66,7 @@ void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { int main(void) { struct mg_mgr mgr; - mg_log_set("2"); // Set to 3 for debug, to 4 for very verbose level + mg_log_set(MG_LL_DEBUG); mg_mgr_init(&mgr); mg_http_listen(&mgr, s_listening_url, fn, &mgr); while (mgr.conns != NULL) mg_mgr_poll(&mgr, 500); diff --git a/examples/webui-plain/main.c b/examples/webui-plain/main.c index 65338c02..cc91674a 100644 --- a/examples/webui-plain/main.c +++ b/examples/webui-plain/main.c @@ -54,7 +54,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { int main(void) { struct mg_mgr mgr; // Event manager - mg_log_set("2"); // Set to 3 to enable debug + mg_log_set(MG_LL_INFO); // Set to 3 to enable debug mg_mgr_init(&mgr); // Initialise event manager mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop diff --git a/examples/webui-push-ws/main.c b/examples/webui-push-ws/main.c index 90fdf3a0..a9ff7aa3 100644 --- a/examples/webui-push-ws/main.c +++ b/examples/webui-push-ws/main.c @@ -43,9 +43,9 @@ static void timer_fn(void *arg) { } int main(void) { - struct mg_mgr mgr; // Event manager - mg_mgr_init(&mgr); // Initialise event manager - mg_log_set("2"); // Set debug log level + struct mg_mgr mgr; // Event manager + mg_mgr_init(&mgr); // Initialise event manager + mg_log_set(MG_LL_DEBUG); // Set log level mg_timer_add(&mgr, 2000, MG_TIMER_REPEAT, timer_fn, &mgr); mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener for (;;) mg_mgr_poll(&mgr, 500); // Infinite event loop diff --git a/examples/webui-rest/main.c b/examples/webui-rest/main.c index 288e9f8a..608b47e1 100644 --- a/examples/webui-rest/main.c +++ b/examples/webui-rest/main.c @@ -39,7 +39,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { int main(void) { struct mg_mgr mgr; // Event manager - mg_log_set("2"); // Set to 3 to enable debug + mg_log_set(MG_LL_DEBUG); // Set log level mg_mgr_init(&mgr); // Initialise event manager mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop diff --git a/examples/zephyr/http-client/src/main.c b/examples/zephyr/http-client/src/main.c index 91d3cd7f..92c7389d 100644 --- a/examples/zephyr/http-client/src/main.c +++ b/examples/zephyr/http-client/src/main.c @@ -4,7 +4,6 @@ #include "certs.h" #include "mongoose.h" -static const char *s_debug_level = "3"; static time_t s_boot_timestamp = 0; static struct mg_connection *s_sntp_conn = NULL; static const char *s_url = "https://example.org/"; @@ -89,7 +88,7 @@ static void timer_fn(void *arg) { } int main(int argc, char *argv[]) { - mg_log_set(s_debug_level); + mg_log_set(MG_LL_DEBUG); mg_mgr_init(&s_mgr); mg_timer_add(&s_mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, diff --git a/examples/zephyr/http-server/src/main.c b/examples/zephyr/http-server/src/main.c index 7fe4ca03..bfae63b6 100644 --- a/examples/zephyr/http-server/src/main.c +++ b/examples/zephyr/http-server/src/main.c @@ -4,7 +4,6 @@ #include "certs.h" #include "mongoose.h" -static const char *s_debug_level = "3"; static const char *s_web_dir = "/"; static const char *s_http_addr = "http://0.0.0.0:8000"; static const char *s_https_addr = "https://0.0.0.0:8443"; @@ -72,7 +71,7 @@ static void timer_fn(void *arg) { int main(int argc, char *argv[]) { struct mg_mgr mgr; - mg_log_set(s_debug_level); + mg_log_set(MG_LL_DEBUG); mg_mgr_init(&mgr); mg_http_listen(&mgr, s_http_addr, wcb, NULL); diff --git a/examples/zephyr/mqtt-aws-client/src/main.c b/examples/zephyr/mqtt-aws-client/src/main.c index 6d36fa3a..bbe732d1 100644 --- a/examples/zephyr/mqtt-aws-client/src/main.c +++ b/examples/zephyr/mqtt-aws-client/src/main.c @@ -6,7 +6,6 @@ struct mg_mgr mgr; -static const char *s_debug_level = "3"; static time_t s_boot_timestamp = 0; static struct mg_connection *s_sntp_conn = NULL; static const char *s_url = @@ -87,7 +86,7 @@ static void timer_fn(void *arg) { } int main(int argc, char *argv[]) { - mg_log_set(s_debug_level); + mg_log_set(MG_LL_DEBUG); mg_mgr_init(&mgr); mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr); diff --git a/examples/zephyr/websocket-server/src/main.c b/examples/zephyr/websocket-server/src/main.c index 886dbafc..38fd0533 100644 --- a/examples/zephyr/websocket-server/src/main.c +++ b/examples/zephyr/websocket-server/src/main.c @@ -4,7 +4,6 @@ #include "certs.h" #include "mongoose.h" -static const char *s_debug_level = "3"; static const char *s_web_dir = "/"; static const char *s_ws_addr = "ws://0.0.0.0:8000"; static const char *s_wss_addr = "wss://0.0.0.0:8443"; @@ -66,9 +65,8 @@ static void timer_fn(void *arg) { int main(int argc, char *argv[]) { struct mg_mgr mgr; - mg_log_set(s_debug_level); - mg_mgr_init(&mgr); + mg_log_set(MG_LL_DEBUG); mg_http_listen(&mgr, s_ws_addr, fn, NULL); mg_http_listen(&mgr, s_wss_addr, fn, &mgr); mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr); diff --git a/mongoose.c b/mongoose.c index 8cbd7701..dfc30613 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2708,7 +2708,7 @@ static void default_logger(unsigned char c) { (void) c; } -static const char *s_spec = "2"; +static int s_level = MG_LL_INFO; static void (*s_log_func)(unsigned char) = default_logger; void mg_log_set_fn(void (*fn)(unsigned char)) { @@ -2724,29 +2724,17 @@ static void logs(const char *buf, size_t len) { for (i = 0; i < len; i++) logc(((unsigned char *) buf)[i]); } -void mg_log_set(const char *spec) { - MG_DEBUG(("Setting log level to %s", spec)); - s_spec = spec; +void mg_log_set(int log_level) { + MG_DEBUG(("Setting log level to %d", log_level)); + s_level = log_level; } bool mg_log_prefix(int level, const char *file, int line, const char *fname) { - // static unsigned long seq; - int max = MG_LL_INFO; - struct mg_str k, v, s = mg_str(s_spec); - const char *p = strrchr(file, '/'); - - if (p == NULL) p = strrchr(file, '\\'); - p = p == NULL ? file : p + 1; - - while (mg_commalist(&s, &k, &v)) { - if (v.len == 0) max = atoi(k.ptr); - if (v.len > 0 && strncmp(p, k.ptr, k.len) == 0) max = atoi(v.ptr); - } - - if (level <= max) { + if (level <= s_level) { + const char *p = strrchr(file, MG_DIRSEP); char buf[41]; size_t n = mg_snprintf(buf, sizeof(buf), "%llx %d %s:%d:%s", mg_millis(), - level, p, line, fname); + level, p == NULL ? fname : p + 1, line, fname); if (n > sizeof(buf) - 2) n = sizeof(buf) - 2; while (n < sizeof(buf)) buf[n++] = ' '; logs(buf, n - 1); @@ -4238,8 +4226,9 @@ static void read_conn(struct mg_connection *c) { char *buf = (char *) &c->recv.buf[c->recv.len]; size_t len = c->recv.size - c->recv.len; n = c->is_tls ? mg_tls_recv(c, buf, len) : mg_sock_recv(c, buf, len); - MG_DEBUG(("%lu %p %d:%d %ld err %d", c->id, c->fd, (int) c->send.len, - (int) c->recv.len, n, MG_SOCK_ERRNO)); + MG_DEBUG(("%lu %p snd %ld/%ld rcv %ld/%ld n=%ld err=%d", c->id, c->fd, + (long) c->send.len, (long) c->send.size, (long) c->recv.len, + (long) c->recv.size, n, MG_SOCK_ERRNO)); iolog(c, buf, n, true); } } @@ -4248,8 +4237,9 @@ static void write_conn(struct mg_connection *c) { char *buf = (char *) c->send.buf; size_t len = c->send.len; long n = c->is_tls ? mg_tls_send(c, buf, len) : mg_sock_send(c, buf, len); - MG_DEBUG(("%lu %p %d:%d %ld err %d", c->id, c->fd, (int) c->send.len, - (int) c->recv.len, n, MG_SOCK_ERRNO)); + MG_DEBUG(("%lu %p snd %ld/%ld rcv %ld/%ld n=%ld err=%d", c->id, c->fd, + (long) c->send.len, (long) c->send.size, (long) c->recv.len, + (long) c->recv.size, n, MG_SOCK_ERRNO)); iolog(c, buf, n, false); } diff --git a/mongoose.h b/mongoose.h index 930e5e17..a5344aad 100644 --- a/mongoose.h +++ b/mongoose.h @@ -775,7 +775,7 @@ char *mg_vmprintf(const char *fmt, va_list ap); enum { MG_LL_NONE, MG_LL_ERROR, MG_LL_INFO, MG_LL_DEBUG, MG_LL_VERBOSE }; void mg_log(const char *fmt, ...); bool mg_log_prefix(int ll, const char *file, int line, const char *fname); -void mg_log_set(const char *spec); +void mg_log_set(int log_level); void mg_hexdump(const void *buf, size_t len); void mg_log_set_fn(void (*logfunc)(unsigned char ch)); diff --git a/src/log.c b/src/log.c index 0caca063..7dcf9095 100644 --- a/src/log.c +++ b/src/log.c @@ -8,7 +8,7 @@ static void default_logger(unsigned char c) { (void) c; } -static const char *s_spec = "2"; +static int s_level = MG_LL_INFO; static void (*s_log_func)(unsigned char) = default_logger; void mg_log_set_fn(void (*fn)(unsigned char)) { @@ -24,29 +24,17 @@ static void logs(const char *buf, size_t len) { for (i = 0; i < len; i++) logc(((unsigned char *) buf)[i]); } -void mg_log_set(const char *spec) { - MG_DEBUG(("Setting log level to %s", spec)); - s_spec = spec; +void mg_log_set(int log_level) { + MG_DEBUG(("Setting log level to %d", log_level)); + s_level = log_level; } bool mg_log_prefix(int level, const char *file, int line, const char *fname) { - // static unsigned long seq; - int max = MG_LL_INFO; - struct mg_str k, v, s = mg_str(s_spec); - const char *p = strrchr(file, '/'); - - if (p == NULL) p = strrchr(file, '\\'); - p = p == NULL ? file : p + 1; - - while (mg_commalist(&s, &k, &v)) { - if (v.len == 0) max = atoi(k.ptr); - if (v.len > 0 && strncmp(p, k.ptr, k.len) == 0) max = atoi(v.ptr); - } - - if (level <= max) { + if (level <= s_level) { + const char *p = strrchr(file, MG_DIRSEP); char buf[41]; size_t n = mg_snprintf(buf, sizeof(buf), "%llx %d %s:%d:%s", mg_millis(), - level, p, line, fname); + level, p == NULL ? fname : p + 1, line, fname); if (n > sizeof(buf) - 2) n = sizeof(buf) - 2; while (n < sizeof(buf)) buf[n++] = ' '; logs(buf, n - 1); diff --git a/src/log.h b/src/log.h index 0450ed2b..aa0b203c 100644 --- a/src/log.h +++ b/src/log.h @@ -6,7 +6,7 @@ enum { MG_LL_NONE, MG_LL_ERROR, MG_LL_INFO, MG_LL_DEBUG, MG_LL_VERBOSE }; void mg_log(const char *fmt, ...); bool mg_log_prefix(int ll, const char *file, int line, const char *fname); -void mg_log_set(const char *spec); +void mg_log_set(int log_level); void mg_hexdump(const void *buf, size_t len); void mg_log_set_fn(void (*logfunc)(unsigned char ch)); diff --git a/src/sock.c b/src/sock.c index 5b5cacce..2e42ba89 100644 --- a/src/sock.c +++ b/src/sock.c @@ -292,8 +292,9 @@ static void read_conn(struct mg_connection *c) { char *buf = (char *) &c->recv.buf[c->recv.len]; size_t len = c->recv.size - c->recv.len; n = c->is_tls ? mg_tls_recv(c, buf, len) : mg_sock_recv(c, buf, len); - MG_DEBUG(("%lu %p %d:%d %ld err %d", c->id, c->fd, (int) c->send.len, - (int) c->recv.len, n, MG_SOCK_ERRNO)); + MG_DEBUG(("%lu %p snd %ld/%ld rcv %ld/%ld n=%ld err=%d", c->id, c->fd, + (long) c->send.len, (long) c->send.size, (long) c->recv.len, + (long) c->recv.size, n, MG_SOCK_ERRNO)); iolog(c, buf, n, true); } } @@ -302,8 +303,9 @@ static void write_conn(struct mg_connection *c) { char *buf = (char *) c->send.buf; size_t len = c->send.len; long n = c->is_tls ? mg_tls_send(c, buf, len) : mg_sock_send(c, buf, len); - MG_DEBUG(("%lu %p %d:%d %ld err %d", c->id, c->fd, (int) c->send.len, - (int) c->recv.len, n, MG_SOCK_ERRNO)); + MG_DEBUG(("%lu %p snd %ld/%ld rcv %ld/%ld n=%ld err=%d", c->id, c->fd, + (long) c->send.len, (long) c->send.size, (long) c->recv.len, + (long) c->recv.size, n, MG_SOCK_ERRNO)); iolog(c, buf, n, false); } diff --git a/test/fuzz.c b/test/fuzz.c index b96183ae..120bcd84 100644 --- a/test/fuzz.c +++ b/test/fuzz.c @@ -7,7 +7,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *, size_t); #endif int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - mg_log_set("0"); + mg_log_set(MG_LL_NONE); struct mg_dns_message dm; mg_dns_parse(data, size, &dm); diff --git a/test/unit_test.c b/test/unit_test.c index 1a901e96..9d4653b1 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -2483,7 +2483,7 @@ static void test_rpc(void) { int main(void) { const char *debug_level = getenv("V"); if (debug_level == NULL) debug_level = "3"; - mg_log_set(debug_level); + mg_log_set(atoi(debug_level)); test_json(); test_rpc();