From 7b343bf7fe9eb58200860a187327b959931b2828 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 11 Feb 2012 14:20:40 +0000 Subject: [PATCH] Add logic to re-direct debug output to a sysloggin device git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4381 42af7a65-404d-4744-a932-0658087f49c3 --- ChangeLog | 3 + Documentation/NuttxPortingGuide.html | 43 +++++++ arch/arm/src/common/up_initialize.c | 8 ++ arch/arm/src/common/up_internal.h | 14 +-- arch/avr/src/at32uc3/at32uc3_config.h | 6 + arch/avr/src/at90usb/at90usb_config.h | 6 + arch/avr/src/atmega/atmega_config.h | 6 + arch/avr/src/common/up_initialize.c | 14 +++ arch/avr/src/common/up_internal.h | 8 -- arch/hc/src/common/up_initialize.c | 7 ++ arch/hc/src/common/up_internal.h | 12 +- arch/mips/src/common/up_initialize.c | 8 ++ arch/mips/src/common/up_internal.h | 14 +-- arch/sh/src/common/up_initialize.c | 7 ++ arch/sh/src/common/up_internal.h | 14 +-- arch/sim/src/up_initialize.c | 15 ++- arch/x86/src/common/up_initialize.c | 8 ++ arch/x86/src/common/up_internal.h | 14 +-- arch/z16/src/common/up_initialize.c | 8 ++ arch/z16/src/common/up_internal.h | 14 +-- configs/README.txt | 26 +++++ drivers/ramlog.c | 159 ++++++++++++++++++++------ include/nuttx/lib.h | 10 +- include/nuttx/ramlog.h | 59 ++++++++-- include/nuttx/streams.h | 23 +++- lib/stdio/Make.defs | 4 + lib/stdio/lib_lowprintf.c | 16 +-- lib/stdio/lib_rawprintf.c | 17 ++- lib/stdio/lib_syslogstream.c | 103 +++++++++++++++++ tools/mkconfig.c | 12 +- 30 files changed, 541 insertions(+), 117 deletions(-) create mode 100644 lib/stdio/lib_syslogstream.c diff --git a/ChangeLog b/ChangeLog index 1de3de5f0f..77d4c443da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2453,4 +2453,7 @@ * drivers/ramlog.c: Add a character driver that can substitute for /dev/console and or be used for logging debug output when there is no serial port available (such as when a telnet console is used). + * lib/stdio/lib_syslogstream: Add a stream object that will be used to + re-direct all debug output to the RAM log if CONFIG_SYSLOG and + CONFIG_RAMLOG_SYSLOG are defined. diff --git a/Documentation/NuttxPortingGuide.html b/Documentation/NuttxPortingGuide.html index 244f07538e..4b562057de 100644 --- a/Documentation/NuttxPortingGuide.html +++ b/Documentation/NuttxPortingGuide.html @@ -3941,6 +3941,49 @@ build +

+ System Logging: +

+ +

Kernel build options:

