| 45 | } |
| 46 | |
| 47 | void DebugLog(const wchar_t *msg) { |
| 48 | if (!DebugMode) |
| 49 | return; |
| 50 | wchar_t temporaryPath[MAX_PATH]; |
| 51 | ::GetTempPathW(MAX_PATH, temporaryPath); |
| 52 | |
| 53 | std::wstring tempPath = std::wstring(temporaryPath) + L"insomnia.log"; |
| 54 | |
| 55 | HANDLE hDebugLog = |
| 56 | ::CreateFileW(tempPath.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, |
| 57 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); |
| 58 | if (hDebugLog == INVALID_HANDLE_VALUE) { |
| 59 | std::wcerr << msg << std::endl; |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | if (::GetLastError() == ERROR_FILE_NOT_FOUND) { |
| 64 | std::wcerr << L"File not found, creating new file." << std::endl; |
| 65 | // write the utf-16 BOM |
| 66 | const wchar_t bom[1] = {0xFEFF}; |
| 67 | ::WriteFile(hDebugLog, &bom, sizeof(bom), NULL, NULL); |
| 68 | } |
| 69 | |
| 70 | ::SetFilePointer(hDebugLog, 0, NULL, FILE_END); |
| 71 | |
| 72 | std::wstring finalMsg = L"[" + GetTimestamp() + L"] " + msg; |
| 73 | ::WriteFile(hDebugLog, finalMsg.c_str(), static_cast<DWORD>(finalMsg.length() * sizeof(wchar_t)), |
| 74 | NULL, NULL); |
| 75 | ::WriteFile(hDebugLog, L"\r\n", 2 * sizeof(wchar_t), NULL, NULL); |
| 76 | ::CloseHandle(hDebugLog); |
| 77 | } |
| 78 | |
| 79 | std::wstring ConvertLPSTRToWString(LPSTR lpstr) { |
| 80 | int size_needed = MultiByteToWideChar(CP_ACP, 0, lpstr, -1, NULL, 0); |
no test coverage detected