/* * Local snprintf which handles the buf-size not. Returns the number * of characters copied into BUF. */ int loc_snprintf(char *buf, const int buf_size, const char *format, ...) { va_list args; int len; va_start(args, format); len = loc_vsnprintf(buf, buf_size, format, args); va_end(args); return len; }
/* * void _dmalloc_vmessage * * DESCRIPTION: * * Message writer with vprintf like arguments which adds a line to the * dmalloc logfile. * * RETURNS: * * None. * * ARGUMENTS: * * format -> Printf-style format statement. * * args -> Already converted pointer to a stdarg list. */ void _dmalloc_vmessage(const char *format, va_list args) { char *str_p, *bounds_p; int len; str_p = message_str; bounds_p = str_p + sizeof(message_str); /* no logpath and no print then no workie */ if (dmalloc_logpath == NULL && ! BIT_IS_SET(_dmalloc_flags, DEBUG_PRINT_MESSAGES)) { return; } #if HAVE_GETPID && LOG_REOPEN if (dmalloc_logpath != NULL) { char *log_p; /* * This static pid will be checked to make sure it doesn't change. * We make it long in case it's big and we hope it will promote if * not. */ static long current_pid = -1; long new_pid; new_pid = getpid(); if (new_pid != current_pid) { /* NOTE: we need to do this _before_ the reopen otherwise we recurse */ current_pid = new_pid; /* if the new pid doesn't match the old one then reopen it */ if (current_pid >= 0) { /* this only works if there is a %p in the logpath */ for (log_p = dmalloc_logpath; *log_p != '\0'; log_p++) { if (*log_p == '%' && *(log_p + 1) == 'p') { _dmalloc_reopen_log(); break; } } } } } #endif /* do we need to open the logfile? */ if (dmalloc_logpath != NULL && outfile_fd < 0) { _dmalloc_open_log(); } #if HAVE_TIME #if LOG_TIME_NUMBER { long now; now = time(NULL); str_p += loc_snprintf(str_p, bounds_p - str_p, "%ld: ", now); } #endif /* LOG_TIME_NUMBER */ #if HAVE_CTIME #if LOG_CTIME_STRING { TIME_TYPE now; now = time(NULL); str_p += loc_snprintf(str_p, bounds_p - str_p, "%.24s: ", ctime(&now)); } #endif /* LOG_CTIME_STRING */ #endif /* HAVE_CTIME */ #endif /* HAVE_TIME */ #if LOG_ITERATION /* add the iteration number */ str_p += loc_snprintf(str_p, bounds_p - str_p, "%lu: ", _dmalloc_iter_c); #endif #if LOG_PID && HAVE_GETPID { /* we make it long in case it's big and we hope it will promote if not */ long our_pid = getpid(); /* add the pid to the log file */ str_p += loc_snprintf(str_p, bounds_p - str_p, "p%ld: ", our_pid); } #endif /* * NOTE: the following code, as well as the function definition * above, would need to be altered to conform to non-ANSI-C * specifications if necessary. */ /* write the format + info into str */ len = loc_vsnprintf(str_p, bounds_p - str_p, format, args); /* was it an empty format? */ if (len == 0) { return; } str_p += len; /* tack on a '\n' if necessary */ if (*(str_p - 1) != '\n') { *str_p++ = '\n'; *str_p = '\0'; } len = str_p - message_str; /* do we need to write the message to the logfile */ if (dmalloc_logpath != NULL) { (void)write(outfile_fd, message_str, len); } /* do we need to print the message? */ if (BIT_IS_SET(_dmalloc_flags, DEBUG_PRINT_MESSAGES)) { (void)write(STDERR, message_str, len); } }