From 7e2ca72310d81ac05324319c8e3bf33dc5c552f1 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Fri, 20 May 2022 09:45:17 +0100 Subject: [PATCH] Remove mg_log_set_callback. Use MG_PUTCHAR macro instead --- docs/README.md | 31 +--- examples/device-dashboard/Makefile | 6 +- examples/device-dashboard/packed_fs.c | 164 +++++++++++--------- examples/device-dashboard/web_root/main.js | 9 +- examples/zephyr/http-client/src/main.c | 6 - examples/zephyr/http-server/src/main.c | 6 - examples/zephyr/mqtt-aws-client/src/main.c | 6 - examples/zephyr/websocket-server/src/main.c | 6 - mongoose.c | 31 +--- mongoose.h | 8 +- src/arch_rtx.h | 2 - src/arch_zephyr.h | 1 + src/config.h | 4 + src/log.c | 31 +--- src/log.h | 1 - 15 files changed, 124 insertions(+), 188 deletions(-) diff --git a/docs/README.md b/docs/README.md index 6ee76447..b7a1c288 100644 --- a/docs/README.md +++ b/docs/README.md @@ -306,12 +306,13 @@ Here is a list of build constants and their default values: |MG_ENABLE_CUSTOM_MILLIS | 0 | Enable custom `mg_millis()` function | |MG_ENABLE_PACKED_FS | 0 | Enable embedded FS support | |MG_ENABLE_FATFS | 0 | Enable embedded FAT FS support | +|MG_ENABLE_LINES | undefined | If defined, show source file names in logs | |MG_IO_SIZE | 2048 | Granularity of the send/recv IO buffer growth | |MG_MAX_RECV_BUF_SIZE | (3 * 1024 * 1024) | Maximum recv buffer size | |MG_MAX_HTTP_HEADERS | 40 | Maximum number of HTTP headers | |MG_HTTP_INDEX | "index.html" | Index file for HTML directory | |MG_FATFS_ROOT | "/" | FAT FS root directory | -|MG_ENABLE_LINES | undefined | If defined, show source file names in logs | +|MG_PUTCHAR | putchar | Character output function, used by logging | NOTE: the `MG_IO_SIZE` constant also sets maximum UDP message size, see @@ -3468,34 +3469,6 @@ mg_log_set("2"); // Set log level to info mg_log_set("2,foo.c=3,bar.c=0"); // Set log level to info, with overrides ``` -### mg\_log\_set\_callback() - -```c -void mg_log_set_callback(void (*fn)(const void *, size_t, void *), void *fnd); -``` - -By default, `LOG` writes to standard output stream (aka `stdout`), but this behaviour -can be changes via `mg_log_set_callback`. This function allows to set callback, -which called once mongoose (or host application) calls `LOG` - -Parameters: -- `fn` - callback function, should be called on logging -- `fnd` - user parameter to pass to `fn` - -Return value: none - -Usage example: - -```c -void log_via_printf(const void *buf, size_t len, void *userdata) { - (void) userdata; - printf("*.%s", buf, len); -} - -// ... -mg_log_set_callback(&log_via_printf, NULL); -``` - ### mg\_hexdump() ```c diff --git a/examples/device-dashboard/Makefile b/examples/device-dashboard/Makefile index a99908b0..963a7dc1 100644 --- a/examples/device-dashboard/Makefile +++ b/examples/device-dashboard/Makefile @@ -10,19 +10,19 @@ MINGW ?= $(DOCKER) mdashnet/mingw all: $(PROG) $(RUN) ./$(PROG) -$(PROG): +$(PROG): $(SOURCES) $(FILES_TO_EMBED) $(CC) ../../test/pack.c -o pack ./pack $(FILES_TO_EMBED) > packed_fs.c $(CC) -W -Wall -Wextra -O0 -g3 $(CFLAGS) -o $(PROG) $(SOURCES) -mingw: +mingw: $(SOURCES) $(FILES_TO_EMBED) $(MINGW) i686-w64-mingw32-gcc $(CFLAGS) ../../test/pack.c -o pack.exe $(MINGW) wine cmd /c 'pack.exe $(FILES_TO_EMBED) > packed_fs.c' $(MINGW) i686-w64-mingw32-gcc $(CFLAGS) $(SOURCES) -lws2_32 -o $(PROG).exe $(MINGW) wine $(PROG).exe -vc98: +vc98: $(SOURCES) $(FILES_TO_EMBED) $(VC98) cl.exe /nologo $(CFLAGS) ../../test/pack.c /Fepack.exe $(VC98) cmd /c 'pack.exe $(FILES_TO_EMBED) > packed_fs.c' $(VC98) cl.exe /nologo /O2 $(CFLAGS) $(SOURCES) /Fe$(PROG).exe diff --git a/examples/device-dashboard/packed_fs.c b/examples/device-dashboard/packed_fs.c index 749db3cc..3221cc26 100644 --- a/examples/device-dashboard/packed_fs.c +++ b/examples/device-dashboard/packed_fs.c @@ -639,11 +639,18 @@ static const unsigned char v2[] = { 105, 103, 93, 32, 61, 32, 117, 115, 101, 83, 116, 97, // ig] = useSta 116, 101, 40, 123, 125, 41, 59, 10, 10, 32, 32, 99, // te({});.. c 111, 110, 115, 116, 32, 114, 101, 102, 114, 101, 115, 104, // onst refresh - 32, 61, 32, 40, 41, 32, 61, 62, 10, 32, 32, 32, // = () =>. - 32, 32, 32, 102, 101, 116, 99, 104, 40, 39, 47, 97, // fetch('/a - 112, 105, 47, 99, 111, 110, 102, 105, 103, 47, 103, 101, // pi/config/ge - 116, 39, 41, 46, 116, 104, 101, 110, 40, 114, 32, 61, // t').then(r = - 62, 32, 114, 46, 106, 115, 111, 110, 40, 41, 41, 46, // > r.json()). + 32, 61, 32, 40, 41, 32, 61, 62, 32, 102, 101, 116, // = () => fet + 99, 104, 40, 39, 47, 97, 112, 105, 47, 99, 111, 110, // ch('/api/con + 102, 105, 103, 47, 103, 101, 116, 39, 44, 32, 123, 104, // fig/get', {h + 101, 97, 100, 101, 114, 115, 58, 32, 123, 65, 117, 116, // eaders: {Aut + 104, 111, 114, 105, 122, 97, 116, 105, 111, 110, 58, 32, // horization: + 39, 39, 125, 125, 41, 10, 32, 32, 32, 32, 32, 32, // ''}}). + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, // + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 116, // .t + 104, 101, 110, 40, 114, 32, 61, 62, 32, 114, 46, 106, // hen(r => r.j + 115, 111, 110, 40, 41, 41, 10, 32, 32, 32, 32, 32, // son()). + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, // + 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, // . 116, 104, 101, 110, 40, 114, 32, 61, 62, 32, 115, 101, // then(r => se 116, 67, 111, 110, 102, 105, 103, 40, 114, 41, 41, 59, // tConfig(r)); 10, 10, 32, 32, 99, 111, 110, 115, 116, 32, 108, 111, // .. const lo @@ -709,80 +716,85 @@ static const unsigned char v2[] = { 32, 32, 32, 32, 32, 125, 41, 59, 10, 32, 32, 32, // });. 32, 125, 59, 10, 32, 32, 32, 32, 102, 101, 116, 99, // };. fetc 104, 40, 39, 47, 97, 112, 105, 47, 119, 97, 116, 99, // h('/api/watc - 104, 39, 41, 10, 32, 32, 32, 32, 32, 32, 32, 32, // h'). - 46, 116, 104, 101, 110, 40, 114, 32, 61, 62, 32, 114, // .then(r => r - 46, 98, 111, 100, 121, 46, 103, 101, 116, 82, 101, 97, // .body.getRea - 100, 101, 114, 40, 41, 41, 10, 32, 32, 32, 32, 32, // der()). - 32, 32, 32, 46, 116, 104, 101, 110, 40, 102, 41, 10, // .then(f). - 32, 32, 32, 32, 32, 32, 32, 32, 46, 99, 97, 116, // .cat - 99, 104, 40, 101, 32, 61, 62, 32, 115, 101, 116, 84, // ch(e => setT - 105, 109, 101, 111, 117, 116, 40, 119, 97, 116, 99, 104, // imeout(watch - 44, 32, 49, 48, 48, 48, 41, 41, 59, 10, 32, 32, // , 1000));. - 125, 59, 10, 10, 32, 32, 117, 115, 101, 69, 102, 102, // };.. useEff - 101, 99, 116, 40, 40, 41, 32, 61, 62, 32, 123, 10, // ect(() => {. - 32, 32, 32, 32, 102, 101, 116, 99, 104, 40, 39, 47, // fetch('/ - 97, 112, 105, 47, 108, 111, 103, 105, 110, 39, 41, 10, // api/login'). + 104, 39, 44, 32, 123, 104, 101, 97, 100, 101, 114, 115, // h', {headers + 58, 32, 123, 65, 117, 116, 104, 111, 114, 105, 122, 97, // : {Authoriza + 116, 105, 111, 110, 58, 32, 39, 39, 125, 125, 41, 10, // tion: ''}}). 32, 32, 32, 32, 32, 32, 32, 32, 46, 116, 104, 101, // .the - 110, 40, 114, 32, 61, 62, 32, 114, 46, 106, 115, 111, // n(r => r.jso - 110, 40, 41, 41, 10, 32, 32, 32, 32, 32, 32, 32, // n()). - 32, 46, 116, 104, 101, 110, 40, 114, 32, 61, 62, 32, // .then(r => - 108, 111, 103, 105, 110, 40, 114, 41, 41, 10, 32, 32, // login(r)). - 32, 32, 32, 32, 32, 32, 46, 99, 97, 116, 99, 104, // .catch - 40, 101, 114, 114, 32, 61, 62, 32, 115, 101, 116, 85, // (err => setU - 115, 101, 114, 40, 39, 39, 41, 41, 59, 10, 32, 32, // ser(''));. - 32, 32, 114, 101, 102, 114, 101, 115, 104, 40, 41, 59, // refresh(); - 10, 32, 32, 32, 32, 119, 97, 116, 99, 104, 40, 41, // . watch() - 59, 10, 32, 32, 125, 44, 32, 91, 93, 41, 59, 10, // ;. }, []);. - 10, 32, 32, 105, 102, 32, 40, 33, 117, 115, 101, 114, // . if (!user - 41, 32, 114, 101, 116, 117, 114, 110, 32, 104, 116, 109, // ) return htm - 108, 96, 60, 36, 123, 76, 111, 103, 105, 110, 125, 32, // l`<${Login} - 108, 111, 103, 105, 110, 61, 36, 123, 108, 111, 103, 105, // login=${logi - 110, 125, 32, 47, 62, 96, 59, 10, 32, 32, 99, 111, // n} />`;. co - 110, 115, 116, 32, 97, 100, 109, 105, 110, 32, 61, 32, // nst admin = - 117, 115, 101, 114, 32, 61, 61, 32, 39, 97, 100, 109, // user == 'adm - 105, 110, 39, 59, 10, 32, 32, 99, 111, 110, 115, 116, // in';. const - 32, 99, 111, 108, 115, 105, 122, 101, 32, 61, 32, 97, // colsize = a - 100, 109, 105, 110, 32, 63, 32, 39, 99, 52, 39, 32, // dmin ? 'c4' - 58, 32, 39, 99, 54, 39, 59, 10, 32, 32, 99, 111, // : 'c6';. co - 110, 115, 116, 32, 99, 115, 32, 61, 32, 97, 100, 109, // nst cs = adm - 105, 110, 32, 63, 32, 104, 116, 109, 108, 96, 60, 36, // in ? html`<$ - 123, 67, 104, 97, 110, 103, 101, 83, 101, 116, 116, 105, // {ChangeSetti - 110, 103, 115, 125, 32, 99, 111, 110, 102, 105, 103, 61, // ngs} config= - 36, 123, 99, 111, 110, 102, 105, 103, 125, 32, 47, 62, // ${config} /> - 96, 32, 58, 32, 39, 39, 59, 10, 32, 32, 114, 101, // ` : '';. re - 116, 117, 114, 110, 32, 104, 116, 109, 108, 96, 10, 60, // turn html`.< - 36, 123, 78, 97, 118, 125, 32, 117, 115, 101, 114, 61, // ${Nav} user= - 36, 123, 117, 115, 101, 114, 125, 32, 108, 111, 103, 111, // ${user} logo - 117, 116, 61, 36, 123, 108, 111, 103, 111, 117, 116, 125, // ut=${logout} - 32, 47, 62, 10, 60, 100, 105, 118, 32, 99, 108, 97, // />.
. <${Her - 111, 125, 32, 47, 62, 10, 32, 32, 60, 100, 105, 118, // o} />.
.
<$ - 123, 83, 104, 111, 119, 83, 101, 116, 116, 105, 110, 103, // {ShowSetting - 115, 125, 32, 99, 111, 110, 102, 105, 103, 61, 36, 123, // s} config=${ - 99, 111, 110, 102, 105, 103, 125, 32, 47, 62, 60, 47, // config} /> r.bod + 121, 46, 103, 101, 116, 82, 101, 97, 100, 101, 114, 40, // y.getReader( + 41, 41, 10, 32, 32, 32, 32, 32, 32, 32, 32, 46, // )). . + 116, 104, 101, 110, 40, 102, 41, 10, 32, 32, 32, 32, // then(f). + 32, 32, 32, 32, 46, 99, 97, 116, 99, 104, 40, 101, // .catch(e + 32, 61, 62, 32, 115, 101, 116, 84, 105, 109, 101, 111, // => setTimeo + 117, 116, 40, 119, 97, 116, 99, 104, 44, 32, 49, 48, // ut(watch, 10 + 48, 48, 41, 41, 59, 10, 32, 32, 125, 59, 10, 10, // 00));. };.. + 32, 32, 117, 115, 101, 69, 102, 102, 101, 99, 116, 40, // useEffect( + 40, 41, 32, 61, 62, 32, 123, 10, 32, 32, 32, 32, // () => {. + 102, 101, 116, 99, 104, 40, 39, 47, 97, 112, 105, 47, // fetch('/api/ + 108, 111, 103, 105, 110, 39, 44, 32, 123, 104, 101, 97, // login', {hea + 100, 101, 114, 115, 58, 32, 123, 65, 117, 116, 104, 111, // ders: {Autho + 114, 105, 122, 97, 116, 105, 111, 110, 58, 32, 39, 39, // rization: '' + 125, 125, 41, 10, 32, 32, 32, 32, 32, 32, 32, 32, // }}). + 46, 116, 104, 101, 110, 40, 114, 32, 61, 62, 32, 114, // .then(r => r + 46, 106, 115, 111, 110, 40, 41, 41, 10, 32, 32, 32, // .json()). + 32, 32, 32, 32, 32, 46, 116, 104, 101, 110, 40, 114, // .then(r + 32, 61, 62, 32, 108, 111, 103, 105, 110, 40, 114, 41, // => login(r) + 41, 10, 32, 32, 32, 32, 32, 32, 32, 32, 46, 99, // ). .c + 97, 116, 99, 104, 40, 101, 114, 114, 32, 61, 62, 32, // atch(err => + 115, 101, 116, 85, 115, 101, 114, 40, 39, 39, 41, 41, // setUser('')) + 59, 10, 32, 32, 32, 32, 114, 101, 102, 114, 101, 115, // ;. refres + 104, 40, 41, 59, 10, 32, 32, 32, 32, 119, 97, 116, // h();. wat + 99, 104, 40, 41, 59, 10, 32, 32, 125, 44, 32, 91, // ch();. }, [ + 93, 41, 59, 10, 10, 32, 32, 105, 102, 32, 40, 33, // ]);.. if (! + 117, 115, 101, 114, 41, 32, 114, 101, 116, 117, 114, 110, // user) return + 32, 104, 116, 109, 108, 96, 60, 36, 123, 76, 111, 103, // html`<${Log + 105, 110, 125, 32, 108, 111, 103, 105, 110, 61, 36, 123, // in} login=${ + 108, 111, 103, 105, 110, 125, 32, 47, 62, 96, 59, 10, // login} />`;. + 32, 32, 99, 111, 110, 115, 116, 32, 97, 100, 109, 105, // const admi + 110, 32, 61, 32, 117, 115, 101, 114, 32, 61, 61, 32, // n = user == + 39, 97, 100, 109, 105, 110, 39, 59, 10, 32, 32, 99, // 'admin';. c + 111, 110, 115, 116, 32, 99, 111, 108, 115, 105, 122, 101, // onst colsize + 32, 61, 32, 97, 100, 109, 105, 110, 32, 63, 32, 39, // = admin ? ' + 99, 52, 39, 32, 58, 32, 39, 99, 54, 39, 59, 10, // c4' : 'c6';. + 32, 32, 99, 111, 110, 115, 116, 32, 99, 115, 32, 61, // const cs = + 32, 97, 100, 109, 105, 110, 32, 63, 32, 104, 116, 109, // admin ? htm + 108, 96, 60, 36, 123, 67, 104, 97, 110, 103, 101, 83, // l`<${ChangeS + 101, 116, 116, 105, 110, 103, 115, 125, 32, 99, 111, 110, // ettings} con + 102, 105, 103, 61, 36, 123, 99, 111, 110, 102, 105, 103, // fig=${config + 125, 32, 47, 62, 96, 32, 58, 32, 39, 39, 59, 10, // } />` : '';. + 32, 32, 114, 101, 116, 117, 114, 110, 32, 104, 116, 109, // return htm + 108, 96, 10, 60, 36, 123, 78, 97, 118, 125, 32, 117, // l`.<${Nav} u + 115, 101, 114, 61, 36, 123, 117, 115, 101, 114, 125, 32, // ser=${user} + 108, 111, 103, 111, 117, 116, 61, 36, 123, 108, 111, 103, // logout=${log + 111, 117, 116, 125, 32, 47, 62, 10, 60, 100, 105, 118, // out} />.
. <$ + 123, 72, 101, 114, 111, 125, 32, 47, 62, 10, 32, 32, // {Hero} />. + 60, 100, 105, 118, 32, 99, 108, 97, 115, 115, 61, 34, //
. <${ShowSet + 116, 105, 110, 103, 115, 125, 32, 99, 111, 110, 102, 105, // tings} confi + 103, 61, 36, 123, 99, 111, 110, 102, 105, 103, 125, 32, // g=${config} + 47, 62, 60, 47, 100, 105, 118, 62, 10, 32, 32, 32, // />
. + 32, 60, 100, 105, 118, 32, 99, 108, 97, 115, 115, 61, //
${cs}. ${cs}
- 10, 32, 32, 32, 32, 60, 100, 105, 118, 32, 99, 108, // .
<${ - 67, 104, 97, 116, 125, 32, 109, 101, 115, 115, 97, 103, // Chat} messag - 101, 115, 61, 36, 123, 109, 101, 115, 115, 97, 103, 101, // es=${message - 115, 125, 32, 47, 62, 60, 47, 100, 105, 118, 62, 10, // s} />
. - 32, 32, 60, 47, 100, 105, 118, 62, 10, 32, 32, 60, //
. < - 36, 123, 70, 111, 111, 116, 101, 114, 125, 32, 47, 62, // ${Footer} /> - 10, 60, 47, 100, 105, 118, 62, 96, 59, 10, 125, 59, // .
`;.}; - 10, 10, 119, 105, 110, 100, 111, 119, 46, 111, 110, 108, // ..window.onl - 111, 97, 100, 32, 61, 32, 40, 41, 32, 61, 62, 32, // oad = () => - 114, 101, 110, 100, 101, 114, 40, 104, 40, 65, 112, 112, // render(h(App - 41, 44, 32, 100, 111, 99, 117, 109, 101, 110, 116, 46, // ), document. - 98, 111, 100, 121, 41, 59, 10, 0 // body);. + 62, 60, 36, 123, 67, 104, 97, 116, 125, 32, 109, 101, // ><${Chat} me + 115, 115, 97, 103, 101, 115, 61, 36, 123, 109, 101, 115, // ssages=${mes + 115, 97, 103, 101, 115, 125, 32, 47, 62, 60, 47, 100, // sages} />.
+ 10, 32, 32, 60, 36, 123, 70, 111, 111, 116, 101, 114, // . <${Footer + 125, 32, 47, 62, 10, 60, 47, 100, 105, 118, 62, 96, // } />.
` + 59, 10, 125, 59, 10, 10, 119, 105, 110, 100, 111, 119, // ;.};..window + 46, 111, 110, 108, 111, 97, 100, 32, 61, 32, 40, 41, // .onload = () + 32, 61, 62, 32, 114, 101, 110, 100, 101, 114, 40, 104, // => render(h + 40, 65, 112, 112, 41, 44, 32, 100, 111, 99, 117, 109, // (App), docum + 101, 110, 116, 46, 98, 111, 100, 121, 41, 59, 10, 0 // ent.body);. }; static const unsigned char v3[] = { 118, 97, 114, 32, 101, 44, 110, 44, 95, 44, 116, 44, // var e,n,_,t, @@ -2345,7 +2357,7 @@ static const struct packed_file { time_t mtime; } packed_files[] = { {"/web_root/index.html", v1, sizeof(v1), 1652432837}, - {"/web_root/main.js", v2, sizeof(v2), 1652514584}, + {"/web_root/main.js", v2, sizeof(v2), 1652995504}, {"/web_root/preact.min.js", v3, sizeof(v3), 1652374364}, {"/web_root/style.css", v4, sizeof(v4), 1652467860}, {"/web_root/user.png", v5, sizeof(v5), 1626172939}, diff --git a/examples/device-dashboard/web_root/main.js b/examples/device-dashboard/web_root/main.js index d3ed93bd..7e83fdaf 100644 --- a/examples/device-dashboard/web_root/main.js +++ b/examples/device-dashboard/web_root/main.js @@ -187,8 +187,9 @@ const App = function(props) { const [user, setUser] = useState(''); const [config, setConfig] = useState({}); - const refresh = () => - fetch('/api/config/get').then(r => r.json()).then(r => setConfig(r)); + const refresh = () => fetch('/api/config/get', {headers: {Authorization: ''}}) + .then(r => r.json()) + .then(r => setConfig(r)); const login = function(u) { document.cookie = `access_token=${u.token};path=/;max-age=3600`; @@ -214,14 +215,14 @@ const App = function(props) { if (!result.done) return f(reader); }); }; - fetch('/api/watch') + fetch('/api/watch', {headers: {Authorization: ''}}) .then(r => r.body.getReader()) .then(f) .catch(e => setTimeout(watch, 1000)); }; useEffect(() => { - fetch('/api/login') + fetch('/api/login', {headers: {Authorization: ''}}) .then(r => r.json()) .then(r => login(r)) .catch(err => setUser('')); diff --git a/examples/zephyr/http-client/src/main.c b/examples/zephyr/http-client/src/main.c index 95b0bebd..91d3cd7f 100644 --- a/examples/zephyr/http-client/src/main.c +++ b/examples/zephyr/http-client/src/main.c @@ -88,14 +88,8 @@ static void timer_fn(void *arg) { if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn); } -// Use Zephyr's printk() for Mongooose MG_* logging -static void logfn(const void *ptr, size_t len, void *userdata) { - printk("%.*s", (int) len, (char *) ptr); -} - int main(int argc, char *argv[]) { mg_log_set(s_debug_level); - mg_log_set_callback(logfn, NULL); 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 08408f80..7fe4ca03 100644 --- a/examples/zephyr/http-server/src/main.c +++ b/examples/zephyr/http-server/src/main.c @@ -69,16 +69,10 @@ static void timer_fn(void *arg) { if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn); } -// Use Zephyr's printk() for Mongooose MG_* logging -static void logfn(const void *ptr, size_t len, void *userdata) { - printk("%.*s", (int) len, (char *) ptr); -} - int main(int argc, char *argv[]) { struct mg_mgr mgr; mg_log_set(s_debug_level); - mg_log_set_callback(logfn, NULL); 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 f8464855..6d36fa3a 100644 --- a/examples/zephyr/mqtt-aws-client/src/main.c +++ b/examples/zephyr/mqtt-aws-client/src/main.c @@ -86,14 +86,8 @@ static void timer_fn(void *arg) { if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn); } -// Use Zephyr's printk() for Mongooose MG_* logging -static void logfn(const void *ptr, size_t len, void *userdata) { - printk("%.*s", (int) len, (char *) ptr); -} - int main(int argc, char *argv[]) { mg_log_set(s_debug_level); - mg_log_set_callback(logfn, NULL); 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 95065357..886dbafc 100644 --- a/examples/zephyr/websocket-server/src/main.c +++ b/examples/zephyr/websocket-server/src/main.c @@ -63,16 +63,10 @@ static void timer_fn(void *arg) { if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn); } -// Use Zephyr's printk() for Mongooose MG_* logging -static void logfn(const void *ptr, size_t len, void *userdata) { - printk("%.*s", (int) len, (char *) ptr); -} - int main(int argc, char *argv[]) { struct mg_mgr mgr; mg_log_set(s_debug_level); - mg_log_set_callback(logfn, NULL); mg_mgr_init(&mgr); mg_http_listen(&mgr, s_ws_addr, fn, NULL); diff --git a/mongoose.c b/mongoose.c index f096bcfc..42b571fb 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1997,24 +1997,17 @@ void mg_iobuf_free(struct mg_iobuf *io) { -static void logc(int c) { - putchar(c); +static const char *s_spec = "2"; + +static void logc(unsigned char c) { + MG_PUTCHAR(c); } static void logs(const char *buf, size_t len) { size_t i; - for (i = 0; i < len; i++) logc(buf[i]); + for (i = 0; i < len; i++) logc(((unsigned char *) buf)[i]); } -static void mg_log_stdout(const void *buf, size_t len, void *userdata) { - (void) userdata, (void) buf, (void) len; - logs((const char *) buf, len); -} - -static const char *s_spec = "2"; -static void (*s_fn)(const void *, size_t, void *) = mg_log_stdout; -static void *s_fn_param = NULL; - void mg_log_set(const char *spec) { MG_DEBUG(("Setting log level to %s", spec)); s_spec = spec; @@ -2026,8 +2019,6 @@ bool mg_log_prefix(int level, const char *file, int line, const char *fname) { struct mg_str k, v, s = mg_str(s_spec); const char *p = strrchr(file, '/'); - if (s_fn == NULL) return false; - if (p == NULL) p = strrchr(file, '\\'); p = p == NULL ? file : p + 1; @@ -2042,8 +2033,7 @@ bool mg_log_prefix(int level, const char *file, int line, const char *fname) { level, p, line, fname); if (n > sizeof(buf) - 2) n = sizeof(buf) - 2; while (n < sizeof(buf)) buf[n++] = ' '; - buf[sizeof(buf) - 1] = '\0'; - s_fn(buf, n - 1, s_fn_param); + logs(buf, n - 1); return true; } else { return false; @@ -2057,16 +2047,11 @@ void mg_log(const char *fmt, ...) { va_start(ap, fmt); len = mg_vasprintf(&buf, sizeof(mem), fmt, ap); va_end(ap); - s_fn(buf, len, s_fn_param); - s_fn("\n", 1, s_fn_param); + logs(buf, len); + logc((unsigned char) '\n'); if (buf != mem) free(buf); } -void mg_log_set_callback(void (*fn)(const void *, size_t, void *), void *fnd) { - s_fn = fn; - s_fn_param = fnd; -} - static unsigned char nibble(unsigned c) { return (unsigned char) (c < 10 ? c + '0' : c + 'W'); } diff --git a/mongoose.h b/mongoose.h index 68a7a466..dbb992e5 100644 --- a/mongoose.h +++ b/mongoose.h @@ -338,8 +338,6 @@ struct timeval { #include -#define MG_IO_SIZE 512 -#define MG_SOCK_LISTEN_BACKLOG_SIZE 1 #define MG_ENABLE_CUSTOM_MILLIS 1 typedef int socklen_t; #define closesocket(x) closesocket(x) @@ -564,6 +562,7 @@ typedef int socklen_t; #include #include +#define MG_PUTCHAR(x) printk("%c", x) #define strerror(x) zsock_gai_strerror(x) #define FD_CLOEXEC 0 #define F_SETFD 0 @@ -674,6 +673,10 @@ int sscanf(const char *, const char *, ...); #endif #endif +#ifndef MG_PUTCHAR +#define MG_PUTCHAR(x) putchar(x) +#endif + @@ -726,7 +729,6 @@ enum { MG_LL_NONE, MG_LL_ERROR, MG_LL_INFO, MG_LL_DEBUG, MG_LL_VERBOSE }; void mg_log(const char *fmt, ...) PRINTF_LIKE(1, 2); 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_callback(void (*fn)(const void *, size_t, void *), void *param); void mg_hexdump(const void *buf, size_t len); #define MG_LOG(level, args) \ diff --git a/src/arch_rtx.h b/src/arch_rtx.h index 6c53f529..8b7dfbc5 100644 --- a/src/arch_rtx.h +++ b/src/arch_rtx.h @@ -15,8 +15,6 @@ #include -#define MG_IO_SIZE 512 -#define MG_SOCK_LISTEN_BACKLOG_SIZE 1 #define MG_ENABLE_CUSTOM_MILLIS 1 typedef int socklen_t; #define closesocket(x) closesocket(x) diff --git a/src/arch_zephyr.h b/src/arch_zephyr.h index 87633882..4315f4b2 100644 --- a/src/arch_zephyr.h +++ b/src/arch_zephyr.h @@ -17,6 +17,7 @@ #include #include +#define MG_PUTCHAR(x) printk("%c", x) #define strerror(x) zsock_gai_strerror(x) #define FD_CLOEXEC 0 #define F_SETFD 0 diff --git a/src/config.h b/src/config.h index ca66c2b8..0d68c468 100644 --- a/src/config.h +++ b/src/config.h @@ -98,3 +98,7 @@ #define MG_ENABLE_FILE 0 #endif #endif + +#ifndef MG_PUTCHAR +#define MG_PUTCHAR(x) putchar(x) +#endif diff --git a/src/log.c b/src/log.c index c9db9ac9..5a5f9e12 100644 --- a/src/log.c +++ b/src/log.c @@ -1,24 +1,17 @@ #include "log.h" #include "util.h" -static void logc(int c) { - putchar(c); +static const char *s_spec = "2"; + +static void logc(unsigned char c) { + MG_PUTCHAR(c); } static void logs(const char *buf, size_t len) { size_t i; - for (i = 0; i < len; i++) logc(buf[i]); + for (i = 0; i < len; i++) logc(((unsigned char *) buf)[i]); } -static void mg_log_stdout(const void *buf, size_t len, void *userdata) { - (void) userdata, (void) buf, (void) len; - logs((const char *) buf, len); -} - -static const char *s_spec = "2"; -static void (*s_fn)(const void *, size_t, void *) = mg_log_stdout; -static void *s_fn_param = NULL; - void mg_log_set(const char *spec) { MG_DEBUG(("Setting log level to %s", spec)); s_spec = spec; @@ -30,8 +23,6 @@ bool mg_log_prefix(int level, const char *file, int line, const char *fname) { struct mg_str k, v, s = mg_str(s_spec); const char *p = strrchr(file, '/'); - if (s_fn == NULL) return false; - if (p == NULL) p = strrchr(file, '\\'); p = p == NULL ? file : p + 1; @@ -46,8 +37,7 @@ bool mg_log_prefix(int level, const char *file, int line, const char *fname) { level, p, line, fname); if (n > sizeof(buf) - 2) n = sizeof(buf) - 2; while (n < sizeof(buf)) buf[n++] = ' '; - buf[sizeof(buf) - 1] = '\0'; - s_fn(buf, n - 1, s_fn_param); + logs(buf, n - 1); return true; } else { return false; @@ -61,16 +51,11 @@ void mg_log(const char *fmt, ...) { va_start(ap, fmt); len = mg_vasprintf(&buf, sizeof(mem), fmt, ap); va_end(ap); - s_fn(buf, len, s_fn_param); - s_fn("\n", 1, s_fn_param); + logs(buf, len); + logc((unsigned char) '\n'); if (buf != mem) free(buf); } -void mg_log_set_callback(void (*fn)(const void *, size_t, void *), void *fnd) { - s_fn = fn; - s_fn_param = fnd; -} - static unsigned char nibble(unsigned c) { return (unsigned char) (c < 10 ? c + '0' : c + 'W'); } diff --git a/src/log.h b/src/log.h index 0b02ca9a..f4d663fb 100644 --- a/src/log.h +++ b/src/log.h @@ -7,7 +7,6 @@ enum { MG_LL_NONE, MG_LL_ERROR, MG_LL_INFO, MG_LL_DEBUG, MG_LL_VERBOSE }; void mg_log(const char *fmt, ...) PRINTF_LIKE(1, 2); 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_callback(void (*fn)(const void *, size_t, void *), void *param); void mg_hexdump(const void *buf, size_t len); #define MG_LOG(level, args) \