diff --git a/arch/arm/src/common/up_initialize.c b/arch/arm/src/common/up_initialize.c index 9d7ba0f193..130f0721ff 100644 --- a/arch/arm/src/common/up_initialize.c +++ b/arch/arm/src/common/up_initialize.c @@ -43,6 +43,8 @@ #include #include +#include + #include #include "up_arch.h" @@ -159,6 +161,12 @@ void up_initialize(void) ramlog_consoleinit(); #endif + /* Initialize the system logging device */ + +#ifdef CONFIG_RAMLOG_SYSLOG + ramlog_sysloginit(); +#endif + /* Initialize the netwok */ up_netinitialize(); diff --git a/arch/arm/src/common/up_internal.h b/arch/arm/src/common/up_internal.h index 6f98ac04d3..8ade816c74 100644 --- a/arch/arm/src/common/up_internal.h +++ b/arch/arm/src/common/up_internal.h @@ -81,6 +81,12 @@ # endif #endig +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /* Check if an interrupt stack size is configured */ #ifndef CONFIG_ARCH_INTERRUPTSTACK @@ -297,14 +303,6 @@ extern void lowconsole_init(void); # define lowconsole_init() #endif -/* Defined in drivers/ramlog.c */ - -#ifdef CONFIG_RAMLOG_CONSOLE -extern void ramlog_consoleinit(void); -#else -# define ramlog_consoleinit() -#endif - /* DMA **********************************************************************/ #ifdef CONFIG_ARCH_DMA diff --git a/arch/avr/src/at32uc3/at32uc3_config.h b/arch/avr/src/at32uc3/at32uc3_config.h index 72536083a7..b425205316 100644 --- a/arch/avr/src/at32uc3/at32uc3_config.h +++ b/arch/avr/src/at32uc3/at32uc3_config.h @@ -168,6 +168,12 @@ # endif #endig +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /* If GPIO IRQ support is defined, then a set of GPIOs must all be included */ #if CONFIG_AVR32_GPIOIRQSETA == 0 && CONFIG_AVR32_GPIOIRQSETB == 0 diff --git a/arch/avr/src/at90usb/at90usb_config.h b/arch/avr/src/at90usb/at90usb_config.h index e6d2fb89ed..41ad9957c3 100644 --- a/arch/avr/src/at90usb/at90usb_config.h +++ b/arch/avr/src/at90usb/at90usb_config.h @@ -87,6 +87,12 @@ # endif #endig +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /************************************************************************************ * Public Types ************************************************************************************/ diff --git a/arch/avr/src/atmega/atmega_config.h b/arch/avr/src/atmega/atmega_config.h index e0a7968cc5..a168a2e475 100644 --- a/arch/avr/src/atmega/atmega_config.h +++ b/arch/avr/src/atmega/atmega_config.h @@ -92,6 +92,12 @@ # endif #endig +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /************************************************************************************ * Public Types ************************************************************************************/ diff --git a/arch/avr/src/common/up_initialize.c b/arch/avr/src/common/up_initialize.c index eed1b41359..3bcbc0dc42 100644 --- a/arch/avr/src/common/up_initialize.c +++ b/arch/avr/src/common/up_initialize.c @@ -43,6 +43,8 @@ #include #include +#include + #include #include "up_arch.h" @@ -76,6 +78,12 @@ # endif #endig +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /**************************************************************************** * Private Types ****************************************************************************/ @@ -183,6 +191,12 @@ void up_initialize(void) ramlog_consoleinit(); #endif + /* Initialize the system logging device */ + +#ifdef CONFIG_RAMLOG_SYSLOG + ramlog_sysloginit(); +#endif + /* Initialize the netwok */ up_netinitialize(); diff --git a/arch/avr/src/common/up_internal.h b/arch/avr/src/common/up_internal.h index bc62be720d..e65b5627d9 100644 --- a/arch/avr/src/common/up_internal.h +++ b/arch/avr/src/common/up_internal.h @@ -172,14 +172,6 @@ extern void lowconsole_init(void); # define lowconsole_init() #endif -/* Defined in drivers/ramlog.c */ - -#ifdef CONFIG_RAMLOG_CONSOLE -extern void ramlog_consoleinit(void); -#else -# define ramlog_consoleinit() -#endif - /* Defined in chip/xxx_timerisr.c */ extern void up_timerinit(void); diff --git a/arch/hc/src/common/up_initialize.c b/arch/hc/src/common/up_initialize.c index a1973e94aa..5481ed7bc7 100755 --- a/arch/hc/src/common/up_initialize.c +++ b/arch/hc/src/common/up_initialize.c @@ -43,6 +43,7 @@ #include #include +#include #include "up_arch.h" #include "up_internal.h" @@ -158,6 +159,12 @@ void up_initialize(void) ramlog_consoleinit(); #endif + /* Initialize the system logging device */ + +#ifdef CONFIG_RAMLOG_SYSLOG + ramlog_sysloginit(); +#endif + /* Initialize the netwok */ up_netinitialize(); diff --git a/arch/hc/src/common/up_internal.h b/arch/hc/src/common/up_internal.h index 22677dd0f7..93572e4049 100755 --- a/arch/hc/src/common/up_internal.h +++ b/arch/hc/src/common/up_internal.h @@ -81,6 +81,12 @@ # endif #endig +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /* Check if an interrupt stack size is configured */ #ifndef CONFIG_ARCH_INTERRUPTSTACK @@ -178,12 +184,6 @@ extern void lowconsole_init(void); # define lowconsole_init() #endif -#ifdef CONFIG_RAMLOG_CONSOLE -extern void ramlog_consoleinit(void); -#else -# define ramlog_consoleinit() -#endif - extern void up_lowputc(char ch); extern void up_puts(const char *str); extern void up_lowputs(const char *str); diff --git a/arch/mips/src/common/up_initialize.c b/arch/mips/src/common/up_initialize.c index 0a061990b3..11bd979f0e 100644 --- a/arch/mips/src/common/up_initialize.c +++ b/arch/mips/src/common/up_initialize.c @@ -43,6 +43,8 @@ #include #include +#include + #include #include "up_arch.h" @@ -159,6 +161,12 @@ void up_initialize(void) ramlog_consoleinit(); #endif + /* Initialize the system logging device */ + +#ifdef CONFIG_RAMLOG_SYSLOG + ramlog_sysloginit(); +#endif + /* Initialize the netwok */ up_netinitialize(); diff --git a/arch/mips/src/common/up_internal.h b/arch/mips/src/common/up_internal.h index 25b5174a5b..d43580c657 100755 --- a/arch/mips/src/common/up_internal.h +++ b/arch/mips/src/common/up_internal.h @@ -81,6 +81,12 @@ # endif #endig +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /* Check if an interrupt stack size is configured */ #ifndef CONFIG_ARCH_INTERRUPTSTACK @@ -188,14 +194,6 @@ extern void lowconsole_init(void); # define lowconsole_init() #endif -/* Defined in drivers/ramlog.c */ - -#ifdef CONFIG_RAMLOG_CONSOLE -extern void ramlog_consoleinit(void); -#else -# define ramlog_consoleinit() -#endif - /* Debug */ #ifdef CONFIG_ARCH_STACKDUMP diff --git a/arch/sh/src/common/up_initialize.c b/arch/sh/src/common/up_initialize.c index b81e3b9723..837b218a04 100644 --- a/arch/sh/src/common/up_initialize.c +++ b/arch/sh/src/common/up_initialize.c @@ -43,6 +43,7 @@ #include #include +#include #include "up_arch.h" #include "up_internal.h" @@ -150,6 +151,12 @@ void up_initialize(void) ramlog_consoleinit(); #endif + /* Initialize the system logging device */ + +#ifdef CONFIG_RAMLOG_SYSLOG + ramlog_sysloginit(); +#endif + /* Initialize the netwok */ up_netinitialize(); diff --git a/arch/sh/src/common/up_internal.h b/arch/sh/src/common/up_internal.h index 1b6e41635b..50aad14787 100644 --- a/arch/sh/src/common/up_internal.h +++ b/arch/sh/src/common/up_internal.h @@ -85,6 +85,12 @@ # endif #endig +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /* Check if an interrupt stack size is configured */ #ifndef CONFIG_ARCH_INTERRUPTSTACK @@ -177,14 +183,6 @@ extern void lowconsole_init(void); # define lowconsole_init() #endif -/* Defined in drivers/ramlog.c */ - -#ifdef CONFIG_RAMLOG_CONSOLE -extern void ramlog_consoleinit(void); -#else -# define ramlog_consoleinit() -#endif - /* Defined in up_watchdog.c */ extern void up_wdtinit(void); diff --git a/arch/sim/src/up_initialize.c b/arch/sim/src/up_initialize.c index 4e07904e4e..a850d7c69f 100644 --- a/arch/sim/src/up_initialize.c +++ b/arch/sim/src/up_initialize.c @@ -1,8 +1,8 @@ /**************************************************************************** * up_initialize.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -43,6 +43,7 @@ #include #include +#include #include "up_internal.h" @@ -50,6 +51,12 @@ * Private Definitions ****************************************************************************/ +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /**************************************************************************** * Private Data ****************************************************************************/ @@ -99,6 +106,10 @@ void up_initialize(void) devzero_register(); /* Standard /dev/zero */ up_devconsole(); /* Our private /dev/console */ +#ifdef CONFIG_RAMLOG_SYSLOG + ramlog_sysloginit(); /* System logging device */ +#endif + #if defined(CONFIG_FS_FAT) && !defined(CONFIG_DISABLE_MOUNTPOINT) up_registerblockdevice(); /* Our FAT ramdisk at /dev/ram0 */ #endif diff --git a/arch/x86/src/common/up_initialize.c b/arch/x86/src/common/up_initialize.c index 92d4d35370..eb0cdccc19 100644 --- a/arch/x86/src/common/up_initialize.c +++ b/arch/x86/src/common/up_initialize.c @@ -43,6 +43,8 @@ #include #include +#include + #include #include "up_arch.h" @@ -159,6 +161,12 @@ void up_initialize(void) ramlog_consoleinit(); #endif + /* Initialize the system logging device */ + +#ifdef CONFIG_RAMLOG_SYSLOG + ramlog_sysloginit(); +#endif + /* Initialize the netwok */ up_netinitialize(); diff --git a/arch/x86/src/common/up_internal.h b/arch/x86/src/common/up_internal.h index f95a9bc746..c840cf90b9 100644 --- a/arch/x86/src/common/up_internal.h +++ b/arch/x86/src/common/up_internal.h @@ -80,6 +80,12 @@ # endif #endig +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /* Check if an interrupt stack size is configured */ #ifndef CONFIG_ARCH_INTERRUPTSTACK @@ -203,14 +209,6 @@ extern void lowconsole_init(void); # define lowconsole_init() #endif -/* Defined in drivers/ramlog.c */ - -#ifdef CONFIG_RAMLOG_CONSOLE -extern void ramlog_consoleinit(void); -#else -# define ramlog_consoleinit() -#endif - /* Defined in up_watchdog.c */ extern void up_wdtinit(void); diff --git a/arch/z16/src/common/up_initialize.c b/arch/z16/src/common/up_initialize.c index ab653f3680..165f0ead36 100644 --- a/arch/z16/src/common/up_initialize.c +++ b/arch/z16/src/common/up_initialize.c @@ -44,6 +44,8 @@ #include #include #include +#include + #include #include "up_internal.h" @@ -174,6 +176,12 @@ void up_initialize(void) ramlog_consoleinit(); #endif + /* Initialize the system logging device */ + +#ifdef CONFIG_RAMLOG_SYSLOG + ramlog_sysloginit(); +#endif + /* Initialize the netwok */ up_netinitialize(); diff --git a/arch/z16/src/common/up_internal.h b/arch/z16/src/common/up_internal.h index e0c8ffc61e..7d0aaaec85 100644 --- a/arch/z16/src/common/up_internal.h +++ b/arch/z16/src/common/up_internal.h @@ -70,7 +70,13 @@ # define USE_SERIALDRIVER 1 # define USE_EARLYSERIALINIT 1 #endif - + +/* Determine which device to use as the system loggin device */ + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + /* Macros for portability */ #define IN_INTERRUPT (current_regs != NULL) @@ -140,12 +146,6 @@ extern void up_serialinit(void); extern void lowconsole_init(void); #endif -/* Defined in drivers/ramlog.c */ - -#ifdef CONFIG_RAMLOG_CONSOLE -extern void ramlog_consoleinit(void); -#endif - /* Defined in up_timerisr.c */ extern void up_timerinit(void); diff --git a/configs/README.txt b/configs/README.txt index a69c51e7e2..81473b73be 100644 --- a/configs/README.txt +++ b/configs/README.txt @@ -357,6 +357,32 @@ defconfig -- This is a configuration file similar to the Linux CONFIG_SIG_SIGWORK - The signal number that will be used to wake-up the worker thread. Default: 4 + System Logging: + CONFIG_SYSLOG enables general system logging support. + + At present, the only system loggin device is a circular buffer in RAM. + If CONFIG_SYSLOG is selected, then these options are also available. + + CONFIG_RAMLOG - Enables the RAM logging feature + CONFIG_RAMLOG_CONSOLE - Use the RAM logging device as a system console. + If this feature is enabled (along with CONFIG_DEV_CONSOLE), then all + console output will be re-directed to a circular buffer in RAM. This + is useful, for example, if the only console is a Telnet console. Then + in that case, console output from non-Telnet threads will go to the + circular buffer and can be viewed using the NSH 'dmesg' command. + CONFIG_RAMLOG_SYSLOG - Use the RAM logging device for the syslogging + interface. If this feature is enabled (along with CONFIG_SYSLOG), + then all debug output (only) will be re-directed to the circular + buffer in RAM. This RAM log can be view from NSH using the 'dmesg' + command. + CONFIG_RAMLOG_NPOLLWAITERS - The number of threads than can be waiting + for this driver on poll(). Default: 4 + + If CONFIG_RAMLOG_CONSOLE or CONFIG_RAMLOG_SYSLOG is selected, then the + following may also be provided: + + CONFIG_RAMLOG_CONSOLE_BUFSIZE - Size of the console RAM log. Default: 1024 + Kernel build options: CONFIG_NUTTX_KERNEL - Builds NuttX as a separately compiled kernel. CONFIG_SYS_RESERVED - Reserved system call values for use diff --git a/drivers/ramlog.c b/drivers/ramlog.c index 741979e7fa..0245e6051c 100644 --- a/drivers/ramlog.c +++ b/drivers/ramlog.c @@ -93,6 +93,15 @@ struct ramlog_dev_s /**************************************************************************** * Private Function Prototypes ****************************************************************************/ +/* Helper functions */ + +#ifndef CONFIG_DISABLE_POLL +static void ramlog_pollnotify(FAR struct ramlog_dev_s *priv, + pollevent_t eventset); +#endif +static ssize_t ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch); + +/* Character driver methods */ static ssize_t ramlog_read(FAR struct file *, FAR char *, size_t); static ssize_t ramlog_write(FAR struct file *, FAR const char *, size_t); @@ -118,10 +127,13 @@ static const struct file_operations g_ramlogfops = #endif }; -/* This is the pre-allocated buffer used for the console RAM log */ +/* This is the pre-allocated buffer used for the console RAM log and/or + * for the syslogging function. + */ -#ifdef CONFIG_RAMLOG_CONSOLE -static char g_consoleramlog[CONFIG_RAMLOG_CONSOLE_BUFSIZE]; +#if defined(CONFIG_RAMLOG_CONSOLE) || defined(CONFIG_RAMLOG_SYSLOG) +static struct ramlog_dev_s g_sysdev; +static char g_sysbuffer[CONFIG_RAMLOG_CONSOLE_BUFSIZE]; #endif /**************************************************************************** @@ -133,7 +145,8 @@ static char g_consoleramlog[CONFIG_RAMLOG_CONSOLE_BUFSIZE]; ****************************************************************************/ #ifndef CONFIG_DISABLE_POLL -static void ramlog_pollnotify(FAR struct ramlog_dev_s *priv, pollevent_t eventset) +static void ramlog_pollnotify(FAR struct ramlog_dev_s *priv, + pollevent_t eventset) { FAR struct pollfd *fds; irqstate_t flags; @@ -160,6 +173,46 @@ static void ramlog_pollnotify(FAR struct ramlog_dev_s *priv, pollevent_t eventse # define ramlog_pollnotify(priv,event) #endif +/**************************************************************************** + * Name: ramlog_addchar + ****************************************************************************/ + +static int ramlog_addchar(FAR struct ramlog_dev_s *priv, char ch) +{ + irqstate_t flags; + int nexthead; + + /* Disable interrupts (in case we are NOT called from interrupt handler) */ + + flags = irqsave(); + + /* Calculate the write index AFTER the next byte is written */ + + nexthead = priv->rl_head + 1; + if (nexthead >= priv->rl_bufsize) + { + nexthead = 0; + } + + /* Would the next write overflow the circular buffer? */ + + if (nexthead == priv->rl_tail) + { + /* Yes... then break out of the loop to return an indication that + * nothing was saved in the buffer. + */ + + return -EBUSY; + } + + /* No... copy the byte and re-enable interrupts */ + + priv->rl_buffer[priv->rl_head] = ch; + priv->rl_head = nexthead; + irqrestore(flags); + return OK; +} + /**************************************************************************** * Name: ramlog_read ****************************************************************************/ @@ -310,10 +363,12 @@ static ssize_t ramlog_read(FAR struct file *filep, FAR char *buffer, size_t len) /* Notify all poll/select waiters that they can write to the FIFO */ errout_without_sem: +#ifndef CONFIG_DISABLE_POLL if (nread > 0) { ramlog_pollnotify(priv, POLLOUT); } +#endif return nread; } @@ -327,7 +382,6 @@ static ssize_t ramlog_write(FAR struct file *filep, FAR const char *buffer, size struct ramlog_dev_s *priv; irqstate_t flags; ssize_t nwritten; - int nexthead; int i; /* Some sanity checking */ @@ -346,34 +400,16 @@ static ssize_t ramlog_write(FAR struct file *filep, FAR const char *buffer, size for (nwritten = 0; nwritten < len; nwritten++) { - /* Disable interrupts (in case we are NOT called from interrupt handler) */ - - flags = irqsave(); - - /* Calculate the write index AFTER the next byte is written */ - - nexthead = priv->rl_head + 1; - if (nexthead >= priv->rl_bufsize) + int ret = ramlog_addchar(priv, buffer[nwritten]); + if (ret < 0) { - nexthead = 0; - } - - /* Would the next write overflow the circular buffer? */ - - if (nexthead == priv->rl_tail) - { - /* Yes... then break out of the loop to return the number of bytes - * written. The data to be written is dropped on the floor. + /* The buffer is full and nothing was saved. Break out of the + * loop to return the number of bytes written up to this point. + * The data to be written is dropped on the floor. */ - return nwritten; + break; } - - /* No... copy the byte and re-enable interrupts */ - - priv->rl_buffer[priv->rl_head] = buffer[nwritten]; - priv->rl_head = nexthead; - irqrestore(flags); } /* Was anything written? */ @@ -393,6 +429,7 @@ static ssize_t ramlog_write(FAR struct file *filep, FAR const char *buffer, size /* Notify all poll/select waiters that they can write to the FIFO */ ramlog_pollnotify(priv, POLLIN); + irqrestore(flags); } /* Return the number of bytes written */ @@ -526,6 +563,7 @@ errout: * ****************************************************************************/ +#if !defined(CONFIG_RAMLOG_CONSOLE) && !defined(CONFIG_RAMLOG_SYSLOG) int ramlog_register(FAR const char *devpath, FAR char *buffer, size_t buflen) { FAR struct ramlog_dev_s *priv; @@ -558,6 +596,7 @@ int ramlog_register(FAR const char *devpath, FAR char *buffer, size_t buflen) return ret; } +#endif /**************************************************************************** * Name: ramlog_consoleinit @@ -571,10 +610,57 @@ int ramlog_register(FAR const char *devpath, FAR char *buffer, size_t buflen) #ifdef CONFIG_RAMLOG_CONSOLE int ramlog_consoleinit(void) { - /* Register a RAM log as the console device */ + FAR struct ramlog_dev_s *priv = &g_sysdev; + int ret; - return ramlog_register("/dev/console", g_consoleramlog, - CONFIG_RAMLOG_CONSOLE_BUFSIZE); + /* Initialize the RAM loggin device structure */ + + sem_init(&priv->rl_exclsem, 0, 1); + sem_init(&priv->rl_waitsem, 0, 0); + priv->rl_bufsize = g_sysbuffer; + priv->rl_buffer = CONFIG_RAMLOG_CONSOLE_BUFSIZE; + + /* Register the console character driver */ + + ret = register_driver("/dev/console", &g_ramlogfops, 0666, priv); + + /* Register the syslog character driver */ + +#ifdef CONFIG_RAMLOG_SYSLOG + if (ret >= 0) + { + ret = register_driver("/dev/syslog", &g_ramlogfops, 0666, priv); + } +#endif + return ret; +} +#endif + +/**************************************************************************** + * Name: ramlog_sysloginit + * + * Description: + * Create the RAM logging device and register it at the specified path. + * Mostly likely this path will be /dev/syslog + * + * If CONFIG_RAMLOG_CONSOLE is also defined, then this functionality is + * performed when ramlog_consoleinit() is called. + * + ****************************************************************************/ + +#if !defined(CONFIG_RAMLOG_CONSOLE) && defined(CONFIG_RAMLOG_SYSLOG) +int ramlog_sysloginit(void) +{ + FAR struct ramlog_dev_s *priv = &g_sysdev; + + /* Initialize the RAM loggin device structure */ + + sem_init(&priv->rl_exclsem, 0, 1); + sem_init(&priv->rl_waitsem, 0, 0); + priv->rl_bufsize = g_sysbuffer; + priv->rl_buffer = CONFIG_RAMLOG_CONSOLE_BUFSIZE; + + return register_driver("/dev/syslog", &g_ramlogfops, 0666, priv); } #endif @@ -591,8 +677,13 @@ int ramlog_consoleinit(void) * ****************************************************************************/ -#ifdef CONFIG_RAMLOG_SYSLOG -# warning "Missing logic" +#if defined(CONFIG_RAMLOG_CONSOLE) || defined(CONFIG_RAMLOG_SYSLOG) +int ramlog_putc(int ch) +{ + FAR struct ramlog_dev_s *priv = &g_sysdev; + (void)ramlog_addchar(priv, ch) + return ch; +} #endif #endif /* CONFIG_RAMLOG */ diff --git a/include/nuttx/lib.h b/include/nuttx/lib.h index 3c74257d4e..32910ad211 100644 --- a/include/nuttx/lib.h +++ b/include/nuttx/lib.h @@ -2,8 +2,8 @@ * include/nuttx/lib.h * Non-standard, internal APIs available in lib/. * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2007-2009, 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -34,8 +34,8 @@ * ****************************************************************************/ -#ifndef __LIB_H -#define __LIB_H +#ifndef __INCLUDE_NUTTX_LIB_H +#define __INCLUDE_NUTTX_LIB_H /**************************************************************************** * Included Files @@ -80,4 +80,4 @@ EXTERN void lib_releaselist(FAR struct streamlist *list); #endif #endif /* __ASSEMBLY__ */ -#endif /* __LIB_H */ +#endif /* __INCLUDE_NUTTX_LIB_H */ diff --git a/include/nuttx/ramlog.h b/include/nuttx/ramlog.h index 4f42de9654..d1b9dea977 100644 --- a/include/nuttx/ramlog.h +++ b/include/nuttx/ramlog.h @@ -63,18 +63,33 @@ /* Configuration ************************************************************/ /* CONFIG_RAMLOG - Enables the RAM logging feature * CONFIG_RAMLOG_CONSOLE - Use the RAM logging device as a system console. + * If this feature is enabled (along with CONFIG_DEV_CONSOLE), then all + * console output will be re-directed to a circular buffer in RAM. This + * is useful, for example, if the only console is a Telnet console. Then + * in that case, console output from non-Telnet threads will go to the + * circular buffer and can be viewed using the NSH 'dmesg' command. * CONFIG_RAMLOG_SYSLOG - Use the RAM logging device for the syslogging - * interface. This should have: - * CONFIG_SYSLOG=ramlog + * interface. If this feature is enabled (along with CONFIG_SYSLOG), + * then all debug output (only) will be re-directed to the circular + * buffer in RAM. This RAM log can be view from NSH using the 'dmesg' + * command. * CONFIG_RAMLOG_NPOLLWAITERS - The number of threads than can be waiting * for this driver on poll(). Default: 4 * - * If CONFIG_RAMLOG_CONSOLE is selected, then the following may also be - * provided: + * If CONFIG_RAMLOG_CONSOLE or CONFIG_RAMLOG_SYSLOG is selected, then the + * following may also be provided: * * CONFIG_RAMLOG_CONSOLE_BUFSIZE - Size of the console RAM log. Default: 1024 */ +#ifndef CONFIG_DEV_CONSOLE +# undef CONFIG_RAMLOG_CONSOLE +#endif + +#ifndef CONFIG_SYSLOG +# undef CONFIG_RAMLOG_SYSLOG +#endif + #ifndef CONFIG_RAMLOG_NPOLLWAITERS # define CONFIG_RAMLOG_NPOLLWAITERS 4 #endif @@ -104,19 +119,31 @@ extern "C" { * * Description: * Create the RAM logging device and register it at the specified path. - * Mostly likely this path will be /dev/console + * Mostly likely this path will be /dev/console. + * + * This interface is not normally used but can be made available is + * someone just wants to tinker with the RAM log as a generic character + * device. Normally both CONFIG_RAMLOG_CONSOLE and CONFIG_RAMLOG_SYSLOG + * would be set (to capture all output in the log) -OR- just + * CONFIG_RAMLOG_SYSLOG would be set to capture debug output only + * in the log. * ****************************************************************************/ +#if !defined(CONFIG_RAMLOG_CONSOLE) && !defined(CONFIG_RAMLOG_SYSLOG) EXTERN int ramlog_register(FAR const char *devpath, FAR char *buffer, size_t buflen); +#endif /**************************************************************************** * Name: ramlog_consoleinit * * Description: * Create the RAM logging device and register it at the specified path. - * Mostly likely this path will be /dev/console + * Mostly likely this path will be /dev/console. + * + * If CONFIG_RAMLOG_SYSLOG is also defined, then the same RAM logging + * device is also registered at /dev/syslog * ****************************************************************************/ @@ -124,6 +151,22 @@ EXTERN int ramlog_register(FAR const char *devpath, FAR char *buffer, EXTERN int ramlog_consoleinit(void) #endif +/**************************************************************************** + * Name: ramlog_sysloginit + * + * Description: + * Create the RAM logging device and register it at the specified path. + * Mostly likely this path will be /dev/syslog + * + * If CONFIG_RAMLOG_CONSOLE is also defined, then this functionality is + * performed when ramlog_consoleinit() is called. + * + ****************************************************************************/ + +#if !defined(CONFIG_RAMLOG_CONSOLE) && defined(CONFIG_RAMLOG_SYSLOG) +EXTERN int ramlog_sysloginit(void) +#endif + /**************************************************************************** * Name: ramlog * @@ -137,8 +180,8 @@ EXTERN int ramlog_consoleinit(void) * ****************************************************************************/ -#ifdef CONFIG_RAMLOG_SYSLOG -# warning "Missing logic" +#if defined(CONFIG_RAMLOG_CONSOLE) || defined(CONFIG_RAMLOG_SYSLOG) +EXTERN int ramlog_putc(int ch); #endif #undef EXTERN diff --git a/include/nuttx/streams.h b/include/nuttx/streams.h index e8b6e2b2df..8317eacbd2 100644 --- a/include/nuttx/streams.h +++ b/include/nuttx/streams.h @@ -1,8 +1,8 @@ /**************************************************************************** * include/nuttx/streams.h * - * Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2009, 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -268,6 +268,25 @@ EXTERN void lib_zeroinstream(FAR struct lib_instream_s *zeroinstream); EXTERN void lib_nullinstream(FAR struct lib_instream_s *nullinstream); EXTERN void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream); +/**************************************************************************** + * Name: lib_sylogstream + * + * Description: + * Initializes a stream for use with the configured syslog interface. + * + * Input parameters: + * lowoutstream - User allocated, uninitialized instance of struct + * lib_lowoutstream_s to be initialized. + * + * Returned Value: + * None (User allocated instance initialized). + * + ****************************************************************************/ + +#ifdef CONFIG_SYSLOG +EXTERN void lib_sylogstream(FAR struct lib_outstream_s *stream); +#endif + #undef EXTERN #if defined(__cplusplus) } diff --git a/lib/stdio/Make.defs b/lib/stdio/Make.defs index 7bffb23403..690c83641c 100644 --- a/lib/stdio/Make.defs +++ b/lib/stdio/Make.defs @@ -52,6 +52,10 @@ STDIO_SRCS += lib_fopen.c lib_fclose.c lib_fread.c lib_libfread.c lib_fseek.c \ endif endif +ifdef ($(CONFIG_SYSLOG) +STDIO_SRCS += lib_syslogstream.c +endif + ifeq ($(CONFIG_LIBC_FLOATINGPOINT),y) STDIO_SRCS += lib_dtoa.c endif diff --git a/lib/stdio/lib_lowprintf.c b/lib/stdio/lib_lowprintf.c index 576402d61b..0314a27f00 100644 --- a/lib/stdio/lib_lowprintf.c +++ b/lib/stdio/lib_lowprintf.c @@ -1,8 +1,8 @@ /**************************************************************************** * lib/stdio/lib_lowprintf.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -82,17 +82,19 @@ * Name: lib_lowvprintf ****************************************************************************/ -#ifdef CONFIG_ARCH_LOWPUTC +#if defined(CONFIG_ARCH_LOWPUTC) || defined(CONFIG_SYSLOG) int lib_lowvprintf(const char *fmt, va_list ap) { struct lib_outstream_s stream; - /* Wrap the stdout in a stream object and let lib_vsprintf - * do the work. - */ + /* Wrap the stdout in a stream object and let lib_vsprintf do the work. */ +#if defined(CONFIG_RAMLOG_CONSOLE) || defined(CONFIG_RAMLOG_SYSLOG) + lib_syslogstream((FAR struct lib_outstream_s *)&stream); +#else lib_lowoutstream((FAR struct lib_outstream_s *)&stream); +#endif return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, ap); } @@ -111,4 +113,4 @@ int lib_lowprintf(const char *fmt, ...) return ret; } -#endif /* CONFIG_ARCH_LOWPUTC */ +#endif /* CONFIG_ARCH_LOWPUTC || CONFIG_SYSLOG*/ diff --git a/lib/stdio/lib_rawprintf.c b/lib/stdio/lib_rawprintf.c index 915fd833bf..d738b212a3 100644 --- a/lib/stdio/lib_rawprintf.c +++ b/lib/stdio/lib_rawprintf.c @@ -1,8 +1,8 @@ /**************************************************************************** * lib/stdio/lib_rawprintf.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -83,7 +83,18 @@ int lib_rawvprintf(const char *fmt, va_list ap) { -#if CONFIG_NFILE_DESCRIPTORS > 0 +#if defined(CONFIG_SYSLOG) + + struct lib_outstream_s stream; + + /* Wrap the low-level output in a stream object and let lib_vsprintf + * do the work. + */ + + lib_syslogstream((FAR struct lib_outstream_s *)&stream); + return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, ap); + +#elif CONFIG_NFILE_DESCRIPTORS > 0 struct lib_rawoutstream_s rawoutstream; diff --git a/lib/stdio/lib_syslogstream.c b/lib/stdio/lib_syslogstream.c new file mode 100644 index 0000000000..0b5a951448 --- /dev/null +++ b/lib/stdio/lib_syslogstream.c @@ -0,0 +1,103 @@ +/**************************************************************************** + * lib/stdio/lib_syslogstream.c + * + * Copyright (C) 2023 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include + +#include "lib_internal.h" + +#ifdef CONFIG_SYSLOG + +/**************************************************************************** + * Pre-processor definition + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: syslogstream_putc + ****************************************************************************/ + +static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch) +{ + /* At present, the RAM log is the only supported logging device */ + +#ifdef CONFIG_RAMLOG_SYSLOG + (void)ramlog_putc(ch); + this->nput++; +#endif +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lib_sylogstream + * + * Description: + * Initializes a stream for use with the coinfigured syslog interface. + * + * Input parameters: + * lowoutstream - User allocated, uninitialized instance of struct + * lib_lowoutstream_s to be initialized. + * + * Returned Value: + * None (User allocated instance initialized). + * + ****************************************************************************/ + +void lib_sylogstream(FAR struct lib_outstream_s *stream) +{ + stream->put = syslogstream_putc; +#ifdef CONFIG_STDIO_LINEBUFFER + stream->flush = lib_noflush; +#endif + stream->nput = 0; +} + +#endif /* CONFIG_SYSLOG */ + + diff --git a/tools/mkconfig.c b/tools/mkconfig.c index 22594c0ae8..7e3162b8ff 100644 --- a/tools/mkconfig.c +++ b/tools/mkconfig.c @@ -1,8 +1,8 @@ /**************************************************************************** * tools/mkconfig.c * - * Copyright (C) 2007-2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2007-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -144,7 +144,13 @@ int main(int argc, char **argv, char **envp) printf("# if CONFIG_NFILE_STREAMS > 0 && CONFIG_NFILE_STREAMS < 3\n"); printf("# undef CONFIG_NFILE_STREAMS\n"); printf("# define CONFIG_NFILE_STREAMS 3\n"); - printf("# endif\n"); + printf("# endif\n\n"); + printf("/* If no console is selected, then disable all console devices */\n\n"); + printf("#else\n"); + printf("# undef CONFIG_DEV_LOWCONSOLE\n"); + printf("# undef CONFIG_RAMLOG_CONSOLE\n"); + printf("# undef CONFIG_CDCACM_CONSOLE\n"); + printf("# undef CONFIG_PL2303_CONSOLE\n"); printf("#endif\n\n"); printf("/* If priority inheritance is disabled, then do not allocate any\n"); printf(" * associated resources.\n");