📄 smpd_printf.c
字号:
} /* else { printf("unable to truncate the smpd log file.\n"); fflush(stdout); } */ } /* else { printf("read %d instead of %d bytes from the smpd log file.\n", num_read, size); fflush(stdout); } */ free(buffer_orig); } /* else { printf("malloc failed to allocate %d bytes.\n", smpd_process.dbg_file_size / 2); fflush(stdout); } */ } /* else { printf("smpd file size: %d\n", size); fflush(stdout); } */ fclose(fout); if (docopy) { sprintf(copy_cmd, "copy %s %s.%d.new", smpd_process.dbg_filename, smpd_process.dbg_filename, copy_number); system(copy_cmd); copy_number++; }}void smpd_trim_logfile_old(){ static int count = 0; FILE *fout; long size; int num_read, num_written; int new_size; char *buffer; char copy_cmd[4096]; int docopy = 0; static copy_number = 0; int a, b; /* check every 1000 iterations */ if (count++ % 1000) return; fout = fopen(smpd_process.dbg_filename, "rb"); if (fout == NULL) { /*printf("unable to open the smpd log file\n");*/ return; } fseek(fout, 0, SEEK_END); size = ftell(fout); if (size > smpd_process.dbg_file_size) { if (copy_number == 0) { srand(smpd_getpid()); copy_number = rand(); } sprintf(copy_cmd, "copy %s %s.%d.old", smpd_process.dbg_filename, smpd_process.dbg_filename, copy_number); system(copy_cmd); new_size = smpd_process.dbg_file_size / 2; buffer = malloc(new_size); if (buffer != NULL) { fseek(fout, - (long)new_size, SEEK_END); num_read = read_file(fout, buffer, new_size); if (num_read == new_size) { fclose(fout); fout = fopen(smpd_process.dbg_filename, "rb"); fseek(fout, -4, SEEK_END); fread(&a, 1, 4, fout); fclose(fout); if (a != *(int*)&buffer[new_size-4]) { FILE *ferr = fopen("c:\\temp\\smpd.err", "a+"); fprintf(ferr, "last 4 bytes not equal, a: %x != %x", a, *(int*)&buffer[new_size-4]); fclose(ferr); } fout = fopen(smpd_process.dbg_filename, "wb"); if (fout != NULL) { num_written = write_file(fout, buffer, new_size); docopy = 1; if (num_written != new_size) { FILE *ferr = fopen("c:\\temp\\smpd.err", "a+"); fprintf(ferr, "wrote %d instead of %d bytes to the smpd log file.\n", num_written, new_size); fclose(ferr); } /* else { printf("wrote %d bytes to the smpd log file.\n", num_written); fflush(stdout); } */ fclose(fout); fout = fopen(smpd_process.dbg_filename, "rb"); fseek(fout, -4, SEEK_END); fread(&b, 1, 4, fout); fclose(fout); if (b != *(int*)&buffer[new_size-4]) { FILE *ferr = fopen("c:\\temp\\smpd.err", "a+"); fprintf(ferr, "last 4 bytes not equal, b: %x != %x", b, *(int*)&buffer[new_size-4]); fclose(ferr); } } /* else { printf("unable to truncate the smpd log file.\n"); fflush(stdout); } */ } /* else { printf("read %d instead of %d bytes from the smpd log file.\n", num_read, new_size); fflush(stdout); } */ free(buffer); } /* else { printf("malloc failed to allocate %d bytes.\n", smpd_process.dbg_file_size / 2); fflush(stdout); } */ } /* else { printf("smpd file size: %d\n", size); fflush(stdout); } */ fclose(fout); if (docopy) { sprintf(copy_cmd, "copy %s %s.%d.new", smpd_process.dbg_filename, smpd_process.dbg_filename, copy_number); system(copy_cmd); copy_number++; }}#endifint smpd_err_printf(char *str, ...){ va_list list; char *indent_str; char *cur_str; int num_bytes; if (smpd_process.id == -1) smpd_init_printf(); if (!(smpd_process.dbg_state & (SMPD_DBG_STATE_ERROUT | SMPD_DBG_STATE_LOGFILE))) return 0;#ifdef HAVE_WINDOWS_H if (!smpd_process.bOutputInitialized) { smpd_process.hOutputMutex = CreateMutex(NULL, FALSE, SMPD_OUTPUT_MUTEX_NAME); smpd_process.bOutputInitialized = TRUE; } WaitForSingleObject(smpd_process.hOutputMutex, INFINITE);#endif /* write the formatted string to a global buffer */ if (smpd_process.dbg_state & SMPD_DBG_STATE_TRACE) indent_str = indent; else indent_str = ""; num_bytes = 0; if (smpd_process.dbg_state & SMPD_DBG_STATE_PREPEND_RANK) { /* prepend output with the process tree node id */ num_bytes = snprintf(smpd_process.printf_buffer, SMPD_MAX_DBG_PRINTF_LENGTH, "[%02d:%d]%sERROR:", smpd_process.id, smpd_getpid(), indent_str); } else { num_bytes = snprintf(smpd_process.printf_buffer, SMPD_MAX_DBG_PRINTF_LENGTH, "%s", indent_str); } cur_str = &smpd_process.printf_buffer[num_bytes]; va_start(list, str); num_bytes += vsnprintf(cur_str, SMPD_MAX_DBG_PRINTF_LENGTH - num_bytes, str, list); va_end(list); /* strip protected fields - passwords, etc */ smpd_clean_output(smpd_process.printf_buffer); if (smpd_process.dbg_state & SMPD_DBG_STATE_ERROUT) { /* use stdout instead of stderr so that ordering will be consistent with dbg messages */ printf("%s", smpd_process.printf_buffer); fflush(stdout); } if ((smpd_process.dbg_state & SMPD_DBG_STATE_LOGFILE) && (smpd_process.dbg_filename[0] != '\0')) { FILE *fout; smpd_trim_logfile(); fout = fopen(smpd_process.dbg_filename, "a+"); if (fout == NULL) { smpd_process.dbg_state ^= SMPD_DBG_STATE_LOGFILE; } else { setvbuf(fout, NULL, _IONBF, 0); fprintf(fout, "%s", smpd_process.printf_buffer); fclose(fout); } } #ifdef HAVE_WINDOWS_H ReleaseMutex(smpd_process.hOutputMutex);#endif return num_bytes;}int smpd_dbg_printf(char *str, ...){ va_list list; char *indent_str; char *cur_str; int num_bytes; if (smpd_process.id == -1) smpd_init_printf(); if (!(smpd_process.dbg_state & (SMPD_DBG_STATE_STDOUT | SMPD_DBG_STATE_LOGFILE))) return 0;#ifdef HAVE_WINDOWS_H if (!smpd_process.bOutputInitialized) { smpd_process.hOutputMutex = CreateMutex(NULL, FALSE, SMPD_OUTPUT_MUTEX_NAME); smpd_process.bOutputInitialized = TRUE; } WaitForSingleObject(smpd_process.hOutputMutex, INFINITE);#endif /* write the formatted string to a global buffer */ if (smpd_process.dbg_state & SMPD_DBG_STATE_TRACE) indent_str = indent; else indent_str = ""; num_bytes = 0; if (smpd_process.dbg_state & SMPD_DBG_STATE_PREPEND_RANK) { /* prepend output with the process tree node id */ num_bytes = snprintf(smpd_process.printf_buffer, SMPD_MAX_DBG_PRINTF_LENGTH, "[%02d:%d]%s", smpd_process.id, smpd_getpid(), indent_str); } else { num_bytes = snprintf(smpd_process.printf_buffer, SMPD_MAX_DBG_PRINTF_LENGTH, "%s", indent_str); } cur_str = &smpd_process.printf_buffer[num_bytes]; va_start(list, str); num_bytes += vsnprintf(cur_str, SMPD_MAX_DBG_PRINTF_LENGTH - num_bytes, str, list); va_end(list); /* strip protected fields - passwords, etc */ smpd_clean_output(smpd_process.printf_buffer); if (smpd_process.dbg_state & SMPD_DBG_STATE_STDOUT) { printf("%s", smpd_process.printf_buffer); fflush(stdout); } if ((smpd_process.dbg_state & SMPD_DBG_STATE_LOGFILE) && (smpd_process.dbg_filename[0] != '\0')) { FILE *fout = NULL; smpd_trim_logfile(); fout = fopen(smpd_process.dbg_filename, "a+"); if (fout == NULL) { /*smpd_process.dbg_state ^= SMPD_DBG_STATE_LOGFILE;*/ } else { setvbuf(fout, NULL, _IONBF, 0); fprintf(fout, "%s", smpd_process.printf_buffer); fclose(fout); } } #ifdef HAVE_WINDOWS_H ReleaseMutex(smpd_process.hOutputMutex);#endif return num_bytes;}int smpd_enter_fn(char *fcname){ if (smpd_process.dbg_state & SMPD_DBG_STATE_TRACE) { smpd_dbg_printf("\\%s\n", fcname); } if (cur_indent >= 0 && cur_indent < SMPD_MAX_INDENT) { indent[cur_indent] = '.'; indent[cur_indent+1] = '\0'; } cur_indent++; return SMPD_SUCCESS;}int smpd_exit_fn(char *fcname){ if (cur_indent > 0 && cur_indent < SMPD_MAX_INDENT) { indent[cur_indent-1] = '\0'; } cur_indent--; if (smpd_process.dbg_state & SMPD_DBG_STATE_TRACE) { smpd_dbg_printf("/%s\n", fcname); } return SMPD_SUCCESS;}SMPD_BOOL smpd_snprintf_update(char **str_pptr, int *len_ptr, char *str_format, ...){ va_list list; int n; va_start(list, str_format); n = vsnprintf(*str_pptr, *len_ptr, str_format, list); va_end(list); if (n < 0) { (*str_pptr)[(*len_ptr)-1] = '\0'; *len_ptr = 0; return SMPD_FALSE; } (*str_pptr )= &(*str_pptr)[n]; *len_ptr = (*len_ptr) - n; return SMPD_TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -