syslog.c
Go to the documentation of this file.
00001
00040 #include "syslog.h"
00041
00042 #include "cfg/cfg_syslog.h"
00043
00044 #include <cpu/byteorder.h> // host_to_net16
00045
00046 #include <lwip/ip_addr.h>
00047 #include <lwip/netif.h>
00048 #include <lwip/netbuf.h>
00049 #include <lwip/tcpip.h>
00050
00051 #include <stdarg.h>
00052 #include <stdio.h>
00053
00054 static char syslog_message[CONFIG_SYSLOG_BUFSIZE];
00055 static SysLog *local_syslog_ctx;
00056
00060 uint32_t syslog_count(void)
00061 {
00062     return local_syslog_ctx->syslog_cnt;
00063 }
00064
00068 struct ip_addr syslog_ip(void)
00069 {
00070     return local_syslog_ctx->server_addr;
00071 }
00072
00077 void syslog_setIp(struct ip_addr addr)
00078 {
00079     local_syslog_ctx->server_addr = addr;
00080 }
00081
00082
00087 int syslog_printf(const char *fmt, ...)
00088 {
00089     va_list ap;
00090     va_start(ap, fmt);
00091     int len = vsnprintf(syslog_message, sizeof(syslog_message), fmt, ap);
00092     va_end(ap);
00093     syslog_message[sizeof(syslog_message) - 1] = 0;
00094
00095     #if CONFIG_SYSLOG_SERIAL
00096         kputs(syslog_message);
00097     #endif
00098 
00099     if (local_syslog_ctx == NULL)
00100     {
00101         kputs("SysLog not init\n");
00102         return -1;
00103     }
00104
00105     local_syslog_ctx->syslog_server = netconn_new(NETCONN_UDP);
00106     if (local_syslog_ctx->syslog_server == NULL)
00107     {
00108         kputs("Unable to alloc UDP connetions\n");
00109         return -1;
00110     }
00111
00112     netbuf_ref(local_syslog_ctx->send_buf, syslog_message, len);
00113     if (netconn_sendto(local_syslog_ctx->syslog_server, local_syslog_ctx->send_buf,
00114                         &(local_syslog_ctx->server_addr), CONFIG_SYSLOG_PORT) != ERR_OK)
00115     {
00116         kputs("Unable to send log!\n");
00117     }
00118
00119     local_syslog_ctx->syslog_cnt++;
00120     netconn_delete(local_syslog_ctx->syslog_server);
00121
00122     return len;
00123 }
00124
00131 void syslog_init(SysLog *syslog_ctx, struct ip_addr addr)
00132 {
00133     memset(syslog_ctx, 0, sizeof(syslog_ctx));
00134     syslog_ctx->server_addr = addr;
00135     syslog_ctx->send_buf = netbuf_new();
00136
00137     local_syslog_ctx = syslog_ctx;
00138 }