logging: Switch to using libosmocore logging for all the code
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 7c06e27..590f0ec 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -4,4 +4,4 @@
 
 AM_CFLAGS = -O2 -fno-builtin -Wall -DSBINDIR='"$(sbindir)"' -ggdb
 
-libmisc_a_SOURCES = getopt1.c getopt.c ippool.c lookup.c syserr.c tun.c
+libmisc_a_SOURCES = getopt1.c getopt.c ippool.c lookup.c tun.c debug.c
diff --git a/lib/debug.c b/lib/debug.c
new file mode 100644
index 0000000..b3850f9
--- /dev/null
+++ b/lib/debug.c
@@ -0,0 +1,34 @@
+/*
+ * (C) 2014 by Holger Hans Peter Freyther
+ */
+#include "syserr.h"
+
+#include <osmocom/core/utils.h>
+
+static const struct log_info_cat default_categories[] = {
+	[DIP] = {
+		.name = "DIP",
+		.description = "IP Pool and other groups",
+		.enabled = 1, .loglevel = LOGL_NOTICE,
+	},
+	[DTUN] = {
+		.name = "DTUN",
+		.description = "Tunnel interface",
+		.enabled = 1, .loglevel = LOGL_NOTICE,
+	},
+	[DGGSN] = {
+		.name = "DGGSN",
+		.description = "GGSN",
+		.enabled = 1, .loglevel = LOGL_NOTICE,
+	},
+	[DSGSN] = {
+		.name = "DSGSN",
+		.description = "SGSN Emulator",
+		.enabled = 1, .loglevel = LOGL_NOTICE,
+	},
+};
+
+const struct log_info log_info = {
+	.cat = default_categories,
+	.num_cat = ARRAY_SIZE(default_categories),
+};
diff --git a/lib/ippool.c b/lib/ippool.c
index 7ece1e8..1f79a77 100644
--- a/lib/ippool.c
+++ b/lib/ippool.c
@@ -13,7 +13,6 @@
 #include <netinet/in.h>		/* in_addr */
 #include <stdlib.h>		/* calloc */
 #include <stdio.h>		/* sscanf */
-#include <syslog.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
@@ -76,7 +75,7 @@
 	}
 
 	if (p != member) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+		SYS_ERR(DIP, LOGL_ERROR, 0,
 			"ippool_hashdel: Tried to delete member not in hash table");
 		return -1;
 	}
@@ -124,31 +123,31 @@
 		break;
 	case 5:
 		if (m1 > 32) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Invalid mask");
+			SYS_ERR(DIP, LOGL_ERROR, 0, "Invalid mask");
 			return -1;	/* Invalid mask */
 		}
 		mask->s_addr = htonl(0xffffffff << (32 - m1));
 		break;
 	case 8:
 		if (m1 >= 256 || m2 >= 256 || m3 >= 256 || m4 >= 256) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Invalid mask");
+			SYS_ERR(DIP, LOGL_ERROR, 0, "Invalid mask");
 			return -1;	/* Wrong mask format */
 		}
 		m = m1 * 0x1000000 + m2 * 0x10000 + m3 * 0x100 + m4;
 		for (masklog = 0; ((1 << masklog) < ((~m) + 1)); masklog++) ;
 		if (((~m) + 1) != (1 << masklog)) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Invalid mask");
+			SYS_ERR(DIP, LOGL_ERROR, 0, "Invalid mask");
 			return -1;	/* Wrong mask format (not all ones followed by all zeros) */
 		}
 		mask->s_addr = htonl(m);
 		break;
 	default:
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Invalid mask");
+		SYS_ERR(DIP, LOGL_ERROR, 0, "Invalid mask");
 		return -1;	/* Invalid mask */
 	}
 
 	if (a1 >= 256 || a2 >= 256 || a3 >= 256 || a4 >= 256) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+		SYS_ERR(DIP, LOGL_ERROR, 0,
 			"Wrong IP address format");
 		return -1;
 	} else
@@ -179,7 +178,7 @@
 		dynsize = 0;
 	} else {
 		if (ippool_aton(&addr, &mask, dyn, 0)) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+			SYS_ERR(DIP, LOGL_ERROR, 0,
 				"Failed to parse dynamic pool");
 			return -1;
 		}
@@ -205,7 +204,7 @@
 		statmask.s_addr = 0;
 	} else {
 		if (ippool_aton(&stataddr, &statmask, stat, 0)) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+			SYS_ERR(DIP, LOGL_ERROR, 0,
 				"Failed to parse static range");
 			return -1;
 		}
