int util_asprintf(char **ret, const char *fmt, ...) { va_list ap; va_start(ap, fmt); int n = util_vasprintf(ret, fmt, ap); va_end(ap); return n; }
/* Implementation of asprintf from GNU/BSD; leverages vasprintf */ int util_asprintf(char **out, const char *fmt, ...) { int alloc_size; va_list ap; va_start(ap, fmt); alloc_size = util_vasprintf(out, fmt, ap); va_end(ap); return alloc_size; }
int util_log_vasprintf(char **result, const char *fmt, va_list args) { int ret; struct timeval tv; struct timezone tz; struct tm tm_gmt; if( result ) *result = NULL; gettimeofday(&tv, &tz); time_t tt = tv.tv_sec; gmtime_r(&tt, &tm_gmt); char timestamp[ sizeof("YYYY-MM-DD HH:MM:SS") ]; strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", &tm_gmt); char *msg = NULL; if( NULL != fmt ) util_vasprintf(&msg, fmt, args); #if 1 ret = util_asprintf(result, "%10llu.%06d %s %llu %s", (unsigned long long)tv.tv_sec, (int) tv.tv_usec, timestamp, (unsigned long long) getpid(), msg ? msg : ""); #else int minutes = tm_gmt.tm_hour * 60 + tm_gmt.tm_min - tz.tz_minuteswest; if( minutes < 0 ) minutes = 24 * 60 - ( -minutes % ( 24 * 60 ) ); if( minutes >= 24*60 ) minutes %= ( 24 * 60 ); ret = util_asprintf(result, "%s.%06dZ %02d:%02d %llu %s", timestamp, (int)tv.tv_usec, minutes / 60, minutes % 60, (unsigned long long) getpid(), msg ? msg : ""); #endif free(msg); return ret; }