Win32ErrorLogMessage::~Win32ErrorLogMessage() { const int error_message_buffer_size = 256; char msgbuf[error_message_buffer_size]; DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; HMODULE hmod; if(module_) { hmod = GetModuleHandleA(module_); if(hmod) { flags |= FORMAT_MESSAGE_FROM_HMODULE; } else { // 导致Win32ErrorLogMessage嵌套, 由于module_是NULL不会再次进入这里, // 所以不会死循环. DPLOG(WARNING) << "Couldn't open module " << module_ << " for error message query"; } } else { hmod = NULL; } DWORD len = FormatMessageA(flags, hmod, err_, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msgbuf, sizeof(msgbuf)/sizeof(msgbuf[0]), NULL); if(len) { while((len>0) && isspace(static_cast<unsigned char>(msgbuf[len-1]))) { msgbuf[--len] = 0; } stream() << ": " << msgbuf; } else { stream() << ": Error " << GetLastError() << " while retrieving error " << err_; } }
void acquirePathLock() { string name = ( boost::filesystem::path( dbpath ) / "mongod.lock" ).string(); #ifdef _WIN32 lockFileHandle = CreateFileA( name.c_str(), GENERIC_READ | GENERIC_WRITE, 0 /* do not allow anyone else access */, NULL, OPEN_ALWAYS /* success if fh can open */, 0, NULL ); if (lockFileHandle == INVALID_HANDLE_VALUE) { DWORD code = GetLastError(); char *msg; FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL); string m = msg; str::stripTrailing(m, "\r\n"); uasserted( 13627 , str::stream() << "Unable to create/open lock file: " << name << ' ' << m << ". Is a mongod instance already running?" ); } lockFile = _open_osfhandle((intptr_t)lockFileHandle, 0); #else lockFile = open( name.c_str(), O_RDWR | O_CREAT , S_IRWXU | S_IRWXG | S_IRWXO ); if( lockFile <= 0 ) { uasserted( 10309 , str::stream() << "Unable to create/open lock file: " << name << ' ' << errnoWithDescription() << " Is a mongod instance already running?" ); } if (flock( lockFile, LOCK_EX | LOCK_NB ) != 0) { close ( lockFile ); lockFile = 0; uassert( 10310 , "Unable to lock file: " + name + ". Is a mongod instance already running?", 0 ); } #endif #ifdef _WIN32 uassert( 13625, "Unable to truncate lock file", _chsize(lockFile, 0) == 0); writePid( lockFile ); _commit( lockFile ); #else uassert( 13342, "Unable to truncate lock file", ftruncate(lockFile, 0) == 0); writePid( lockFile ); fsync( lockFile ); flushMyDirectory(name); #endif }
char *err_strA(DWORD dw) { static char msgstr[256]; LPVOID lpMsgBuf; FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &lpMsgBuf, 0, NULL ); // Display the error message and exit the process StringCchPrintfA(msgstr, ARRAYSIZE(msgstr), "%d: %s", dw, lpMsgBuf); return msgstr; }
const char* COMException::what() const { if(!msg) { LPSTR readable_hr = nullptr; FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, hr, 0, reinterpret_cast<LPSTR>(&readable_hr), 0, nullptr); if(readable_hr) { msg = FormatMessagef("%1!s! (%2!s!)", readable_hr, function); LocalFree(readable_hr); } else { msg = FormatMessagef("HR 0x%1!08x! (%2!s!)", hr, function); } } return msg ? msg : "COMException"; }
static std::string GetErrorMessage(DWORD error) { std::string message; LPVOID buffer = NULL; DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; DWORD lang_id = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT); DWORD result = FormatMessageA(flags, NULL, error, lang_id, reinterpret_cast<LPSTR>(&buffer), 0, NULL); if (result > 0) { message.assign(StripNewLine(reinterpret_cast<char*>(buffer))); } LocalFree(buffer); return message; }
const char* X_GetLastError() { #if defined WINDOWS || WIN32 char szBuf[256] = {0}; LPVOID lpMsgBuf; DWORD dw = GetLastError(); FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf, 0, NULL); return (const char*)lpMsgBuf; #else extern int errno; errno = 0; return dlerror(); #endif }
DLLEXPORT(char*) hresultString( HRESULT hr ) { static char msgbuf[256]; int len; sprintf( msgbuf, "(0x%lx) ", hr ); len = strlen(msgbuf); FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), msgbuf + len, 256, NULL ); return msgbuf; }
// Generic function to get last error message. // Call directly after the command or use the error num. // This function might change the error code. const char* GetLastErrorMsg() { static const size_t buff_size = 255; #ifdef _WIN32 static __declspec(thread) char err_str[buff_size] = {}; FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_str, buff_size, nullptr); #else static __thread char err_str[buff_size] = {}; // Thread safe (XSI-compliant) if (strerror_r(errno, err_str, buff_size)) return nullptr; #endif return err_str; }
void win_perror(const char *str) { DWORD dwError = GetLastError(); LPVOID lpvMessageBuffer; FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpvMessageBuffer, 0, NULL); //... now display this string fprintf(stderr, "%s: %s\n", str, lpvMessageBuffer); // Free the buffer allocated by the system LocalFree(lpvMessageBuffer); }
LIBCOUCHSTORE_API void couchstore_last_os_error(char* buf, size_t size) { struct _os_error *err = get_os_error_store(); #ifndef WINDOWS snprintf(buf, size, "errno = %d: `%s'", err->errno_err, strerror(err->errno_err)); #else char* win_msg = NULL; FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err->win_err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &win_msg, 0, NULL); snprintf(buf, size, "errno = %d: `%s', WINAPI error = %d: `%s'", err->errno_err, strerror(err->errno_err), err->win_err, win_msg); LocalFree(win_msg); #endif }
static void TestDceErrorInqText (void) { char bufferInvalid [1024]; char buffer [1024]; /* The required size is not documented but would * appear to be 256. */ DWORD dwCount; dwCount = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, RPC_S_NOT_RPC_ERROR, 0, bufferInvalid, sizeof(bufferInvalid)/sizeof(bufferInvalid[0]), NULL); /* A random sample of DceErrorInqText */ /* 0 is success */ ok ((DceErrorInqTextA (0, (unsigned char*)buffer) == RPC_S_OK), "DceErrorInqTextA(0...)\n"); /* A real RPC_S error */ ok ((DceErrorInqTextA (RPC_S_INVALID_STRING_UUID, (unsigned char*)buffer) == RPC_S_OK), "DceErrorInqTextA(valid...)\n"); if (dwCount) { /* A message for which FormatMessage should fail * which should return RPC_S_OK and the * fixed "not valid" message */ ok ((DceErrorInqTextA (35, (unsigned char*)buffer) == RPC_S_OK && strcmp (buffer, bufferInvalid) == 0), "DceErrorInqTextA(unformattable...)\n"); /* One for which FormatMessage should succeed but * DceErrorInqText should "fail" * 3814 is generally quite a long message */ ok ((DceErrorInqTextA (3814, (unsigned char*)buffer) == RPC_S_OK && strcmp (buffer, bufferInvalid) == 0), "DceErrorInqTextA(deviation...)\n"); } else ok (0, "Cannot set up for DceErrorInqText\n"); }
last_system_error::last_system_error(const std::string &func) : base_type("") { LPSTR systemError = NULL; if (!FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, (LPSTR)&systemError, 0, NULL)) { if (systemError) LocalFree(systemError); static const size_t bufLen = 256; systemError = (char *)LocalAlloc(0, bufLen); sprintf_s(systemError, bufLen, "Format Message failed with error: 0x%x", GetLastError()); } m_what = func + ": " + systemError; if (systemError) LocalFree(systemError); }
MEDUSA_NAMESPACE_BEGIN std::string Exception_System::ErrorCodeToMessage(void) { HLOCAL hLocal = nullptr; if (FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, nullptr, GetLastError(), 0x0, (LPSTR)&hLocal, 0x0, nullptr ) == 0x0) return "Unable to retrieve the exception message"; std::string Res = (LPCSTR)LocalLock(hLocal); LocalFree(hLocal); return Res; }
std::string GetLastErrorString() { LPSTR lpBuffer = NULL; std::string strError; if(FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &lpBuffer, 0, NULL) != 0) { strError = lpBuffer; LocalFree( lpBuffer ); } else { std::ostringstream buffer; //! Buffer for the log message excluding level, file and line. buffer << "Unknown error (" << GetLastError() << ")."; strError = buffer.str(); } return strError; }
void ipk_perror_win(unsigned long error) { if (error == 0) return; char* errmsg = NULL; unsigned long hr = FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0U, error, LANG_NEUTRAL, (char *)&errmsg, 0U, NULL); assert(hr > 0); if (errmsg) { _ipk_error_log("Chyba #%d: %s", error, errmsg); } else { _ipk_error_log("Chyba #%d", error); } LocalFree(errmsg); }
static VOID set_krberror(DWORD errCode, const SEC_CHAR* msg) { SEC_CHAR* err; DWORD status; DWORD flags = (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS); status = FormatMessageA(flags, NULL, errCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&err, 0, NULL); if (status) { PyErr_Format(KrbError, "SSPI: %s: %s", msg, err); LocalFree(err); } else { PyErr_Format(KrbError, "SSPI: %s", msg); } }
void Library::getLastErrorMessage( std::string& message ) { #ifdef CORAL_OS_WIN LPVOID lpMsgBuf; FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL ); message = (char*)lpMsgBuf; LocalFree( lpMsgBuf ); #else const char* errorMsg = dlerror(); message = ( errorMsg == NULL ? "" : errorMsg ); #endif }
char* tr_net_strerror(char* buf, size_t buflen, int err) { *buf = '\0'; #ifdef _WIN32 DWORD len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, buflen, NULL); while (len > 0 && buf[len - 1] >= '\0' && buf[len - 1] <= ' ') { buf[--len] = '\0'; } #else tr_strlcpy(buf, tr_strerror(err), buflen); #endif return buf; }
char *win32_strerror(int inErrorCode) { static char buffer[1024]; DWORD n; memset(buffer, 0, sizeof(buffer)); n = FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD) inErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, sizeof(buffer), NULL); if (n > 0) { // Remove any trailing CR's or LF's since some messages have them. while ((n > 0) && isspace(((unsigned char *) buffer)[n - 1])) buffer[--n] = '\0'; } return buffer; }
void _mongoc_sspi_set_gsserror (DWORD errCode, const SEC_CHAR *msg) { SEC_CHAR *err; DWORD status; DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; status = FormatMessageA (flags, NULL, errCode, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &err, 0, NULL); if (status) { MONGOC_ERROR ("SSPI: %s: %s", msg, err); LocalFree (err); } else { MONGOC_ERROR ("SSPI: %s", msg); } }
CString CServerSocet::MyGetLastError() { DWORD dError; dError = GetLastError(); void* cstr; FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPSTR) &cstr, 0, NULL ); CString res((char*)cstr); res = ReplaceLeftSymbols(res, TRUE); LocalFree(cstr); CString sRet; sRet.Format(_T("ERROR\t%d %s\n"),dError,res); return sRet; }
const char* CLibrary::GetLoadErrorDescription() { static char szBuffer[ 512 ]; #ifdef WIN32 if( FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, GetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ), szBuffer, sizeof( szBuffer ), nullptr ) == 0 ) { strcpy( szBuffer, "Unknown error" ); } #else strncpy( szBuffer, dlerror(), sizeof( szBuffer ) ); szBuffer[ sizeof( szBuffer ) - 1 ] = '\0'; #endif size_t uiLength = strlen( szBuffer ); //Strip any newlines after the message. if( uiLength > 0 && szBuffer[ uiLength - 1 ] == '\n' ) { szBuffer[ --uiLength ] = '\0'; } //carriage returns too (Windows). if( uiLength > 0 && szBuffer[ uiLength - 1 ] == '\r' ) { szBuffer[ --uiLength ] = '\0'; } return szBuffer; }
void CLog::Log(LOG_LEVEL level, const char *file, int line, int error, char *fmt, ...) { if(m_pLog == NULL) return; // no log file specified if(level > m_level) return; // too detail // format time char strtime[128]; time_t t = time(NULL); struct tm *tt = gmtime(&t); snprintf(strtime, sizeof(strtime)-1, "%d-%d-%d %d:%d:%d", 1900+tt->tm_year, tt->tm_mon, tt->tm_yday, tt->tm_hour, tt->tm_min, tt->tm_sec); // format message char buf[64*1024]; // max error message size in Windows va_list maker; va_start(maker, fmt); vsnprintf(buf, sizeof(buf)-1, fmt, maker); va_end(maker); fprintf(m_pLog, "[%s][%s][%s][line:%d]%s[errno=%d]", strtime, GetLevelString(level), file, line, buf, error); // format error strings buf[0] = '\0'; if(error != 0) { #ifdef WIN32 // Windows FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, DWORD(error), 0, buf, sizeof(buf)-1, NULL); fprintf(m_pLog, "%s.\n", buf); #else // Linux fprintf(m_pLog, "%s.\n", strerror(error)); #endif } else { fprintf(m_pLog, ".\n"); } // flush file for error message if(level == LL_ERROR) { fflush(m_pLog); } }
static void dlfailure(void) { #ifdef _WIN32 LPSTR msg; DWORD dwBufferLength; DWORD dwLastError = GetLastError(); DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; if ((dwBufferLength = FormatMessageA(dwFormatFlags, NULL, dwLastError, 0, (LPSTR) &msg, 0, NULL))) { msg[dwBufferLength] = '\0'; fprintf(stderr, "Error: %s", msg); LocalFree(msg); } #else fprintf(stderr, "Error: %s", dlerror()); #endif /*_WIN32*/ exit(2); }
std::string System_error::message () const { std::string mesg(action); if (!target.empty()) { mesg += ": "; mesg += target; } if (error) { LPTSTR error_message; FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&error_message), 0, NULL); mesg += error_message; LocalFree(error_message); } return mesg; }
static void assert_result(HRESULT hres, JNIEnv *env) { #ifdef _DEBUG if (hres != 0) { DWORD lastError = GetLastError(); if (lastError != 0) { LPSTR msgBuffer = NULL; FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msgBuffer, // it's an output parameter when allocate buffer is used 0, NULL); DTRACE_PRINTLN3("Error: hres=0x%x lastError=0x%x %s\n", hres, lastError, msgBuffer); } } #endif }
void PrintError( int error ) { LPSTR errorText = NULL; FormatMessageA( // use system message tables to retrieve error text FORMAT_MESSAGE_FROM_SYSTEM // allocate buffer on local heap for error text |FORMAT_MESSAGE_ALLOCATE_BUFFER // Important! will fail otherwise, since we're not // (and CANNOT) pass insertion parameters |FORMAT_MESSAGE_IGNORE_INSERTS, NULL, // unused with FORMAT_MESSAGE_FROM_SYSTEM error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&errorText, // output 0, // minimum size for output buffer NULL); // arguments - see note if (errorText != NULL) { LOG(ERR,"Error: [%d] %s", error, errorText); } }
void glwtWin32Error(const char *msg) { LPVOID msgbuf = 0; DWORD err = GetLastError(); if(!FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, (LPSTR) &msgbuf, 0, NULL )) { glwtErrorPrintf("%s: %d\n", msg, err); return; } glwtErrorPrintf("%s: %s\n", msg, (const char *)msgbuf); LocalFree(msgbuf); }
const char * _p11_module_error (void) { DWORD code = GetLastError(); p11_local *local; LPVOID msg_buf; local = _p11_library_get_thread_local (); FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, code, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg_buf, 0, NULL); if (local->last_error) LocalFree (local->last_error); local->last_error = msg_buf; return msg_buf; }
/* Displays an error and doesn't die. */ void nberror(char *str) { int error = getlasterror(); #ifdef WIN32 char error_str[1024]; FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, error_str, 1024, NULL); if(str) fprintf(stderr, "%s\n", str); fprintf(stderr, "Error %d: %s", error, error_str); #else char *error_str = strerror(error); if(str) fprintf(stderr, "%s (error %d: %s)\n", str, error, error_str); else fprintf(stderr, "Error %d: %s\n", error, error_str); #endif }