@@ -219,7 +218,7 @@
 	listsize = dynsize + statsize;	/* Allocate space for static IP addresses */
 
 	if (!(*this = calloc(sizeof(struct ippool_t), 1))) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+		SYS_ERR(DIP, LOGL_ERROR, 0,
 			"Failed to allocate memory for ippool");
 		return -1;
 	}
@@ -231,7 +230,7 @@
 
 	(*this)->listsize += listsize;
 	if (!((*this)->member = calloc(sizeof(struct ippoolm_t), listsize))) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+		SYS_ERR(DIP, LOGL_ERROR, 0,
 			"Failed to allocate memory for members in ippool");
 		return -1;
 	}
@@ -249,7 +248,7 @@
 	if (!
 	    ((*this)->hash =
 	     calloc(sizeof(struct ippoolm_t), (*this)->hashsize))) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+		SYS_ERR(DIP, LOGL_ERROR, 0,
 			"Failed to allocate memory for hash members in ippool");
 		return -1;
 	}
@@ -333,7 +332,7 @@
 	}
 	if (member)
 		*member = NULL;
-	/*sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Address could not be found"); */
+	/*SYS_ERR(DIP, LOGL_ERROR, 0, "Address could not be found"); */
 	return -1;
 }
 
@@ -369,19 +368,19 @@
 	/* First check to see if this type of address is allowed */
 	if ((addr) && (addr->s_addr) && statip) {	/* IP address given */
 		if (!this->allowstat) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+			SYS_ERR(DIP, LOGL_ERROR, 0,
 				"Static IP address not allowed");
 			return -1;
 		}
 		if ((addr->s_addr & this->statmask.s_addr) !=
 		    this->stataddr.s_addr) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+			SYS_ERR(DIP, LOGL_ERROR, 0,
 				"Static out of range");
 			return -1;
 		}
 	} else {
 		if (!this->allowdyn) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+			SYS_ERR(DIP, LOGL_ERROR, 0,
 				"Dynamic IP address not allowed");
 			return -1;
 		}
@@ -407,7 +406,7 @@
 	/* If not found yet and dynamic IP then allocate dynamic IP */
 	if ((!p2) && (!statip)) {
 		if (!this->firstdyn) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+			SYS_ERR(DIP, LOGL_ERROR, 0,
 				"No more IP addresses available");
 			return -1;
 		} else
@@ -416,7 +415,7 @@
 
 	if (p2) {		/* Was allocated from dynamic address pool */
 		if (p2->inuse) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+			SYS_ERR(DIP, LOGL_ERROR, 0,
 				"IP address allready in use");
 			return -1;	/* Allready in use / Should not happen */
 		}
@@ -445,7 +444,7 @@
 
 	if ((addr) && (addr->s_addr) && (statip)) {	/* IP address given */
 		if (!this->firststat) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+			SYS_ERR(DIP, LOGL_ERROR, 0,
 				"No more IP addresses available");
 			return -1;	/* No more available */
 		} else
@@ -471,7 +470,7 @@
 		return 0;	/* Success */
 	}
 
-	sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+	SYS_ERR(DIP, LOGL_ERROR, 0,
 		"Could not allocate IP address");
 	return -1;		/* Should never get here. TODO: Bad code */
 }
@@ -483,13 +482,13 @@
 		(void)ippool_printaddr(this);
 
 	if (!member->inuse) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Address not in use");
+		SYS_ERR(DIP, LOGL_ERROR, 0, "Address not in use");
 		return -1;	/* Not in use: Should not happen */
 	}
 
 	switch (member->inuse) {
 	case 0:		/* Not in use: Should not happen */
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Address not in use");
+		SYS_ERR(DIP, LOGL_ERROR, 0, "Address not in use");
 		return -1;
 	case 1:		/* Allocated from dynamic address space */
 		/* Insert into list of unused */
@@ -526,7 +525,7 @@
 			(void)ippool_printaddr(this);
 		return 0;
 	default:		/* Should not happen */
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+		SYS_ERR(DIP, LOGL_ERROR, 0,
 			"Could not free IP address");
 		return -1;
 	}
