Implement syslog.
[bertos.git] / bertos / net / syslog.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2012 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief SYSLOG implementation
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  *
37  * notest:all
38  */
39
40 #include "syslog.h"
41
42 #include "cfg/cfg_syslog.h"
43
44 #include <cpu/byteorder.h> // host_to_net16
45
46 #include <lwip/ip_addr.h>
47 #include <lwip/netif.h>
48 #include <lwip/netbuf.h>
49 #include <lwip/tcpip.h>
50
51 #include <stdarg.h>
52 #include <stdio.h>
53
54 static char syslog_message[CONFIG_SYSLOG_BUFSIZE];
55 static SysLog *local_syslog_ctx;
56
57 /**
58  * Return the number of log message has been sent.
59  */
60 uint32_t syslog_count(void)
61 {
62         return local_syslog_ctx->syslog_cnt;
63 }
64
65 /**
66  * Get the current syslog server address, in lwip ip_address format.
67  */
68 struct ip_addr syslog_ip(void)
69 {
70         return local_syslog_ctx->server_addr;
71 }
72
73 /**
74  * Change the current syslog server ip address.
75  * \param lwip ip_address (you could use the macro IP4_ADDR() to get it form ip address)
76  */
77 void syslog_setIp(struct ip_addr addr)
78 {
79         local_syslog_ctx->server_addr = addr;
80 }
81
82
83 /**
84  * Print the log message on upd socket and serial if you configure the
85  * macro CONFIG_SYSLOG_SERIAL in cfg_syslog.h,
86  */
87 int syslog_printf(const char *fmt, ...)
88 {
89         va_list ap;
90         va_start(ap, fmt);
91         int len = vsnprintf(syslog_message, sizeof(syslog_message), fmt, ap);
92         va_end(ap);
93         syslog_message[sizeof(syslog_message) - 1] = 0;
94
95         #if CONFIG_SYSLOG_SERIAL
96                 kputs(syslog_message);
97         #endif
98
99         if (local_syslog_ctx == NULL)
100         {
101                 kputs("SysLog not init\n");
102                 return -1;
103         }
104
105         local_syslog_ctx->syslog_server = netconn_new(NETCONN_UDP);
106         if (local_syslog_ctx->syslog_server == NULL)
107         {
108                 kputs("Unable to alloc UDP connetions\n");
109                 return -1;
110         }
111
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)
115         {
116                 kputs("Unable to send log!\n");
117         }
118
119         local_syslog_ctx->syslog_cnt++;
120         netconn_delete(local_syslog_ctx->syslog_server);
121
122         return len;
123 }
124
125 /**
126  * Init the syslog message.
127  *
128  * \param syslog context
129  * \param lwip ip_address (you could use the macro IP4_ADDR() to get it form ip address)
130  */
131 void syslog_init(SysLog *syslog_ctx, struct ip_addr addr)
132 {
133         memset(syslog_ctx, 0, sizeof(syslog_ctx));
134         syslog_ctx->server_addr = addr;
135         syslog_ctx->send_buf = netbuf_new();
136
137         local_syslog_ctx = syslog_ctx;
138 }