Permalink
Browse files

enabled syslog warning in some functions

  • Loading branch information...
1 parent f5a5599 commit d65c4dd4435426e4bbdce76045e2cb2d6002135d Hirochika Asai committed Nov 17, 2011
Showing with 257 additions and 4 deletions.
  1. +1 −1 Makefile.am
  2. +185 −0 error.c
  3. +61 −0 error.h
  4. +6 −0 main.c
  5. +4 −3 net.c
View
2 Makefile.am
@@ -8,7 +8,7 @@
bin_PROGRAMS = vxlan
vxlan_SOURCES = main.c fdb.c hash.c iftap.c net.c common.h fdb.h hash.h \
- iftap.h net.h
+ iftap.h net.h error.c error.h
vxlan_LDFLAGS = -lpthread
View
185 error.c
@@ -0,0 +1,185 @@
+/*_
+ * Copyright 2009 Scyphus Solutions Co.,Ltd. All rights reserved.
+ *
+ * Authors:
+ * Hirochika Asai <asai@scyphus.co.jp>
+ */
+
+/* $Id: error.c,v 3fd7b0a2108d 2011/02/11 09:36:32 Hirochika $ */
+
+#include "error.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+#include <syslog.h>
+
+int error_syslog = 0;
+
+static void
+error_printf(int, int, const char *, va_list);
+
+/* Print error message. */
+void
+error_msg(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(0, LOG_INFO, fmt, ap);
+ va_end(ap);
+}
+
+/* Print system error message. */
+void
+error_sys_msg(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(1, LOG_INFO, fmt, ap);
+ va_end(ap);
+}
+
+/* Print notice message. */
+void
+error_notice(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(0, LOG_NOTICE, fmt, ap);
+ va_end(ap);
+}
+
+/* Print system warning message. */
+void
+error_sys_notice(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(1, LOG_NOTICE, fmt, ap);
+ va_end(ap);
+}
+
+/* Print warning message. */
+void
+error_warn(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(0, LOG_WARNING, fmt, ap);
+ va_end(ap);
+}
+
+/* Print system warning message. */
+void
+error_sys_warn(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(1, LOG_WARNING, fmt, ap);
+ va_end(ap);
+}
+
+/* Print error message and quit the program. */
+void
+error_quit(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(0, LOG_ERR, fmt, ap);
+ va_end(ap);
+ exit(EXIT_FAILURE);
+}
+
+/* Print system error message and quit the program. */
+void
+error_sys(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(1, LOG_ERR, fmt, ap);
+ va_end(ap);
+ exit(EXIT_FAILURE);
+}
+
+/* Print error message and quit the program with a status. */
+void
+error_exit(int status, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(0, LOG_ERR, fmt, ap);
+ va_end(ap);
+ exit(status);
+}
+
+/* Print error message and abort. */
+void
+error_dump(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ error_printf(1, LOG_ERR, fmt, ap);
+ va_end(ap);
+ abort(); /* dump core and terminate */
+ exit(EXIT_FAILURE); /* shouldn't get here */
+}
+
+/**
+ * error_printf
+ */
+static void
+error_printf(int errnoflag, int level, const char *fmt, va_list ap)
+{
+ int errno_save, n;
+ char buf[MAXLINE];
+
+ errno_save = errno;
+ vsnprintf(buf, sizeof(buf), fmt, ap);
+
+ n = strlen(buf);
+ if ( errnoflag ) {
+ snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
+ }
+ strcat(buf, "\n");
+
+ if ( error_syslog ) {
+ /* output to syslog */
+ syslog(level, buf);
+ } else {
+ fflush(stdout); /* in case stdout and stderr are the same */
+ fputs(buf, stderr);
+ fflush(stderr);
+ }
+}
+
+void
+error_enable_syslog(void)
+{
+ error_syslog = 1;
+}
+
+void
+error_disable_syslog(void)
+{
+ error_syslog = 0;
+}
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
View
61 error.h
@@ -0,0 +1,61 @@
+/*_
+ * Copyright 2009 Scyphus Solutions Co.,Ltd. All rights reserved.
+ *
+ * Authors:
+ * Hirochika Asai <asai@scyphus.co.jp>
+ */
+
+/* $Id: error.h,v f045f3d6e083 2011/02/12 10:50:28 Hirochika $ */
+
+#ifndef _ERROR_H
+#define _ERROR_H
+
+#ifndef MAXLINE
+#define MAXLINE 256
+#endif
+
+#include <stdio.h>
+#include <stdarg.h>
+
+extern int error_syslog;
+
+static const struct {
+ const char *nomem; /* Memory error */
+ const char *server; /* Server error */
+} errstr = {
+ .nomem = "No enough memory.",
+ .server = "Server error: %s"
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+ void error_msg(const char *, ...);
+ void error_sys_msg(const char *, ...);
+ void error_notice(const char *, ...);
+ void error_sys_notice(const char *, ...);
+ void error_warn(const char *, ...);
+ void error_sys_warn(const char *, ...);
+ void error_quit(const char *, ...);
+ void error_sys(const char *, ...);
+ void error_exit(int, const char *, ...);
+ void error_dump(const char *fmt, ...);
+
+ void error_enable_syslog(void);
+ void error_disable_syslog(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ERROR_H */
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
View
6 main.c
@@ -14,12 +14,14 @@
#include "net.h"
#include "fdb.h"
#include "iftap.h"
+#include "error.h"
struct vxlan vxlan;
unsigned short uport = 0;
unsigned short mport = 0;
+
void process_vxlan (void);
void debug_print_vhdr (struct vxlan_hdr * vhdr);
@@ -126,6 +128,10 @@ main (int argc, char * argv[])
err (EXIT_FAILURE, "Invalid subinterface number %u", subn);
}
+ /* Enable syslog */
+ error_enable_syslog();
+ /*error_warn("test %d", 12345);*/
+
(void)snprintf(tunifname, IFNAMSIZ, "%s%d", VXLAN_TUNNAME, subn);
uport = VXLAN_PORT_BASE + subn;
mport = VXLAN_MCAST_PORT_BASE + subn;
View
7 net.c
@@ -1,6 +1,7 @@
#include "net.h"
#include "common.h"
#include "fdb.h"
+#include "error.h"
#include <err.h>
#include <stdlib.h>
@@ -124,7 +125,7 @@ void
send_etherflame_from_vxlan_to_local (struct ether_header * ether, int len)
{
if (write (vxlan.tap_sock, ether, len) < 0) {
- warn ("write etherflame to local network failed");
+ error_warn("Write etherflame to local network failed");
}
return;
@@ -155,7 +156,7 @@ send_etherflame_from_local_to_vxlan (struct ether_header * ether, int len)
mhdr.msg_controllen = 0;
if (sendmsg (vxlan.mst_send_sock, &mhdr, 0) < 0)
- warn ("sendmsg to multicast failed");
+ error_warn("sendmsg to multicast failed");
} else {
mhdr.msg_name = &entry->vtep_addr;
@@ -165,7 +166,7 @@ send_etherflame_from_local_to_vxlan (struct ether_header * ether, int len)
mhdr.msg_controllen = 0;
if (sendmsg (vxlan.udp_sock, &mhdr, 0) < 0)
- warn ("sendmsg to unicast failed");
+ error_warn("sendmsg to unicast failed");
}

0 comments on commit d65c4dd

Please sign in to comment.