diff --git a/lib/syserr.c b/lib/syserr.c
deleted file mode 100644
index 73b3f15..0000000
--- a/lib/syserr.c
+++ /dev/null
@@ -1,51 +0,0 @@
-/* 
- * Syslog functions.
- * Copyright (C) 2003, 2004 Mondru AB.
- * 
- * The contents of this file may be used under the terms of the GNU
- * General Public License Version 2, provided that the above copyright
- * notice and this permission notice is included in all copies or
- * substantial portions of the software.
- * 
- */
-
-#include <stdarg.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <stdio.h>
-#include <syslog.h>
-#include <string.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-#include "syserr.h"
-
-static FILE* err_log;
-
-void sys_err_setlogfile(FILE* log)
-{
-	err_log = log;
-}
-
-void sys_err(int pri, char *fn, int ln, int en, char *fmt, ...)
-{
-	va_list args;
-	char buf[SYSERR_MSGSIZE];
-
-	va_start(args, fmt);
-	vsnprintf(buf, SYSERR_MSGSIZE, fmt, args);
-	va_end(args);
-	buf[SYSERR_MSGSIZE - 1] = 0;	/* Make sure it is null terminated */
-	if (en) {
-		if (err_log)
-			fprintf(err_log, "%s: %d: %d (%s) %s\n",
-				fn, ln, en, strerror(en), buf);
-		syslog(pri, "%s: %d: %d (%s) %s", fn, ln, en, strerror(en),
-		       buf);
-	} else {
-		if (err_log)
-			fprintf(err_log, "%s: %d: %s\n", fn, ln, buf);
-		syslog(pri, "%s: %d: %s", fn, ln, buf);
-	}
-}
-
diff --git a/lib/syserr.h b/lib/syserr.h
index 991549c..0c50a5f 100644
--- a/lib/syserr.h
+++ b/lib/syserr.h
@@ -12,10 +12,25 @@
 #ifndef _SYSERR_H
 #define _SYSERR_H
 
-#define SYSERR_MSGSIZE 256
+#include <osmocom/core/logging.h>
 
-void sys_err_setlogfile(FILE*);
+enum {
+	DIP,
+	DTUN,
+	DGGSN,
+	DSGSN,
+};
 
-void sys_err(int pri, char *filename, int en, int line, char *fmt, ...);
+#define SYS_ERR(sub, pri, en, fmt, args...)				\
+	if (en) {							\
+		logp2(sub, pri, __FILE__, __LINE__, 0,			\
+			"errno=%d/%s " fmt "\n", en, strerror(en),	\
+			##args);					\
+	} else {							\
+		logp2(sub, pri, __FILE__, __LINE__, 0,			\
+			fmt "\n", ##args);				\
+	}
+
+extern const struct log_info log_info;
 
 #endif /* !_SYSERR_H */
diff --git a/lib/tun.c b/lib/tun.c
index 43a02ee..94d92ef 100644
--- a/lib/tun.c
+++ b/lib/tun.c
@@ -16,7 +16,6 @@
  *
  */
 
-#include <syslog.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
@@ -94,10 +93,10 @@
 	strncpy(ifr.ifr_name, this->devname, IFNAMSIZ);
 	ifr.ifr_name[IFNAMSIZ - 1] = 0;	/* Make sure to terminate */
 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "socket() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
 	}
 	if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "ioctl() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "ioctl() failed");
 		close(fd);
 		return -1;
 	}
@@ -117,10 +116,10 @@
 	strncpy(ifr.ifr_name, this->devname, IFNAMSIZ);
 	ifr.ifr_name[IFNAMSIZ - 1] = 0;	/* Make sure to terminate */
 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "socket() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
 	}
 	if (ioctl(fd, SIOCSIFFLAGS, &ifr)) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"ioctl(SIOCSIFFLAGS) failed");
 		close(fd);
 		return -1;
@@ -162,7 +161,7 @@
   tun_nlattr(&req.n, sizeof(req), RTA_GATEWAY, gateway, 4);
   
   if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
-    sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+    SYS_ERR(DTUN, LOGL_ERROR, errno,
 	    "socket() failed");
     return -1;
   }
