From 97e93eec653e38a04e94e5191bdbf5ea48edde96 Mon Sep 17 00:00:00 2001 From: Daniele Basile Date: Mon, 16 Jan 2012 18:29:27 +0100 Subject: [PATCH] Implement syslog. --- bertos/cfg/cfg_syslog.h | 29 ++++++++---- bertos/cfg/log.h | 31 ++++++++++--- bertos/net/syslog.c | 97 ++++++++++++++++++++++++++++++++++++++--- bertos/net/syslog.h | 49 ++++++++++++++++++++- 4 files changed, 184 insertions(+), 22 deletions(-) diff --git a/bertos/cfg/cfg_syslog.h b/bertos/cfg/cfg_syslog.h index 922a5f7b..69bc990c 100644 --- a/bertos/cfg/cfg_syslog.h +++ b/bertos/cfg/cfg_syslog.h @@ -39,19 +39,32 @@ #define CFG_SYSLOG_H /** - * Module logging level. + * Enable the net logging. * - * $WIZ$ type = "enum" - * $WIZ$ value_list = "log_level" + * $WIZ$ type = "bool" */ -#define SYSLOG_LOG_LEVEL LOG_LVL_WARN +#define CONFIG_SYSLOG_NET 1 /** - * Module logging format. + * Enable the serial logging. * - * $WIZ$ type = "enum" - * $WIZ$ value_list = "log_format" + * $WIZ$ type = "bool" */ -#define SYSLOG_LOG_FORMAT LOG_FMT_VERBOSE +#define CONFIG_SYSLOG_SERIAL 1 + +/** + * Destination port of log messages + * + * $WIZ$ type = "int" + */ +#define CONFIG_SYSLOG_PORT 514 + +/** + * Max log message length. + * + * $WIZ$ type = "int" + */ +#define CONFIG_SYSLOG_BUFSIZE 256 + #endif /* CFG_SYSLOG_H */ diff --git a/bertos/cfg/log.h b/bertos/cfg/log.h index 1e4fc150..086d4615 100644 --- a/bertos/cfg/log.h +++ b/bertos/cfg/log.h @@ -145,12 +145,33 @@ #define LOG_FMT_TERSE 0 /** \} */ -#if LOG_FORMAT == LOG_FMT_VERBOSE - #define LOG_PRINT(str_level, str,...) kprintf("%s():%d:%s: " str, __func__, __LINE__, str_level, ## __VA_ARGS__) -#elif LOG_FORMAT == LOG_FMT_TERSE - #define LOG_PRINT(str_level, str,...) kprintf("%s: " str, str_level, ## __VA_ARGS__) +#include "cfg/cfg_syslog.h" + +/* For backward compatibility */ +#ifndef CONFIG_SYSLOG_NET + #define CONFIG_SYSLOG_NET 0 +#endif + +#if CONFIG_SYSLOG_NET + #include + + #if LOG_FORMAT == LOG_FMT_VERBOSE + #define LOG_PRINT(str_level, str,...) syslog_printf("<182>%d-%s():%d:%s: " str, syslog_count(), __func__, __LINE__, str_level, ## __VA_ARGS__) + #elif LOG_FORMAT == LOG_FMT_TERSE + #define LOG_PRINT(str_level, str,...) syslog_printf("<182>%d-%s: " str, syslog_count(), str_level, ## __VA_ARGS__) + #else + #error No LOG_FORMAT defined + #endif + #else - #error No LOG_FORMAT defined + #if LOG_FORMAT == LOG_FMT_VERBOSE + #define LOG_PRINT(str_level, str,...) kprintf("%s():%d:%s: " str, __func__, __LINE__, str_level, ## __VA_ARGS__) + #elif LOG_FORMAT == LOG_FMT_TERSE + #define LOG_PRINT(str_level, str,...) kprintf("%s: " str, str_level, ## __VA_ARGS__) + #else + #error No LOG_FORMAT defined + #endif + #endif #if LOG_LEVEL >= LOG_LVL_ERR diff --git a/bertos/net/syslog.c b/bertos/net/syslog.c index 2974c47d..a8524bd6 100644 --- a/bertos/net/syslog.c +++ b/bertos/net/syslog.c @@ -40,16 +40,99 @@ #include "syslog.h" #include "cfg/cfg_syslog.h" -#define LOG_LEVEL SYSLOG_LOG_LEVEL -#define LOG_FORMAT SYSLOG_LOG_FORMAT -#include + +#include // host_to_net16 + +#include +#include +#include +#include + +#include +#include + +static char syslog_message[CONFIG_SYSLOG_BUFSIZE]; +static SysLog *local_syslog_ctx; /** - * Init - * + * Return the number of log message has been sent. */ -int syslog_init(void) +uint32_t syslog_count(void) { - return 0; + return local_syslog_ctx->syslog_cnt; } +/** + * Get the current syslog server address, in lwip ip_address format. + */ +struct ip_addr syslog_ip(void) +{ + return local_syslog_ctx->server_addr; +} + +/** + * Change the current syslog server ip address. + * \param lwip ip_address (you could use the macro IP4_ADDR() to get it form ip address) + */ +void syslog_setIp(struct ip_addr addr) +{ + local_syslog_ctx->server_addr = addr; +} + + +/** + * Print the log message on upd socket and serial if you configure the + * macro CONFIG_SYSLOG_SERIAL in cfg_syslog.h, + */ +int syslog_printf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + int len = vsnprintf(syslog_message, sizeof(syslog_message), fmt, ap); + va_end(ap); + syslog_message[sizeof(syslog_message) - 1] = 0; + + #if CONFIG_SYSLOG_SERIAL + kputs(syslog_message); + #endif + + if (local_syslog_ctx == NULL) + { + kputs("SysLog not init\n"); + return -1; + } + + local_syslog_ctx->syslog_server = netconn_new(NETCONN_UDP); + if (local_syslog_ctx->syslog_server == NULL) + { + kputs("Unable to alloc UDP connetions\n"); + return -1; + } + + netbuf_ref(local_syslog_ctx->send_buf, syslog_message, len); + if (netconn_sendto(local_syslog_ctx->syslog_server, local_syslog_ctx->send_buf, + &(local_syslog_ctx->server_addr), CONFIG_SYSLOG_PORT) != ERR_OK) + { + kputs("Unable to send log!\n"); + } + + local_syslog_ctx->syslog_cnt++; + netconn_delete(local_syslog_ctx->syslog_server); + + return len; +} + +/** + * Init the syslog message. + * + * \param syslog context + * \param lwip ip_address (you could use the macro IP4_ADDR() to get it form ip address) + */ +void syslog_init(SysLog *syslog_ctx, struct ip_addr addr) +{ + memset(syslog_ctx, 0, sizeof(syslog_ctx)); + syslog_ctx->server_addr = addr; + syslog_ctx->send_buf = netbuf_new(); + + local_syslog_ctx = syslog_ctx; +} diff --git a/bertos/net/syslog.h b/bertos/net/syslog.h index 92c9f23e..52e9b1e0 100644 --- a/bertos/net/syslog.h +++ b/bertos/net/syslog.h @@ -30,10 +30,36 @@ * * --> * - * \brief SYSLOG + * \brief SYSLOG log all debug message in the BeRTOS code, with the + * respective log level, to one syslog server on the udp protocol. * * The usage pattern is as follows: * \code + * // Init the network, es using dhcp: + * + * // Initialize TCP/IP stack + * tcpip_init(NULL, NULL); + * + * // Bring up the network interface + * netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input); + * netif_set_default(&netif); + * netif_set_up(&netif); + * + * dhcp_start(&netif); + * while (!netif.ip_addr.addr) + * timer_delay(DHCP_FINE_TIMER_MSECS); + * + * // lwip address struct + * struct ip_addr server_addr; + * // convert address to ip_address + * IP4_ADDR(&server_addr, 192, 168, 0, 2); + * + * // init the syslog module + * syslog_init(&syslog, server_addr); + * + * // now all LOG_*(message) are logged on + * // syslog server. + * // see the cfg_syslog.h for all settings. * \endcode * * @@ -47,6 +73,25 @@ #ifndef NET_SYSLOG_H #define NET_SYSLOG_H -int syslog_init(void); +#include +#include + +typedef struct SysLog +{ + struct netconn *syslog_server; + struct netbuf *send_buf; + struct ip_addr server_addr; + + uint32_t syslog_cnt; +} SysLog; + + +uint32_t syslog_count(void); +struct ip_addr syslog_ip(void); +void syslog_setIp(struct ip_addr addr); + +int syslog_printf(const char *fmt, ...); + +void syslog_init(SysLog *syslog_ctx, struct ip_addr addr); #endif /* NET_SYSLOG_H */ -- 2.25.1