void PrintStatus(unsigned int duration, BOOL debug, const char *format, ...) { char *p = szStatusMessage; va_list args; int n; va_start(args, format); n = safe_vsnprintf(p, sizeof(szStatusMessage)-1, format, args); // room for NUL va_end(args); p += (n < 0)?sizeof(szStatusMessage)-1:n; while((p>szStatusMessage) && (isspace(p[-1]))) *--p = '\0'; *p = '\0'; if (debug) uprintf("%s\n", szStatusMessage); if ((duration) || (!bStatusTimerArmed)) { SendMessageLU(GetDlgItem(hMainDialog, IDC_STATUS), SB_SETTEXTA, SBT_OWNERDRAW | 0, szStatusMessage); } if (duration) { SetTimer(hMainDialog, TID_MESSAGE, duration, PrintStatusTimeout); bStatusTimerArmed = TRUE; } }
void _uprintf(const char *format, ...) { char buf[4096], *p = buf; va_list args; int n; va_start(args, format); n = safe_vsnprintf(p, sizeof(buf)-3, format, args); // buf-3 is room for CR/LF/NUL va_end(args); p += (n < 0)?sizeof(buf)-3:n; while((p>buf) && (isspace((unsigned char)p[-1]))) *--p = '\0'; *p++ = '\r'; *p++ = '\n'; *p = '\0'; // Send output to Windows debug facility OutputDebugStringA(buf); // Send output to our log Window Edit_SetSel(hLog, MAX_LOG_SIZE, MAX_LOG_SIZE); Edit_ReplaceSelU(hLog, buf); // Make sure the message scrolls into view // (Or see code commented in LogProc:WM_SHOWWINDOW for a less forceful scroll) SendMessage(hLog, EM_LINESCROLL, 0, SendMessage(hLog, EM_GETLINECOUNT, 0, 0)); }
ssize_t fvprintf(ssys::FileHandle hndl, const wchar_t* format, va_list vl) { memory::auto_array<wchar_t> buf(DEFAULT_PRINTF_BUFFER_LEN); while (!safe_vsnprintf(buf.data(), buf.size(), format, vl)) buf.reserve(buf.size() * 2); return fputs(buf.data(), cstr::length(buf.data()), hndl); }
// void ATSkeletonWindow::AddToLog( int iTunnelIndex, const char *format, ... ) void ATSkeletonWindow::AddToLog( Tunnel_c &tunnel, const char *format, ... ) { char buffer[8096]; va_list argptr; va_start(argptr, format); safe_vsnprintf(buffer, sizeof(buffer), format, argptr); va_end(argptr); QString str( buffer ); str.replace( '\n', "<br>" ); //Tunnel_c *pt = &m_listTunnels[iTunnelIndex]; Tunnel_c *pt = &tunnel; pt->byteLog.append( str ); pt->byteLog.append( "<br>" ); QTreeWidgetItem *twi = ui.treeTunnels->currentItem(); if ( ( twi != NULL ) && ( twi != tunnel.twi ) ) twi = NULL; if ( pt->byteLog.size() > (MAX_BUFFER_SIZE*2) ) { pt->byteLog = pt->byteLog.right( MAX_BUFFER_SIZE ); if ( twi != NULL ) { ui.textBrowser->setHtml( pt->byteLog ); ui.textBrowser->verticalScrollBar()->setValue( ui.textBrowser->verticalScrollBar()->maximum() ); } } if ( twi != NULL ) ui.textBrowser->append( str ); }
int safe_snprintf(char *buf, int size, const char *fmt, ...) { int i; va_list ap; va_start(ap, fmt); i = safe_vsnprintf(buf, size, fmt, ap); va_end(ap); return i; }
int safe_snprintf(char *buf, size_t size, const char *fmt, ...) { va_list ap; va_start(ap, fmt); int ret = safe_vsnprintf(buf, size, fmt, ap); // 调用 acl::vsnprintf va_end(ap); return ret; }
static void _err_vformat(const char *format, va_list args) { char *ptr = err_message; err_frame_t *frame = _err_top; if(frame) ptr = frame->context(frame->userdata, ptr, endof(err_message)); safe_vsnprintf(ptr, endof(err_message) - ptr, format, args); }
int DECL_VARARGS safe_snprintf(char *buf, size_t len, const char *format, ...) { va_list args; int written; va_start(args, format); written = safe_vsnprintf(buf, len, format, args); va_end(args); return written; }
// Log data with parent app through the pipe void plog_v(const char *format, va_list args) { char buffer[STR_BUFFER_SIZE]; DWORD junk; int size; if (pipe_handle == INVALID_HANDLE_VALUE) return; buffer[0] = IC_PRINT_MESSAGE; size = safe_vsnprintf(buffer+1, STR_BUFFER_SIZE-1, format, args); if (size < 0) { buffer[STR_BUFFER_SIZE-1] = 0; size = STR_BUFFER_SIZE-2; } WriteFile(pipe_handle, buffer, (DWORD)size+2, &junk, NULL); }
/* * Display a custom notification */ BOOL notification(int type, const notification_info* more_info, char* title, char* format, ...) { BOOL ret; va_list args; dialog_showing++; szMessageText = (char*)malloc(MAX_PATH); if (szMessageText == NULL) return FALSE; szMessageTitle = title; va_start(args, format); safe_vsnprintf(szMessageText, MAX_PATH-1, format, args); va_end(args); szMessageText[MAX_PATH-1] = 0; notification_more_info = more_info; notification_is_question = FALSE; switch(type) { case MSG_WARNING: hMessageIcon = LoadIcon(NULL, IDI_WARNING); break; case MSG_ERROR: hMessageIcon = LoadIcon(NULL, IDI_ERROR); break; case MSG_QUESTION: hMessageIcon = LoadIcon(NULL, IDI_QUESTION); notification_is_question = TRUE; break; case MSG_INFO: default: hMessageIcon = LoadIcon(NULL, IDI_INFORMATION); break; } ret = (DialogBox(main_instance, MAKEINTRESOURCE(IDD_NOTIFICATION), hMainDialog, notification_callback) == IDYES); safe_free(szMessageText); dialog_showing--; return ret; }
static void pipe_wdi_log_v(enum wdi_log_level level, const char *function, const char *format, va_list args) { char buffer[LOGBUF_SIZE]; DWORD junk; int size1, size2; BOOL truncated = FALSE; const char* prefix; const char* truncation_notice = "TRUNCATION detected for above line - Please " "send this log excerpt to the libwdi developers so we can fix it."; if (logger_wr_handle == INVALID_HANDLE_VALUE) return; #ifndef ENABLE_DEBUG_LOGGING if (level < global_log_level) return; #endif switch (level) { case WDI_LOG_LEVEL_DEBUG: prefix = "debug"; break; case WDI_LOG_LEVEL_INFO: prefix = "info"; break; case WDI_LOG_LEVEL_WARNING: prefix = "warning"; break; case WDI_LOG_LEVEL_ERROR: prefix = "error"; break; default: prefix = "unknown"; break; } size1 = safe_snprintf(buffer, LOGBUF_SIZE, "libwdi:%s [%s] ", prefix, function); size2 = 0; if (size1 < 0) { buffer[LOGBUF_SIZE-1] = 0; size1 = LOGBUF_SIZE-1; truncated = TRUE; } else { size2 = safe_vsnprintf(buffer+size1, LOGBUF_SIZE-size1, format, args); if (size2 < 0) { buffer[LOGBUF_SIZE-1] = 0; size2 = LOGBUF_SIZE-1-size1; truncated = TRUE; } } // http://msdn.microsoft.com/en-us/library/aa365150%28VS.85%29.aspx: // "if your specified buffer size is too small, the system will grow the // buffer as needed, but the downside is that the operation will block // until the (existing) data is read from the pipe." // Existing pipe data should have produced a notification, but if the pipe // is left to fill without readout, we might run into blocking log calls. // TODO: address this potential issue if it is reported WriteFile(logger_wr_handle, buffer, (DWORD)(size1+size2+1), &junk, NULL); // Notify the destination window of a new log message log_messages_pending++; PostMessage(logger_dest, logger_msg, level, 0); if (truncated) { WriteFile(logger_wr_handle, truncation_notice, (DWORD)strlen(truncation_notice)+1, &junk, NULL); log_messages_pending++; PostMessage(logger_dest, logger_msg, (WPARAM)level, 0); } }
astring format(const char* fmt, va_list args) { char buf[DEFAULT_PRINTF_BUFFER]; safe_vsnprintf(buf, lengthof(buf), fmt, args); return astring(buf); }