Include line and function name in nblog() output.
authorEmil Mikulic <emikulic@gmail.com>
Sun, 24 Mar 2013 14:28:50 +0000 (01:28 +1100)
committerEmil Mikulic <emikulic@gmail.com>
Sun, 24 Mar 2013 17:27:21 +0000 (04:27 +1100)
buftee.c

index 651e6f3..b69b318 100644 (file)
--- a/buftee.c
+++ b/buftee.c
@@ -179,7 +179,14 @@ static int gettid(void) {
   return (int)syscall(SYS_gettid);
 }
 
-static void nblogx(const char* format, ...);  // Forward.
+static void _nblog_helper(int want_errno,
+                          int line,
+                          const char* func,
+                          const char* format,
+                          ...);  // Forward.
+
+#define nblog(fmt...) _nblog_helper(1, __LINE__, __FUNCTION__, fmt)
+#define nblogx(fmt...) _nblog_helper(0, __LINE__, __FUNCTION__, fmt)
 
 static void warn_time(const char* desc,
                       const struct timespec* restrict start,
@@ -361,9 +368,17 @@ static void init_logger_thread(void) {
 }
 
 static void _nblog_helper(int want_errno,
-                          int saved_errno,
+                          int line,
+                          const char* func,
                           const char* format,
-                          va_list va) {
+                          ...) {
+  int saved_errno;
+  if (want_errno)
+    saved_errno = errno;
+
+  va_list va;
+  va_start(va, format);
+
   // Timing.
   struct timespec now;
   struct timespec diff;
@@ -374,8 +389,9 @@ static void _nblog_helper(int want_errno,
   char buf[512];
   size_t len;
   extern char *__progname;  // This is where glibc stashes argv[0].
-  len = snprintf(buf, sizeof(buf), "%s: tid %d at %d.%09d: ",
-                 __progname, gettid(), (int)diff.tv_sec, (int)diff.tv_nsec);
+  len = snprintf(buf, sizeof(buf), "%s:%d:%s(): tid %d at %d.%09d: ",
+                 __progname, line, func, gettid(),
+                 (int)diff.tv_sec, (int)diff.tv_nsec);
 
   // Format message.
   len += vsnprintf(buf + len, sizeof(buf) - len, format, va);
@@ -395,22 +411,6 @@ static void _nblog_helper(int want_errno,
   unlock(logger->mutex);
 }
 
-// nblog() is like warn() but non-blocking.
-static void nblog(const char* format, ...) {
-  int saved_errno = errno;
-  va_list va;
-  va_start(va, format);
-  _nblog_helper(1, saved_errno, format, va);
-  va_end(va);
-}
-
-static void nblogx(const char* format, ...) {
-  va_list va;
-  va_start(va, format);
-  _nblog_helper(0, 0, format, va);
-  va_end(va);
-}
-
 static void xpthread_join(pthread_t thread) {
   int ret = pthread_join(thread, NULL);
   if (ret == 0) return;