4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2012 Develer S.r.l. (http://www.develer.com/)
33 * \brief SYSLOG implementation
35 * \author Daniele Basile <asterix@develer.com>
42 #include "cfg/cfg_syslog.h"
44 #include <cpu/byteorder.h> // host_to_net16
46 #include <lwip/ip_addr.h>
47 #include <lwip/netif.h>
48 #include <lwip/netbuf.h>
49 #include <lwip/tcpip.h>
54 static char syslog_message[CONFIG_SYSLOG_BUFSIZE];
55 static SysLog *local_syslog_ctx;
58 * Return the number of log message has been sent.
60 uint32_t syslog_count(void)
62 return local_syslog_ctx->syslog_cnt;
66 * Get the current syslog server address, in lwip ip_address format.
68 struct ip_addr syslog_ip(void)
70 return local_syslog_ctx->server_addr;
74 * Change the current syslog server ip address.
75 * \param addr lwip ip_address (you could use the macro IP4_ADDR() to get it form ip address)
77 void syslog_setIp(struct ip_addr addr)
79 local_syslog_ctx->server_addr = addr;
84 * Print the log message on upd socket and serial if you configure the
85 * macro CONFIG_SYSLOG_SERIAL in cfg_syslog.h,
87 int syslog_printf(const char *fmt, ...)
91 int len = vsnprintf(syslog_message, sizeof(syslog_message), fmt, ap);
93 syslog_message[sizeof(syslog_message) - 1] = 0;
95 #if CONFIG_SYSLOG_SERIAL
96 kputs(syslog_message);
99 if (local_syslog_ctx == NULL)
101 kputs("SysLog not init\n");
105 local_syslog_ctx->syslog_server = netconn_new(NETCONN_UDP);
106 if (local_syslog_ctx->syslog_server == NULL)
108 kputs("Unable to alloc UDP connetions\n");
112 netbuf_ref(local_syslog_ctx->send_buf, syslog_message, len);
113 if (netconn_sendto(local_syslog_ctx->syslog_server, local_syslog_ctx->send_buf,
114 &(local_syslog_ctx->server_addr), CONFIG_SYSLOG_PORT) != ERR_OK)
116 kputs("Unable to send log!\n");
119 local_syslog_ctx->syslog_cnt++;
120 netconn_delete(local_syslog_ctx->syslog_server);
126 * Init the syslog message.
128 * \param syslog_ctx syslog context
129 * \param addr lwip ip_address (you could use the macro IP4_ADDR() to get it form ip address)
131 void syslog_init(SysLog *syslog_ctx, struct ip_addr addr)
133 memset(syslog_ctx, 0, sizeof(syslog_ctx));
134 syslog_ctx->server_addr = addr;
135 syslog_ctx->send_buf = netbuf_new();
137 local_syslog_ctx = syslog_ctx;