From ef5d0f68fdf7003fc1665d77c2630fe24ff1ba65 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Fri, 14 Mar 2014 00:28:04 +0400 Subject: Avoid locking in __debug() --- debug.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/debug.c b/debug.c index 4263002..67f034f 100644 --- a/debug.c +++ b/debug.c @@ -24,12 +24,11 @@ THE SOFTWARE. #include "config.h" #endif -#include #include #include +#include #include - -static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; +#include void __debug (const char *file, int line, const char *func, const char *msg, ...) @@ -37,20 +36,30 @@ __debug (const char *file, int line, const char *func, const char *msg, ...) va_list ap; time_t ltime; char time_str[32]; + char buf[512]; + int bytes = 0; ltime = time (NULL); strftime (time_str, sizeof (time_str), "%Y-%m-%d %T %z", localtime (<ime)); - va_start (ap, msg); - pthread_mutex_lock (&debug_mutex); + bytes = + snprintf (buf, sizeof (buf), "%s %s:%d (%s): ", time_str, file, line, + func); + + if (bytes < sizeof (buf)) + { + va_start (ap, msg); + bytes += vsnprintf (buf + bytes, sizeof (buf) - bytes, msg, ap); + va_end (ap); + } - fprintf (stderr, "%s %s:%d (%s): ", time_str, file, line, func); - vfprintf (stderr, msg, ap); - fprintf (stderr, "\n"); + if (bytes < sizeof (buf)) + bytes += snprintf (buf + bytes, sizeof (buf) - bytes, "\n"); + else + strcpy (buf + sizeof (buf) - 5, "...\n"); - pthread_mutex_unlock (&debug_mutex); + (void) write (2, buf, strlen (buf)); - va_end (ap); } -- cgit v1.2.3