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,
}
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;
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);
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;