[Box Backup-commit] COMMIT r1400 - box/chris/merge/lib/win32

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Sat Mar 10 17:27:31 GMT 2007


Author: chris
Date: 2007-03-10 17:27:31 +0000 (Sat, 10 Mar 2007)
New Revision: 1400

Modified:
   box/chris/merge/lib/win32/emu.cpp
Log:
Print localised error messages rather than error codes for all errors
(refs #3, merges [1046])


Modified: box/chris/merge/lib/win32/emu.cpp
===================================================================
--- box/chris/merge/lib/win32/emu.cpp	2007-03-10 17:20:40 UTC (rev 1399)
+++ box/chris/merge/lib/win32/emu.cpp	2007-03-10 17:27:31 UTC (rev 1400)
@@ -482,6 +482,32 @@
 	return tmpStr;
 }
 
+std::string GetErrorMessage(DWORD errorCode)
+{
+	char* pMsgBuf = NULL;
+	
+	DWORD chars = FormatMessage
+	(
+		FORMAT_MESSAGE_ALLOCATE_BUFFER | 
+		FORMAT_MESSAGE_FROM_SYSTEM,
+		NULL,
+		errorCode,
+		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+		(char *)(&pMsgBuf),
+		0, NULL
+	);
+
+	if (chars == 0 || pMsgBuf == NULL)
+	{
+		return std::string("failed to get error message");
+	}
+
+	std::string out(pMsgBuf);
+	LocalFree(pMsgBuf);
+
+	return out;
+}
+
 // --------------------------------------------------------------------------
 //
 // Function
@@ -560,8 +586,9 @@
 
 	if (hdir == INVALID_HANDLE_VALUE)
 	{
-		::syslog(LOG_WARNING, "Failed to open file %s: "
-			"error %i", pFileName, GetLastError());
+		::syslog(LOG_WARNING, "Failed to open file '%s': "
+			"%s", pFileName, 
+			GetErrorMessage(GetLastError()).c_str());
 		return INVALID_HANDLE_VALUE;
 	}
 
@@ -589,7 +616,7 @@
 	if (!GetFileInformationByHandle(hdir, &fi))
 	{
 		::syslog(LOG_WARNING, "Failed to read file information: "
-			"error %d", GetLastError());
+			"%s", GetErrorMessage(GetLastError()).c_str());
 		errno = EACCES;
 		return -1;
 	}
@@ -597,7 +624,7 @@
 	if (INVALID_FILE_ATTRIBUTES == fi.dwFileAttributes)
 	{
 		::syslog(LOG_WARNING, "Failed to get file attributes: "
-			"error %d", GetLastError());
+			"%s", GetErrorMessage(GetLastError()).c_str());
 		errno = EACCES;
 		return -1;
 	}
@@ -628,7 +655,7 @@
 		if (!GetFileSizeEx(hdir, &st_size))
 		{
 			::syslog(LOG_WARNING, "Failed to get file size: "
-				"error %d", GetLastError());
+				"%s", GetErrorMessage(GetLastError()).c_str());
 			errno = EACCES;
 			return -1;
 		}
@@ -751,8 +778,9 @@
 		}
 		else
 		{
-			::syslog(LOG_WARNING, 
-				"Failed to open '%s': error %d", pFileName, err);
+			::syslog(LOG_WARNING, "Failed to open '%s': "
+				"%s", pFileName, 
+				GetErrorMessage(err).c_str());
 			errno = EACCES;
 		}
 
@@ -820,7 +848,8 @@
 	if (!GetFileInformationByHandle(handle, &fi))
 	{
 		::syslog(LOG_WARNING, "Failed to get file information "
-			"for '%s': error %d", pName, GetLastError());
+			"for '%s': %s", pName,
+			GetErrorMessage(GetLastError()).c_str());
 		CloseHandle(handle);
 		errno = EACCES;
 		return -1;
@@ -873,8 +902,8 @@
 
 	if (!SetFileTime(handle, &creationTime, NULL, &modificationTime))
 	{
-		::syslog(LOG_ERR, "Failed to set times on '%s': error %d",
-			pName, GetLastError());
+		::syslog(LOG_ERR, "Failed to set times on '%s': %s", pName,
+			GetErrorMessage(GetLastError()).c_str());
 		CloseHandle(handle);
 		return 1;
 	}
