Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/fmt: use fflush(); stdio_write() instead of fwrite() #19250

Merged
merged 2 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions sys/fmt/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "kernel_defines.h"
#include "fmt.h"
#include "stdio_base.h"

static const char _hex_chars[16] = "0123456789ABCDEF";

Expand Down Expand Up @@ -506,25 +507,24 @@ uint32_t scn_u32_hex(const char *str, size_t n)
return res;
}

/* native gets special treatment as native's stdio code is ... special.
* And when not building for RIOT, there's no `stdio_write()`.
* In those cases, just defer to `printf()`.
*/
#if IS_USED(MODULE_STDIO_NATIVE) || !defined(RIOT_VERSION)
Comment on lines +510 to +514
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain to me why this is needed? I fail to understand the reason on my own. E.g. if I remove the guarded code, the unit test still pass for native. What am I misunderstanding?

void print(const char *s, size_t n)
{
if (IS_USED(MODULE_STDIO_NATIVE)) {
/* native gets special treatment as native's stdio code is ...
* special */
printf("%.*s", (int)n, s);
return;
}
printf("%.*s", (int)n, s);
}
#else
void print(const char *s, size_t n)
{
/* flush the libc's output buffer so output is not intermingled. */
fflush(stdout);

while (n > 0) {
ssize_t written;
written = fwrite(s, 1, n, stdout);
if (written < 0) {
break;
}
n -= written;
s += written;
}
stdio_write(s, n);
}
#endif

void print_u32_dec(uint32_t val)
{
Expand Down
6 changes: 5 additions & 1 deletion sys/test_utils/print_stack_usage/print_stack_usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
#include <stdio.h>
#endif

#if MODULE_FMT
# define MIN_SIZE (THREAD_STACKSIZE_TINY)
#else
# define MIN_SIZE (THREAD_STACKSIZE_TINY + THREAD_EXTRA_STACKSIZE_PRINTF)
#endif

void print_stack_usage_metric(const char *name, void *stack, unsigned max_size)
{
Expand All @@ -37,7 +41,7 @@ void print_stack_usage_metric(const char *name, void *stack, unsigned max_size)
#if MODULE_FMT
print_str("{ \"threads\": [{ \"name\": \"");
print_str(name);
print_str(", \"stack_size\": ");
print_str("\", \"stack_size\": ");
print_u32_dec(max_size);
print_str(", \"stack_used\": ");
print_u32_dec(max_size - free);
Expand Down