Skip to content

Commit 235af97

Browse files
committed
libc/stdout: add an option to redirect standard output stream to syslog
Redirect standard output stream of printf(), vprintf(), puts(), putchar() to syslog, This option will ensure the printing sequence of syslog/printf to avoid confusion. Noted that if enable this option, the redirection capability of standard input(<) and output(>) will not be work as expect in (nsh)nuttx shell. Signed-off-by: chao an <[email protected]>
1 parent 358261a commit 235af97

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

drivers/syslog/Kconfig

+20
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,25 @@ config SYSLOG_REGISTER
407407
---help---
408408
This option will support register the syslog channel dynamically.
409409

410+
config SYSLOG_STDOUT
411+
bool "Redirect standard output stream of printf() and vprintf() to syslog"
412+
default n
413+
---help---
414+
Redirect standard output stream of printf(), vprintf(), puts(), putchar() to syslog,
415+
This option will ensure the printing sequence of syslog/printf to avoid
416+
confusion. Noted that if enable this option, the redirection
417+
capability of standard input(<) and output(>) will not be work as expect in
418+
(nsh)nuttx shell.
419+
420+
if SYSLOG_STDOUT
421+
422+
config SYSLOG_STDOUT_PREFIX
423+
bool "Redirect standard output stream with syslog prefix"
424+
default n
425+
---help---
426+
Redirect standard output stream with syslog prefix.
427+
428+
endif # SYSLOG_STDOUT
429+
410430
endif # SYSLOG
411431
endmenu # System logging

libs/libc/stdio/lib_printf.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
****************************************************************************/
2626

2727
#include <stdio.h>
28+
#include <syslog.h>
29+
30+
#include <nuttx/streams.h>
31+
#include <nuttx/syslog/syslog.h>
2832

2933
/****************************************************************************
3034
* Public Functions
@@ -40,7 +44,25 @@ int printf(FAR const IPTR char *fmt, ...)
4044
int ret;
4145

4246
va_start(ap, fmt);
43-
#ifdef CONFIG_FILE_STREAM
47+
48+
#ifdef CONFIG_SYSLOG_STDOUT_PREFIX
49+
ret = nx_vsyslog(LOG_NOTICE, fmt, &ap);
50+
#elif defined(CONFIG_SYSLOG_STDOUT)
51+
struct lib_syslograwstream_s stream;
52+
53+
/* Wrap the low-level output in a stream object and let lib_vsprintf
54+
* do the work.
55+
*/
56+
57+
lib_syslograwstream_open(&stream);
58+
59+
ret = lib_vsprintf_internal(&stream.common, fmt, ap);
60+
61+
/* Flush and destroy the syslog stream buffer */
62+
63+
lib_syslograwstream_close(&stream);
64+
65+
#elif defined(CONFIG_FILE_STREAM)
4466
ret = vfprintf(stdout, fmt, ap);
4567
#else
4668
ret = vdprintf(STDOUT_FILENO, fmt, ap);

libs/libc/stdio/lib_putchar.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333

3434
int putchar(int c)
3535
{
36-
#ifdef CONFIG_FILE_STREAM
36+
#ifdef CONFIG_SYSLOG_STDOUT
37+
return printf("%c", c);
38+
#elif defined(CONFIG_FILE_STREAM)
3739
return fputc(c, stdout);
3840
#else
3941
unsigned char tmp = c;
@@ -43,7 +45,9 @@ int putchar(int c)
4345

4446
int putchar_unlocked(int c)
4547
{
46-
#ifdef CONFIG_FILE_STREAM
48+
#ifdef CONFIG_SYSLOG_STDOUT
49+
return printf("%c", c);
50+
#elif defined(CONFIG_FILE_STREAM)
4751
return fputc_unlocked(c, stdout);
4852
#else
4953
unsigned char tmp = c;

libs/libc/stdio/lib_puts.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444

4545
int puts(FAR const IPTR char *s)
4646
{
47-
#ifdef CONFIG_FILE_STREAM
47+
#ifdef CONFIG_SYSLOG_STDOUT
48+
return printf("%s\n", s);
49+
#elif defined(CONFIG_FILE_STREAM)
4850
FILE *stream = stdout;
4951
int nwritten;
5052
int nput = EOF;

0 commit comments

Comments
 (0)