@@ -916,8 +945,8 @@
 	DWORD attribs = GetFileAttributesW(pBuffer);
 	if (attribs == INVALID_FILE_ATTRIBUTES)
 	{
-		::syslog(LOG_ERR, "Failed to get file attributes of '%s': "
-			"error %d", pName, GetLastError());
+		::syslog(LOG_ERR, "Failed to get file attributes of '%s': %s",
+			pName, GetErrorMessage(GetLastError()).c_str());
 		errno = EACCES;
 		free(pBuffer);
 		return -1;
@@ -934,8 +963,8 @@
 
 	if (!SetFileAttributesW(pBuffer, attribs))
 	{
-		::syslog(LOG_ERR, "Failed to set file attributes of '%s': "
-			"error %d", pName, GetLastError());
+		::syslog(LOG_ERR, "Failed to set file attributes of '%s': %s",
+			pName, GetErrorMessage(GetLastError()).c_str());
 		errno = EACCES;
 		free(pBuffer);
 		return -1;
@@ -1204,8 +1233,8 @@
 	char cmd[MAX_PATH];
 	if (GetModuleFileName(NULL, cmd, sizeof(cmd)-1) == 0)
 	{
-		::syslog(LOG_ERR, "Failed to get the program file name: "
-			"error %d", GetLastError());
+		::syslog(LOG_ERR, "Failed to get the program file name: %s",
+			GetErrorMessage(GetLastError()).c_str());
 		return FALSE;
 	}
 	cmd[sizeof(cmd)-1] = 0;
@@ -1224,8 +1253,8 @@
 			 0, NULL, REG_OPTION_NON_VOLATILE,
 			 KEY_WRITE, NULL, &hk, &dwDisp)) 
 	{
-		::syslog(LOG_ERR, "Failed to create the registry key: "
-			"error %d", GetLastError()); 
+		::syslog(LOG_ERR, "Failed to create the registry key: %s",
+			GetErrorMessage(GetLastError()).c_str());
 		return FALSE;
 	}
 
@@ -1238,8 +1267,8 @@
 			 (LPBYTE) exepath.c_str(),  // pointer to value data 
 			 (DWORD) (exepath.size()))) // data size
 	{
-		::syslog(LOG_ERR, "Failed to set the event message file: "
-			"error %d", GetLastError()); 
+		::syslog(LOG_ERR, "Failed to set the event message file: %s",
+			GetErrorMessage(GetLastError()).c_str());
 		RegCloseKey(hk); 
 		return FALSE;
 	}
@@ -1256,8 +1285,8 @@
 			  (LPBYTE) &dwData, // pointer to value data 
 			  sizeof(DWORD)))   // length of value data 
 	{
-		::syslog(LOG_ERR, "Failed to set the supported types: "
-			"error %d", GetLastError()); 
+		::syslog(LOG_ERR, "Failed to set the supported types: %s",
+			GetErrorMessage(GetLastError()).c_str());
 		RegCloseKey(hk); 
 		return FALSE;
 	}
@@ -1272,7 +1301,7 @@
 			  (DWORD) (exepath.size()))) // data size
 	{
 		::syslog(LOG_ERR, "Failed to set the category message file: "
-			"error %d", GetLastError());
+			"%s", GetErrorMessage(GetLastError()).c_str());
 		RegCloseKey(hk); 
 		return FALSE;
 	}
@@ -1284,8 +1313,8 @@
 			  (LPBYTE) &dwNum, // pointer to value data 
 			  sizeof(DWORD)))  // length of value data 
 	{
-		::syslog(LOG_ERR, "Failed to set the category count: "
-			"error %d", GetLastError());
+		::syslog(LOG_ERR, "Failed to set the category count: %s",
+			GetErrorMessage(GetLastError()).c_str());
 		RegCloseKey(hk); 
 		return FALSE;
 	}
@@ -1318,7 +1347,7 @@
 	if (newSyslogH == NULL)
 	{
 		::syslog(LOG_ERR, "Failed to register our own event source: "
-			"error %d", GetLastError());
+			"%s", GetErrorMessage(GetLastError()).c_str());
 		return;
 	}
 
@@ -1417,8 +1446,8 @@
 		}
 		else
 		{
-			printf("Unable to send message to Event Log: "
-				"error %i:\r\n", (int)err);
+			printf("Unable to send message to Event Log: %s:\r\n",
+				GetErrorMessage(err).c_str());
 			fflush(stdout);
 		}
 	}
@@ -1562,7 +1591,8 @@
 		else
 		{
 			::syslog(LOG_WARNING, "Failed to delete file "
-				"'%s': error %d", pFileName, (int)err);
+				"'%s': %s", pFileName, 
+				GetErrorMessage(err).c_str());
 			errno = ENOSYS;
 		}
 		return -1;
@@ -1578,7 +1608,7 @@
 	if (hConsole == INVALID_HANDLE_VALUE)
 	{
 		::fprintf(stderr, "Failed to get a handle on standard input: "
-			"error %d\n", GetLastError());
+			"%s", GetErrorMessage(GetLastError()).c_str());
 		return -1;
 	}
 
@@ -1601,8 +1631,8 @@
 			NULL // reserved
 		)) 
 	{
-		::fprintf(stderr, "Failed to read from console: error %d\n",
-			GetLastError());
+		::fprintf(stderr, "Failed to read from console: %s\n",
+			GetErrorMessage(GetLastError()).c_str());
 		return -1;
 	}
 
@@ -1698,13 +1728,13 @@
 	// Convert the last-write time to local time.
 	if (!SystemTimeToFileTime(&stUTC, pTo))
 	{
-		syslog(LOG_ERR, "Failed to convert between time formats: "
-			"error %d", GetLastError());
+		syslog(LOG_ERR, "Failed to convert between time formats: %s",
+			GetErrorMessage(GetLastError()).c_str());
 		return false;
 	}
 
 	return true;
 }
 
+#endif // WIN32
 
-#endif // WIN32




More information about the Boxbackup-commit mailing list