@@ -172,7 +171,7 @@
   local.nl_groups = 0;
   
   if (bind(fd, (struct sockaddr*)&local, sizeof(local)) < 0) {
-    sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+    SYS_ERR(DTUN, LOGL_ERROR, errno,
 	    "bind() failed");
     close(fd);
     return -1;
@@ -180,21 +179,21 @@
 
   addr_len = sizeof(local);
   if (getsockname(fd, (struct sockaddr*)&local, &addr_len) < 0) {
-    sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+    SYS_ERR(DTUN, LOGL_ERROR, errno,
 	    "getsockname() failed");
     close(fd);
     return -1;
   }
 
   if (addr_len != sizeof(local)) {
-    sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+    SYS_ERR(DTUN, LOGL_ERROR, 0,
 	    "Wrong address length %d", addr_len);
     close(fd);
     return -1;
   }
 
   if (local.nl_family != AF_NETLINK) {
-    sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+    SYS_ERR(DTUN, LOGL_ERROR, 0,
 	    "Wrong address family %d", local.nl_family);
     close(fd);
     return -1;
@@ -265,7 +264,7 @@
 	tun_nlattr(&req.n, sizeof(req), IFA_LOCAL, dstaddr, sizeof(dstaddr));
 
 	if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "socket() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
 		return -1;
 	}
 
@@ -274,28 +273,28 @@
 	local.nl_groups = 0;
 
 	if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "bind() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "bind() failed");
 		close(fd);
 		return -1;
 	}
 
 	addr_len = sizeof(local);
 	if (getsockname(fd, (struct sockaddr *)&local, &addr_len) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"getsockname() failed");
 		close(fd);
 		return -1;
 	}
 
 	if (addr_len != sizeof(local)) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+		SYS_ERR(DTUN, LOGL_ERROR, 0,
 			"Wrong address length %d", addr_len);
 		close(fd);
 		return -1;
 	}
 
 	if (local.nl_family != AF_NETLINK) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+		SYS_ERR(DTUN, LOGL_ERROR, 0,
 			"Wrong address family %d", local.nl_family);
 		close(fd);
 		return -1;
@@ -361,12 +360,12 @@
 
 	/* Create a channel to the NET kernel. */
 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "socket() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
 		return -1;
 	}
 
 	if (ioctl(fd, SIOCAIFADDR, (void *)&areq) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"ioctl(SIOCAIFADDR) failed");
 		close(fd);
 		return -1;
@@ -381,7 +380,7 @@
 	if (!this->addrs)	/* Use ioctl for first addr to make ping work */
 		return tun_setaddr(this, addr, dstaddr, netmask);
 
-	sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+	SYS_ERR(DTUN, LOGL_ERROR, errno,
 		"Setting multiple addresses not possible on Solaris");
 	return -1;
 
@@ -417,7 +416,7 @@
 
 	/* Create a channel to the NET kernel. */
 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "socket() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
 		return -1;
 	}
 
@@ -427,10 +426,10 @@
 		       sizeof(*addr));
 		if (ioctl(fd, SIOCSIFADDR, (void *)&ifr) < 0) {
 			if (errno != EEXIST) {
-				sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+				SYS_ERR(DTUN, LOGL_ERROR, errno,
 					"ioctl(SIOCSIFADDR) failed");
 			} else {
-				sys_err(LOG_WARNING, __FILE__, __LINE__, errno,
+				SYS_ERR(DTUN, LOGL_NOTICE, errno,
 					"ioctl(SIOCSIFADDR): Address already exists");
 			}
 			close(fd);
@@ -443,7 +442,7 @@
 		memcpy(&((struct sockaddr_in *)&ifr.ifr_dstaddr)->sin_addr,
 		       dstaddr, sizeof(*dstaddr));
 		if (ioctl(fd, SIOCSIFDSTADDR, (caddr_t) & ifr) < 0) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+			SYS_ERR(DTUN, LOGL_ERROR, errno,
 				"ioctl(SIOCSIFDSTADDR) failed");
 			close(fd);
 			return -1;
@@ -468,7 +467,7 @@
 #endif
 
 		if (ioctl(fd, SIOCSIFNETMASK, (void *)&ifr) < 0) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+			SYS_ERR(DTUN, LOGL_ERROR, errno,
 				"ioctl(SIOCSIFNETMASK) failed");
 			close(fd);
 			return -1;
@@ -510,7 +509,7 @@
 
 	/* Create a channel to the NET kernel. */
 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "socket() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
 		return -1;
 	}
 
@@ -525,14 +524,14 @@
 
 	if (delete) {
 		if (ioctl(fd, SIOCDELRT, (void *)&r) < 0) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+			SYS_ERR(DTUN, LOGL_ERROR, errno,
 				"ioctl(SIOCDELRT) failed");
 			close(fd);
 			return -1;
 		}
 	} else {
 		if (ioctl(fd, SIOCADDRT, (void *)&r) < 0) {
-			sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+			SYS_ERR(DTUN, LOGL_ERROR, errno,
 				"ioctl(SIOCADDRT) failed");
 			close(fd);
 			return -1;
@@ -554,7 +553,7 @@
 	struct rt_msghdr *rtm;
 
 	if ((fd = socket(AF_ROUTE, SOCK_RAW, 0)) == -1) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "socket() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
 		return -1;
 	}
 
@@ -586,7 +585,7 @@
 	req.gate.sin_addr.s_addr = gateway->s_addr;
 
 	if (write(fd, rtm, rtm->rtm_msglen) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "write() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "write() failed");
 		close(fd);
 		return -1;
 	}
@@ -594,7 +593,7 @@
 	return 0;
 
 #elif defined(__sun__)
-	sys_err(LOG_WARNING, __FILE__, __LINE__, errno,
+	SYS_ERR(DTUN, LOGL_NOTICE, errno,
 		"Could not set up routing on Solaris. Please add route manually.");
 	return 0;
 
@@ -641,7 +640,7 @@
 #endif
 
 	if (!(*tun = calloc(1, sizeof(struct tun_t)))) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "calloc() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "calloc() failed");
 		return EOF;
 	}
 
@@ -652,7 +651,7 @@
 #if defined(__linux__)
 	/* Open the actual tun device */
 	if (((*tun)->fd = open("/dev/net/tun", O_RDWR)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "open() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "open() failed");
 		return -1;
 	}
 
@@ -661,7 +660,7 @@
 	memset(&ifr, 0, sizeof(ifr));
 	ifr.ifr_flags = IFF_TUN | IFF_NO_PI;	/* Tun device, no packet info */
 	if (ioctl((*tun)->fd, TUNSETIFF, (void *)&ifr) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "ioctl() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "ioctl() failed");
 		close((*tun)->fd);
 		return -1;
 	}
@@ -683,7 +682,7 @@
 			break;
 	}
 	if ((*tun)->fd < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"Can't find tunnel device");
 		return -1;
 	}
