int link_putvfstring(struct link *link, const char *fmt, time_t stoptime, va_list va) { int rc; size_t l; const char *str; buffer_t B; buffer_init(&B); if (buffer_putvfstring(&B, fmt, va) == -1) return -1; str = buffer_tolstring(&B, &l); rc = link_putlstring(link, str, l, stoptime); buffer_free(&B); return rc; }
static void do_debug(INT64_T flags, const char *fmt, va_list args) { buffer_t B; char ubuf[1<<16]; buffer_init(&B); buffer_ubuf(&B, ubuf, sizeof(ubuf)); buffer_max(&B, sizeof(ubuf)); if (debug_write == debug_file_write || debug_write == debug_stderr_write || debug_write == debug_stdout_write) { struct timeval tv; struct tm *tm; gettimeofday(&tv, 0); tm = localtime(&tv.tv_sec); buffer_putfstring(&B, "%04d/%02d/%02d %02d:%02d:%02d.%02ld ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (long) tv.tv_usec / 10000); buffer_putfstring(&B, "%s[%d] ", debug_program_name, getpid()); } /* Parrot prints debug messages for children: */ if (getpid() != debug_getpid()) { buffer_putfstring(&B, "<child:%d> ", (int)debug_getpid()); } buffer_putfstring(&B, "%s: ", debug_flags_to_name(flags)); buffer_putvfstring(&B, fmt, args); while(isspace(buffer_tostring(&B)[buffer_pos(&B)-1])) buffer_rewind(&B, buffer_pos(&B)-1); /* chomp whitespace */ buffer_putliteral(&B, "\n"); debug_write(flags, buffer_tostring(&B)); if(terminal_available && (flags & (D_ERROR | D_NOTICE | D_FATAL))) { if(debug_write != debug_stderr_write || !isatty(STDERR_FILENO)) { if(!terminal_f) { if((terminal_f = fopen(terminal_path, "a")) == NULL) { /* print to wherever stderr is pointing that we could not open the terminal. */ terminal_available = 0; } } } if(terminal_f) fprintf(terminal_f, "%s", buffer_tostring(&B)); } buffer_free(&B); }
struct jx * jx_format( const char *fmt, ... ) { va_list va; struct jx *j; buffer_t B[1]; char *str; buffer_init(B); buffer_abortonfailure(B, 1); va_start(va, fmt); buffer_putvfstring(B, fmt, va); va_end(va); buffer_dup(B, &str); buffer_free(B); j = jx_create(JX_STRING); j->u.string_value = str; return j; }