#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 */
#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 <net/syslog.h>
+
+ #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
#include "syslog.h"
#include "cfg/cfg_syslog.h"
-#define LOG_LEVEL SYSLOG_LOG_LEVEL
-#define LOG_FORMAT SYSLOG_LOG_FORMAT
-#include <cfg/log.h>
+
+#include <cpu/byteorder.h> // host_to_net16
+
+#include <lwip/ip_addr.h>
+#include <lwip/netif.h>
+#include <lwip/netbuf.h>
+#include <lwip/tcpip.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+
+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;
+}
*
* -->
*
- * \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
*
*
#ifndef NET_SYSLOG_H
#define NET_SYSLOG_H
-int syslog_init(void);
+#include <lwip/netif.h>
+#include <lwip/ip_addr.h>
+
+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 */