void w_log(int level, const char *fmt, ...) { char buf[4096]; va_list ap; int len; uint32_t tid = (uint32_t)(uintptr_t)pthread_self(); bool fatal = false; if (level == W_LOG_FATAL) { level = W_LOG_ERR; fatal = true; } len = snprintf(buf, sizeof(buf), "%d: tid=%" PRIu32 " ", (int)time(NULL), tid); va_start(ap, fmt); len += vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); va_end(ap); if (level <= log_level) { ignore_result(write(STDERR_FILENO, buf, len)); } w_log_to_clients(level, buf); if (fatal) { log_stack_trace(); abort(); } }
void w_log(int level, const char *fmt, ...) { char buf[4096]; va_list ap; int len; bool fatal = false; struct timeval tv; char timebuf[64]; struct tm tm; bool should_log_to_stderr = level <= log_level; bool should_log_to_clients = w_should_log_to_clients(level); if (!(should_log_to_stderr || should_log_to_clients)) { // Don't bother formatting the log message if nobody's listening. return; } if (level == W_LOG_FATAL) { level = W_LOG_ERR; fatal = true; } gettimeofday(&tv, NULL); localtime_r(&tv.tv_sec, &tm); strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%S", &tm); len = snprintf(buf, sizeof(buf), "%s,%03d: [%s] ", timebuf, (int)tv.tv_usec / 1000, w_get_thread_name()); va_start(ap, fmt); vsnprintf(buf + len, sizeof(buf) - len, fmt, ap); va_end(ap); len = strlen(buf); if (buf[len - 1] != '\n') { if (len < (int)sizeof(buf) - 1) { buf[len] = '\n'; buf[len + 1] = 0; len++; } else { buf[len - 1] = '\n'; } } if (should_log_to_stderr) { ignore_result(write(STDERR_FILENO, buf, len)); } if (should_log_to_clients) { w_log_to_clients(level, buf); } if (fatal) { log_stack_trace(); abort(); } }