@@ -702,7 +701,7 @@
 
 	/* Create a channel to the NET kernel. */
 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "socket() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed");
 		return -1;
 	}
 
@@ -715,45 +714,45 @@
 #elif defined(__sun__)
 
 	if ((ip_fd = open("/dev/udp", O_RDWR, 0)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"Can't open /dev/udp");
 		return -1;
 	}
 
 	if (((*tun)->fd = open("/dev/tun", O_RDWR, 0)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"Can't open /dev/tun");
 		return -1;
 	}
 
 	/* Assign a new PPA and get its unit number. */
 	if ((ppa = ioctl((*tun)->fd, TUNNEWPPA, -1)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"Can't assign new interface");
 		return -1;
 	}
 
 	if ((if_fd = open("/dev/tun", O_RDWR, 0)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"Can't open /dev/tun (2)");
 		return -1;
 	}
 	if (ioctl(if_fd, I_PUSH, "ip") < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"Can't push IP module");
 		return -1;
 	}
 
 	/* Assign ppa according to the unit number returned by tun device */
 	if (ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "Can't set PPA %d",
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "Can't set PPA %d",
 			ppa);
 		return -1;
 	}
 
 	/* Link the two streams */
 	if ((muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"Can't link TUN device to IP");
 		return -1;
 	}
@@ -769,7 +768,7 @@
 
 	if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
 		ioctl(ip_fd, I_PUNLINK, muxid);
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"Can't set multiplexor id");
 		return -1;
 	}
@@ -793,7 +792,7 @@
 	}
 
 	if (close(tun->fd)) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "close() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "close() failed");
 	}
 
 	/* TODO: For solaris we need to unlink streams */
@@ -818,7 +817,7 @@
 	int status;
 
 	if ((status = read(this->fd, buffer, sizeof(buffer))) <= 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "read() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "read() failed");
 		return -1;
 	}
 
@@ -836,7 +835,7 @@
 	sbuf.maxlen = PACKET_MAX;
 	sbuf.buf = buffer;
 	if (getmsg(this->fd, NULL, &sbuf, &f) < 0) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno, "getmsg() failed");
+		SYS_ERR(DTUN, LOGL_ERROR, errno, "getmsg() failed");
 		return -1;
 	}
 
@@ -885,7 +884,7 @@
 	buf[sizeof(buf) - 1] = 0;
 	rc = system(buf);
 	if (rc == -1) {
-		sys_err(LOG_ERR, __FILE__, __LINE__, errno,
+		SYS_ERR(DTUN, LOGL_ERROR, errno,
 			"Error executing command %s", buf);
 		return -1;
 	}