コミットメタ情報

リビジョン78405f449bc7145d758536115f9bddb9e4402e3d (tree)
日時2014-09-11 22:22:27
作者Kazuhiro Fujieda <fujieda@user...>
コミッターKazuhiro Fujieda

ログメッセージ

Make CUtils::Log thread safe

変更サマリ

差分

--- a/xkeymacsdll/Utils.cpp
+++ b/xkeymacsdll/Utils.cpp
@@ -417,26 +417,29 @@ void CUtils::Log(LPCTSTR fmt, ...)
417417 {
418418 va_list ap;
419419 va_start(ap, fmt);
420- TCHAR log[1024];
421- _set_invalid_parameter_handler(invalid_parameter_handler);
422- if (_vstprintf_s(log, fmt, ap) < 0)
423- _tcscpy_s(log, _T("invalid format"));
424-
420+ TCHAR log[1024], msg[1024];
425421 static int n = 0;
422+ _set_invalid_parameter_handler(invalid_parameter_handler);
423+ if (_vstprintf_s(msg, fmt, ap) < 0)
424+ _tcscpy_s(msg, _T("invalid format"));
425+ va_end(ap);
426+ _stprintf_s(log, _T("%4d %8x: %s\t%s\r\n"), n++, GetCurrentThreadId(), AppName::GetAppName(), msg);
426427 TCHAR path[MAX_PATH];
427428 if (GetTempPath(MAX_PATH, path)) {
428429 #ifndef _WIN64
429- _tmakepath_s(path, NULL, path, _T("xkeylog"), _T("txt"));
430+ _tmakepath_s(path, nullptr, path, _T("xkeylog"), _T("txt"));
430431 #else
431- _tmakepath_s(path, NULL, path, _T("xkeylog64"), _T("txt"));
432+ _tmakepath_s(path, nullptr, path, _T("xkeylog64"), _T("txt"));
432433 #endif
433434 } else
434435 _tcscpy_s(path, _T("c:\\xkeylog.txt"));
435-
436- FILE *fp;
437- _tfopen_s(&fp, path, _T("a"));
438- _ftprintf(fp, _T("%8d: %s\t%s\n"), n++, AppName::GetAppName(), log);
439- fclose(fp);
436+ HANDLE fh = CreateFile(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
437+ if (fh == INVALID_HANDLE_VALUE)
438+ return;
439+ SetFilePointer(fh, 0, nullptr, FILE_END);
440+ DWORD written;
441+ WriteFile(fh, log, static_cast<DWORD>(strlen(log)), &written, nullptr);
442+ CloseHandle(fh);
440443 }
441444
442445 BOOL CUtils::IsSh()
旧リポジトリブラウザで表示