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); }
static void do_debug(int is_fatal, INT64_T flags, const char *fmt, va_list args) { char newfmt[65536]; char buffer[65536]; int length; struct timeval tv; struct tm *tm; gettimeofday(&tv, 0); tm = localtime(&tv.tv_sec); sprintf(newfmt, "%04d/%02d/%02d %02d:%02d:%02d.%02ld [%d] %s: %s: %s", 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, (int) debug_getpid(), program_name, is_fatal ? "fatal " : flag_to_name(flags), fmt); vsprintf(buffer, newfmt, args); string_chomp(buffer); strcat(buffer, "\n"); length = strlen(buffer); if(debug_file) { struct stat info; fstat(debug_fd, &info); if(info.st_size >= debug_file_size && debug_file_size != 0) { close(debug_fd); if(stat(debug_file, &info) == 0) { if(info.st_size >= debug_file_size) { char *newname = malloc(strlen(debug_file) + 5); sprintf(newname, "%s.old", debug_file); rename(debug_file, newname); free(newname); } } debug_fd = open(debug_file, O_CREAT | O_TRUNC | O_WRONLY, 0660); if(debug_fd == -1){ debug_fd = STDERR_FILENO; fatal("could not open log file `%s': %s", debug_file, strerror(errno)); } } } full_write(debug_fd, buffer, length); }