Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.3 KiB

  1. //logger.cpp
  2. #include "globals.h"
  3. BOOL kLogFile::StripCommas(PTCHAR szString)
  4. {
  5. for (int i = 0; i <= (int)lstrlen(szString); i++)
  6. {
  7. if (szString[i] == ',')
  8. szString[i] = ' '; //Replace comma with a space to make SQL friendly string
  9. }
  10. return TRUE;
  11. }
  12. BOOL kLogFile::LogString(PTCHAR szString, ...)
  13. {
  14. HANDLE hFile;
  15. DWORD dwRet = 0;
  16. DWORD dwBytesWrit;
  17. PTCHAR szBuffer = (PTCHAR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PATH * 4);
  18. if(!szBuffer)
  19. return FALSE;
  20. va_list va;
  21. va_start(va,szString);
  22. vsprintf(szBuffer, szString, va);
  23. va_end(va);
  24. hFile = CreateFile(szFile, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  25. if (hFile != INVALID_HANDLE_VALUE)
  26. {
  27. SetFilePointer(hFile, 0, NULL, FILE_END);
  28. if(!WriteFile(hFile, szBuffer, lstrlen(szBuffer), &dwBytesWrit, NULL))
  29. {
  30. CloseHandle(hFile);
  31. HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, szBuffer);
  32. return FALSE;
  33. }
  34. if ((DWORD)lstrlen(szBuffer) != dwBytesWrit)
  35. LogString(",ERROR: String lengths differ,\r\n");
  36. }
  37. else
  38. {
  39. HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, szBuffer);
  40. CloseHandle(hFile);
  41. return FALSE;
  42. }
  43. CloseHandle(hFile);
  44. HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, szBuffer);
  45. return TRUE;
  46. }
  47. kLogFile::InitFile(PTCHAR szTempFile, PTCHAR szTempDir)
  48. {
  49. if (!szTempFile || !szTempDir)
  50. return FALSE;
  51. if ((lstrlen(szTempFile) <= 0) || (lstrlen(szTempDir) <= 0))
  52. return FALSE;
  53. szFile = (PTCHAR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlen(szTempFile) + 1) * sizeof(TCHAR));
  54. if(!szFile)
  55. return FALSE;
  56. szLogDir = (PTCHAR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (lstrlen(szTempDir) + 1) * sizeof(TCHAR));
  57. if(!szLogDir)
  58. {
  59. free(szFile);
  60. return FALSE;
  61. }
  62. lstrcpy(szFile, szTempFile);
  63. lstrcpy(szLogDir, szTempDir);
  64. return TRUE;
  65. }
  66. kLogFile::kLogFile()
  67. {
  68. szFile = NULL;
  69. szLogDir = NULL;
  70. }
  71. kLogFile::~kLogFile()
  72. {
  73. if(szFile)
  74. HeapFree (GetProcessHeap(), HEAP_ZERO_MEMORY, szFile);
  75. if(szLogDir)
  76. HeapFree (GetProcessHeap(), HEAP_ZERO_MEMORY, szLogDir);
  77. }