static int _outputvws_safe(wchar_t* buf, int cnt, const wchar_t* fmt, va_list ap) { int ret; #if defined(_MSC_VER) if (NULL == buf || 0 == cnt) return _vscwprintf(fmt, ap); #if _MSC_VER >= 1500 ret = _vsnwprintf_s(buf, cnt, _TRUNCATE, fmt, ap); #else ret = _vsnwprintf(buf, cnt, fmt, ap); #endif #else ret = vsnwprintf(buf, cnt, fmt, ap); #endif if (NULL != buf && cnt > 0){ if (ret < 0) buf[cnt-1] = 0; else if (ret >= cnt) ret = -1; } return ret; }
void printMessage(LogLevel level, const wchar_t* format, ...) { wchar_t buffer[1024]; va_list vl; va_start(vl, format); int result = vsnwprintf(buffer, cpp::length(buffer) - 1, format, vl); va_end(vl); if (result < 0) { // TODO: Error } static const wchar_t* prefix[LL_MAX] = { L"--- MESSAGE: ", // LL_MESSAGE L"??? WARNING: ", // LL_WARNING L"!!! CRITIAL: ", // LL_CRITICAL L"!!! ERROR: ", // LL_ERROR }; OutputDebugStringW(prefix[level]); OutputDebugStringW(buffer); switch (level) { case LL_CRITICAL: // MessageBoxW(NULL, buffer, L"critical error", MB_OK | MB_ICONWARNING); break; case LL_ERROR: MessageBoxW(NULL, buffer, L"Fatal error", MB_OK | MB_ICONERROR); ExitProcess(1); default:; } }
wchar_t *vwcsdupf(const wchar_t *format, va_list l) { size_t len; wchar_t *s; va_list temp; #if !defined(WIN32) && !defined(WIN64) size_t temp_len; #endif #if defined(WIN32) || defined(WIN64) va_copy(temp, l); len = _vscwprintf(format, l) + 1; va_end(temp); s = (wchar_t*)malloc(len * sizeof(wchar_t)); vsnwprintf(s, len, format, l); #else // TODO: faster len = BUFSIZE; s = NULL; do { len += BUFSIZE; if (realloc_free((void**)&s, len*sizeof(wchar_t)) != PALO_SUCCESS) { va_end(l); return NULL; } va_copy(temp, l); temp_len = vswprintf(s, len, format, temp); va_end(temp); } while (temp_len == -1); s = (wchar_t*)realloc((void*)s, (temp_len + 1) * sizeof(wchar_t)); assert(s != NULL); #endif return s; }
// Unicode appSprintf int appSprintf(wchar_t *dest, int size, const wchar_t *fmt, ...) { va_list argptr; va_start(argptr, fmt); int len = vsnwprintf(dest, size, fmt, argptr); va_end(argptr); if (len < 0 || len >= size - 1) appPrintf("appSprintf: overflow of size %d (fmt=%S)\n", size, fmt); return len; }
void CLogger::WriteFormated(const wchar_t* zFormat,...) { ParaEngine::Lock lock_(m_mutex); va_list args; va_start(args, zFormat); int nSize = vsnwprintf((wchar_t*)m_buffer, MAX_DEBUG_STRING_LENGTH, zFormat, args); if(nSize < 0 || nSize>=MAX_DEBUG_STRING_LENGTH) { m_buffer[MAX_DEBUG_STRING_LENGTH-1] = '\0'; } va_end(args); }
void print(const wchar_t* format, ...) { wchar_t buffer[1024]; va_list vl; static std::size_t line = 0; line++; snwprintf(buffer, 5, L"%03d:", line); va_start(vl, format); vsnwprintf(buffer + 5, 1023 - 5, format, vl); va_end(vl); buffer[4] = L' '; buffer[1023] = L'\0'; OutputDebugStringW(buffer); }
wstring str_format(const wchar_t* fmt, ...) { int size = 100; wstring str; va_list ap; while (1) { str.resize(size); va_start(ap, fmt); int n = vsnwprintf((wchar_t *)str.c_str(), size, fmt, ap); va_end(ap); if (n > -1 && n < size) { str.resize(n); return str; } if (n > -1) size = n + 1; else size *= 2; } return str; }
CpixExc::CpixExc(const wchar_t * wWhat, const char * file, size_t line, ...) : wWhat_(EMPTY_MSG), file_(file), line_(line) { if (wWhat != NULL) { wWhat_ = LFAILED_CTOR_MSG; wchar_t tmpBuf[Cpix::EXCMSG_BUFSIZE]; va_list args; va_start(args, line); int result = vsnwprintf(tmpBuf, sizeof(tmpBuf)/sizeof(wchar_t), wWhat, args); va_end(args); if (result > 0) { wWhat_ = wcsdup(tmpBuf); if (wWhat_ == NULL) { wWhat_ = LFAILED_CTOR_MSG; } } } }