From 70a30a79d71e35cb1b50abf65857cb50f48a6ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oto=20Pet=C5=99=C3=ADk?= Date: Fri, 16 Dec 2016 00:53:23 +0100 Subject: [PATCH] Cleanup stdout write redirection. - comments, naming, missing include --- core/include/stdout_redirect.h | 21 +++++++++++++++++++++ core/newlib_syscalls.c | 10 +++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/core/include/stdout_redirect.h b/core/include/stdout_redirect.h index 2179b5a..8cc06ed 100644 --- a/core/include/stdout_redirect.h +++ b/core/include/stdout_redirect.h @@ -7,6 +7,8 @@ #ifndef _STDOUT_REDIRECT_H_ #define _STDOUT_REDIRECT_H_ +#include + #ifdef __cplusplus extern "C" { @@ -14,7 +16,26 @@ extern "C" typedef long _WriteFunction(struct _reent *r, int fd, const char *ptr, int len ); +/** Set implementation of write syscall for stdout. + * + * Use this function to redirect stdout for further processing (save to file, send over network). + * It may be good idea to save result of `get_write_stdout()` beforehand and call + * it at the end of the supplied function. + * + * NOTE: use NULL to reset to default implementation. + * + * @param[in] f New code to handle stdout output + * + */ void set_write_stdout(_WriteFunction *f); + +/** Get current implementation of write syscall for stdout. + * + * Save returned value before calling `set_write_stdout`, it allows for chaining + * multiple independent handlers. + * + * @returns current stdout handler + */ _WriteFunction *get_write_stdout(); #ifdef __cplusplus diff --git a/core/newlib_syscalls.c b/core/newlib_syscalls.c index 45e6991..146a010 100644 --- a/core/newlib_syscalls.c +++ b/core/newlib_syscalls.c @@ -55,20 +55,20 @@ __attribute__((weak)) long _write_stdout_r(struct _reent *r, int fd, const char return len; } -_WriteFunction *_current_stdout_write_r = &_write_stdout_r; +static _WriteFunction *current_stdout_write_r = &_write_stdout_r; void set_write_stdout(_WriteFunction *f) { if (f != NULL) { - _current_stdout_write_r = f; + current_stdout_write_r = f; } else { - _current_stdout_write_r = &_write_stdout_r; + current_stdout_write_r = &_write_stdout_r; } } _WriteFunction *get_write_stdout() { - return _current_stdout_write_r; + return current_stdout_write_r; } /* default implementation, replace in a filesystem */ @@ -83,7 +83,7 @@ __attribute__((weak)) long _write_r(struct _reent *r, int fd, const char *ptr, i if(fd != r->_stdout->_file) { return _write_filesystem_r(r, fd, ptr, len); } - return _current_stdout_write_r(r, fd, ptr, len); + return current_stdout_write_r(r, fd, ptr, len); } /* syscall implementation for stdio read from UART */