Clean up internal libc logging.

We only need one logging API, and I prefer the one that does no
allocation and is thus safe to use in any context.

Also use O_CLOEXEC when opening the /dev/log files.

Move everything logging-related into one header file.

Change-Id: Ic1e3ea8e9b910dc29df351bff6c0aa4db26fbb58
diff --git a/libc/netbsd/resolv/res_cache.c b/libc/netbsd/resolv/res_cache.c
index 838e084..a2bc7ca 100644
--- a/libc/netbsd/resolv/res_cache.c
+++ b/libc/netbsd/resolv/res_cache.c
@@ -170,9 +170,8 @@
 
 #undef XLOG
 #if DEBUG
-#  include <logd.h>
-#  define  XLOG(...)   \
-    __libc_android_log_print(ANDROID_LOG_DEBUG,"libc",__VA_ARGS__)
+#  include "libc_logging.h"
+#  define XLOG(...)  __libc_format_log(ANDROID_LOG_DEBUG,"libc",__VA_ARGS__)
 
 #include <stdio.h>
 #include <stdarg.h>
@@ -708,9 +707,9 @@
 _dnsPacket_bprintQR(DnsPacket*  packet, char*  p, char*  end)
 {
 #define  QQ(x)   { DNS_TYPE_##x, #x }
-    static const struct { 
+    static const struct {
         const char*  typeBytes;
-        const char*  typeString; 
+        const char*  typeString;
     } qTypes[] =
     {
         QQ(A), QQ(PTR), QQ(MX), QQ(AAAA), QQ(ALL),
@@ -1487,7 +1486,7 @@
 
         pnode = &node->hlink;
     }
-    return pnode; 
+    return pnode;
 }
 
 /* Add a new entry to the hash table. 'lookup' must be the
diff --git a/libc/netbsd/resolv/res_send.c b/libc/netbsd/resolv/res_send.c
index f3ee539..c6c863b 100644
--- a/libc/netbsd/resolv/res_send.c
+++ b/libc/netbsd/resolv/res_send.c
@@ -119,7 +119,7 @@
 #  include <resolv_cache.h>
 #endif
 
-#include "logd.h"
+#include "libc_logging.h"
 
 #ifndef DE_CONST
 #define DE_CONST(c,v)   v = ((c) ? \
@@ -542,7 +542,7 @@
 				    ns);
 
 			if (DBG) {
-				__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
+				__libc_format_log(ANDROID_LOG_DEBUG, "libc",
 					"used send_vc %d\n", n);
 			}
 
@@ -554,15 +554,13 @@
 		} else {
 			/* Use datagrams. */
 			if (DBG) {
-				__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
-					"using send_dg\n");
+				__libc_format_log(ANDROID_LOG_DEBUG, "libc", "using send_dg\n");
 			}
 
 			n = send_dg(statp, buf, buflen, ans, anssiz, &terrno,
 				    ns, &v_circuit, &gotsomewhere);
 			if (DBG) {
-				__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
-					"used send_dg %d\n",n);
+				__libc_format_log(ANDROID_LOG_DEBUG, "libc", "used send_dg %d\n",n);
 			}
 
 			if (n < 0)
@@ -570,7 +568,7 @@
 			if (n == 0)
 				goto next_ns;
 			if (DBG) {
-				__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
+				__libc_format_log(ANDROID_LOG_DEBUG, "libc",
 					"time=%d, %d\n",time(NULL), time(NULL)%2);
 			}
 			if (v_circuit)
@@ -715,8 +713,7 @@
 		timeout = 1;
 	}
 	if (DBG) {
-		__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
-			"using timeout of %d sec\n", timeout);
+		__libc_format_log(ANDROID_LOG_DEBUG, "libc", "using timeout of %d sec\n", timeout);
 	}
 
 	return timeout;
@@ -738,7 +735,7 @@
 	void *tmp;
 
 	if (DBG) {
-		__libc_android_log_print(ANDROID_LOG_DEBUG, "libc", "using send_vc\n");
+		__libc_format_log(ANDROID_LOG_DEBUG, "libc", "using send_vc\n");
 	}
 
 	nsap = get_nsaddr(statp, (size_t)ns);
@@ -939,8 +936,7 @@
 		timeout = evConsTime((long)sec, 0L);
 		finish = evAddTime(now, timeout);
 		if (DBG) {
-			__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
-				"  %d send_vc\n", sock);
+			__libc_format_log(ANDROID_LOG_DEBUG, "libc", "  %d send_vc\n", sock);
 		}
 
 		res = retrying_select(sock, &rset, &wset, &finish);
@@ -951,7 +947,7 @@
 done:
 	fcntl(sock, F_SETFL, origflags);
 	if (DBG) {
-		__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
+		__libc_format_log(ANDROID_LOG_DEBUG, "libc",
 			"  %d connect_with_timeout returning %s\n", sock, res);
 	}
 	return res;
@@ -967,7 +963,7 @@
 
 retry:
 	if (DBG) {
-		__libc_android_log_print(ANDROID_LOG_DEBUG, "libc", "  %d retying_select\n", sock);
+		__libc_format_log(ANDROID_LOG_DEBUG, "libc", "  %d retying_select\n", sock);
 	}
 
 	now = evNowTime();
@@ -987,7 +983,7 @@
 	n = pselect(sock + 1, readset, writeset, NULL, &timeout, NULL);
 	if (n == 0) {
 		if (DBG) {
-			__libc_android_log_print(ANDROID_LOG_DEBUG, " libc",
+			__libc_format_log(ANDROID_LOG_DEBUG, " libc",
 				"  %d retrying_select timeout\n", sock);
 		}
 		errno = ETIMEDOUT;
@@ -997,7 +993,7 @@
 		if (errno == EINTR)
 			goto retry;
 		if (DBG) {
-			__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
+			__libc_format_log(ANDROID_LOG_DEBUG, "libc",
 				"  %d retrying_select got error %d\n",sock, n);
 		}
 		return n;
@@ -1007,7 +1003,7 @@
 		if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
 			errno = error;
 			if (DBG) {
-				__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
+				__libc_format_log(ANDROID_LOG_DEBUG, "libc",
 					"  %d retrying_select dot error2 %d\n", sock, errno);
 			}
 
@@ -1015,7 +1011,7 @@
 		}
 	}
 	if (DBG) {
-		__libc_android_log_print(ANDROID_LOG_DEBUG, "libc",
+		__libc_format_log(ANDROID_LOG_DEBUG, "libc",
 			"  %d retrying_select returning %d for %d\n",sock, n);
 	}
 
diff --git a/libc/netbsd/resolv/res_state.c b/libc/netbsd/resolv/res_state.c
index e05846a..3e1f67b 100644
--- a/libc/netbsd/resolv/res_state.c
+++ b/libc/netbsd/resolv/res_state.c
@@ -42,9 +42,9 @@
 #define DEBUG 0
 
 #if DEBUG
-#  include <logd.h>
+#  include "libc_logging.h"
 #  include <unistd.h>  /* for gettid() */
-#  define D(...)  __libc_android_log_print(ANDROID_LOG_DEBUG,"libc", __VA_ARGS__)
+#  define D(...) __libc_format_log(ANDROID_LOG_DEBUG,"libc", __VA_ARGS__)
 #else
 #  define D(...)  do{}while(0)
 #endif