[PATCH 041/114] Add support for syslog for Phoenix.

Jakub Sejdak jakub.sejdak@phoesys.com
Mon Apr 11 10:24:00 GMT 2016


From: Kuba Sejdak <jakub.sejdak@phoesys.com>

---
 newlib/libc/sys/phoenix/syslog.c | 100 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)
 create mode 100644 newlib/libc/sys/phoenix/syslog.c

diff --git a/newlib/libc/sys/phoenix/syslog.c b/newlib/libc/sys/phoenix/syslog.c
new file mode 100644
index 0000000..5f34cbf
--- /dev/null
+++ b/newlib/libc/sys/phoenix/syslog.c
@@ -0,0 +1,100 @@
+/* Copyright (C) 2012-2016 Phoenix Systems (http://www.phoesys.com/).
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <netinet/in.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <syslog.h>
+#include <unistd.h>
+
+#define BUFFER_SIZE		1024
+#define LOG_MAX_SIZE	(3 * 512)
+
+struct {
+	int socket;
+	struct sockaddr_in addr;
+	const char *ident;
+	int option;
+	int facility;
+	int tried;
+} client = {
+	.ident = NULL,
+	.socket = -1,
+	.option = 0,
+	.facility = 0,
+};
+
+void openlog(const char *ident, int option, int facility)
+{
+	if (client.socket != -1)
+		return;
+
+	client.ident = ident;
+	client.option = option;
+	client.facility = facility;
+
+	if (client.ident == NULL)
+		client.ident = "app";
+
+	if ((client.socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
+		printf("Socket failed: %s\n", strerror(errno));
+		return;
+	}
+
+	memset(&(client.addr), 0, sizeof(client.addr));
+	client.addr.sin_family = AF_INET;
+	client.addr.sin_addr.s_addr = inet_addr("127.0.0.1");
+	client.addr.sin_port = htons(31000);
+}
+
+void syslog(int priority, const char *format, ...)
+{
+	char buffer[BUFFER_SIZE];
+	char msg[LOG_MAX_SIZE];
+	memset(buffer, 0, sizeof(buffer));
+	memset(msg, 0, sizeof(msg));
+	
+	if (LOG_FAC(priority) == 0)
+		priority |= client.facility;
+	
+	va_list args;
+	va_start(args, format);
+	vsnprintf(buffer, sizeof(buffer), format, args);
+	va_end(args);
+	
+	snprintf(msg, sizeof(msg), "<%d> %s[%d]: %s", priority, client.ident, getpid(), buffer);
+
+	if (sendto(client.socket, msg, strlen(msg), 0, (struct sockaddr *) &(client.addr), sizeof(client.addr)) == -1) {
+		printf("Send failed: %s\n", strerror(errno));
+		return;
+	}
+}
+
+void closelog()
+{
+	if (client.socket == -1)
+		return;
+
+	close(client.socket);
+	client.ident = NULL;
+	client.socket = -1;
+}
-- 
2.5.0



More information about the Newlib mailing list