Revision: 9873 https://osdn.net/projects/ttssh2/scm/svn/commits/9873 Author: zmatsuo Date: 2022-04-21 22:36:16 +0900 (Thu, 21 Apr 2022) Log Message: ----------- ファイル内グローバル変数の使用を減らした Modified Paths: -------------- trunk/teraterm/teraterm/filesys_log.cpp -------------- next part -------------- Modified: trunk/teraterm/teraterm/filesys_log.cpp =================================================================== --- trunk/teraterm/teraterm/filesys_log.cpp 2022-04-21 13:36:07 UTC (rev 9872) +++ trunk/teraterm/teraterm/filesys_log.cpp 2022-04-21 13:36:16 UTC (rev 9873) @@ -98,12 +98,9 @@ BOOL FileLog; BOOL BinLog; -} TFileVar_; -typedef TFileVar_ *PFileVar_; +} TFileVar; +typedef TFileVar *PFileVar; -#define PFileVar PFileVar_ -#define TFileVar TFileVar_ - static PFileVar LogVar = NULL; static PCHAR cv_LogBuf; @@ -115,7 +112,6 @@ // \x92x\x89\x84\x8F\x91\x82\xAB\x8D\x9E\x82ݗp\x83X\x83\x8C\x83b\x83h\x82̃\x81\x83b\x83Z\x81[\x83W #define WM_DPC_LOGTHREAD_SEND (WM_APP + 1) -static void FileTransEnd_(void); static void Log1Bin(BYTE b); static void LogBinSkip(int add); static BOOL CreateLogBuf(void); @@ -122,8 +118,8 @@ static BOOL CreateBinBuf(void); void LogPut1(BYTE b); static void OutputStr(const wchar_t *str); -static void LogToFile(void); -static void FLogOutputBOM(void); +static void LogToFile(PFileVar fv); +static void FLogOutputBOM(PFileVar fv); static BOOL OpenFTDlg_(PFileVar fv) { @@ -228,29 +224,29 @@ // \x83X\x83\x8C\x83b\x83h\x82̏I\x97\xB9\x82ƃt\x83@\x83C\x83\x8B\x82̃N\x83\x8D\x81[\x83Y -static void CloseFileSync(PFileVar ptr) +static void CloseFileSync(PFileVar fv) { BOOL ret; - if (ptr->FileHandle == INVALID_HANDLE_VALUE) { + if (fv->FileHandle == INVALID_HANDLE_VALUE) { return; } - if (ptr->LogThread != INVALID_HANDLE_VALUE) { + if (fv->LogThread != INVALID_HANDLE_VALUE) { // \x83X\x83\x8C\x83b\x83h\x82̏I\x97\xB9\x91҂\xBF - ret = PostThreadMessage(ptr->LogThreadId, WM_QUIT, 0, 0); + ret = PostThreadMessage(fv->LogThreadId, WM_QUIT, 0, 0); if (ret != 0) { // \x83X\x83\x8C\x83b\x83h\x83L\x83\x85\x81[\x82ɃG\x83\x93\x83L\x83\x85\x81[\x82ł\xAB\x82\xBD\x8Fꍇ\x82̂ݑ҂\xBF\x8D\x87\x82킹\x82\xF0\x8Ds\x82\xA4\x81B - WaitForSingleObject(ptr->LogThread, INFINITE); + WaitForSingleObject(fv->LogThread, INFINITE); } else { //DWORD code = GetLastError(); } - CloseHandle(ptr->LogThread); - ptr->LogThread = INVALID_HANDLE_VALUE; + CloseHandle(fv->LogThread); + fv->LogThread = INVALID_HANDLE_VALUE; } - CloseHandle(ptr->FileHandle); - ptr->FileHandle = INVALID_HANDLE_VALUE; + CloseHandle(fv->FileHandle); + fv->FileHandle = INVALID_HANDLE_VALUE; } // \x92x\x89\x84\x8F\x91\x82\xAB\x8D\x9E\x82ݗp\x83X\x83\x8C\x83b\x83h @@ -274,7 +270,7 @@ case WM_DPC_LOGTHREAD_SEND: buf = (PCHAR)msg.wParam; buflen = (DWORD)msg.lParam; - WriteFile(LogVar->FileHandle, buf, buflen, &wrote, NULL); + WriteFile(fv->FileHandle, buf, buflen, &wrote, NULL); free(buf); // \x82\xB1\x82\xB1\x82Ń\x81\x83\x82\x83\x8A\x89\xF0\x95\xFA break; @@ -692,14 +688,12 @@ if (!ts.LogLockExclusive) { dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; } - LogVar->FileHandle = CreateFileW(fv->FullName, GENERIC_WRITE, dwShareMode, NULL, - OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + fv->FileHandle = CreateFileW(fv->FullName, GENERIC_WRITE, dwShareMode, NULL, + OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); } -static BOOL LogStart(const wchar_t *fname) +static BOOL LogStart(PFileVar fv, const wchar_t *fname) { - PFileVar fv = LogVar; - fv->FullName = _wcsdup(fname); FixLogOption(); @@ -743,7 +737,7 @@ // BOM\x8Fo\x97\xCD if (ts.Append == 0 && ts.LogBinary == 0 && fv->bom) { // \x92NjL\x82ł͂Ȃ\xA2(\x90V\x8BK) && \x83o\x83C\x83i\x83\x8A\x82ł͂Ȃ\xA2 && BOM \x82\xF0\x8Fo\x97͎\x9E - FLogOutputBOM(); + FLogOutputBOM(fv); } // Log rotate configuration @@ -796,6 +790,7 @@ */ void FLogOutputAllBuffer(void) { + PFileVar fv = LogVar; DWORD ofs; int size; wchar_t buf[512]; @@ -807,7 +802,7 @@ OutputStr(buf); OutputStr(L"\r\n"); - LogToFile(); + LogToFile(fv); } } @@ -871,18 +866,18 @@ // \x83\x8D\x83O\x82\xF0\x83\x8D\x81[\x83e\x81[\x83g\x82\xB7\x82\xE9\x81B // (2013.3.21 yutaka) -static void LogRotate(void) +static void LogRotate(PFileVar fv) { int loopmax = 10000; // XXX int i, k; - if (LogVar->RotateMode == ROTATE_NONE) + if (fv->RotateMode == ROTATE_NONE) return; - if (LogVar->RotateMode == ROTATE_SIZE) { - if (LogVar->ByteCount <= LogVar->RotateSize) + if (fv->RotateMode == ROTATE_SIZE) { + if (fv->ByteCount <= fv->RotateSize) return; - //OutputDebugPrintf("%s: mode %d size %ld\n", __FUNCTION__, LogVar->RotateMode, LogVar->ByteCount); + //OutputDebugPrintf("%s: mode %d size %ld\n", __FUNCTION__, fv->RotateMode, fv->ByteCount); } else { return; } @@ -889,18 +884,18 @@ logfile_lock(); // \x83\x8D\x83O\x83T\x83C\x83Y\x82\xF0\x8Dď\x89\x8A\x{227B0B7}\x82\xE9\x81B - LogVar->ByteCount = 0; + fv->ByteCount = 0; // \x82\xA2\x82\xC1\x82\xBD\x82̃t\x83@\x83C\x83\x8B\x82\xF0\x83N\x83\x8D\x81[\x83Y\x82\xB5\x82āA\x95ʖ\xBC\x82̃t\x83@\x83C\x83\x8B\x82\xF0\x83I\x81[\x83v\x83\x93\x82\xB7\x82\xE9\x81B - CloseFileSync(LogVar); + CloseFileSync(fv); // \x90\xA2\x91ネ\x81[\x83e\x81[\x83V\x83\x87\x83\x93\x82̃X\x83e\x83b\x83v\x90\x94\x82̎w\x92肪\x82\xA0\x82邩 - if (LogVar->RotateStep > 0) - loopmax = LogVar->RotateStep; + if (fv->RotateStep > 0) + loopmax = fv->RotateStep; for (i = 1 ; i <= loopmax ; i++) { wchar_t *filename; - aswprintf(&filename, L"%s.%d", LogVar->FullName, i); + aswprintf(&filename, L"%s.%d", fv->FullName, i); DWORD attr = GetFileAttributesW(filename); free(filename); if (attr == INVALID_FILE_ATTRIBUTES) @@ -915,11 +910,11 @@ for (k = i-1 ; k >= 0 ; k--) { wchar_t *oldfile; if (k == 0) - oldfile = _wcsdup(LogVar->FullName); + oldfile = _wcsdup(fv->FullName); else - aswprintf(&oldfile, L"%s.%d", LogVar->FullName, k); + aswprintf(&oldfile, L"%s.%d", fv->FullName, k); wchar_t *newfile; - aswprintf(&newfile, L"%s.%d", LogVar->FullName, k+1); + aswprintf(&newfile, L"%s.%d", fv->FullName, k+1); DeleteFileW(newfile); if (MoveFileW(oldfile, newfile) == 0) { OutputDebugPrintf("%s: rename %d\n", __FUNCTION__, errno); @@ -929,18 +924,18 @@ } // \x8DăI\x81[\x83v\x83\x93 - OpenLogFile(LogVar); - if (LogVar->bom) { - FLogOutputBOM(); + OpenLogFile(fv); + if (fv->bom) { + FLogOutputBOM(fv); } if (ts.DeferredLogWriteMode) { - StartThread(LogVar); + StartThread(fv); } logfile_unlock(); } -static wchar_t *TimeStampStr() +static wchar_t *TimeStampStr(PFileVar fv) { char *strtime = NULL; switch (ts.LogTimestampType) { @@ -952,7 +947,7 @@ strtime = mctimelocal(ts.LogTimestampFormat, TRUE); break; case TIMESTAMP_ELAPSED_LOGSTART: - strtime = strelapsed(LogVar->StartTime); + strtime = strelapsed(fv->StartTime); break; case TIMESTAMP_ELAPSED_CONNECTED: strtime = strelapsed(cv.ConnectedTime); @@ -971,9 +966,8 @@ /** * \x83o\x83b\x83t\x83@\x93\xE0\x82̃\x8D\x83O\x82\xF0\x83t\x83@\x83C\x83\x8B\x82֏\x91\x82\xAB\x8D\x9E\x82\xDE */ -static void LogToFile(void) +static void LogToFile(PFileVar fv) { - PFileVar fv = LogVar; PCHAR Buf; int Start, Count; BYTE b; @@ -1015,17 +1009,17 @@ WriteBuf[WriteBufLen++] = b; - (LogVar->ByteCount)++; + (fv->ByteCount)++; } // \x8F\x91\x82\xAB\x8D\x9E\x82\xDD if (WriteBufLen > 0) { if (ts.DeferredLogWriteMode) { - PostThreadMessage(LogVar->LogThreadId, WM_DPC_LOGTHREAD_SEND, (WPARAM)WriteBuf, WriteBufLen); + PostThreadMessage(fv->LogThreadId, WM_DPC_LOGTHREAD_SEND, (WPARAM)WriteBuf, WriteBufLen); } else { DWORD wrote; - WriteFile(LogVar->FileHandle, WriteBuf, WriteBufLen, &wrote, NULL); + WriteFile(fv->FileHandle, WriteBuf, WriteBufLen, &wrote, NULL); free(WriteBuf); } } @@ -1042,11 +1036,11 @@ cv_BCount = Count; } if (FLogIsPause() || ProtoGetProtoFlag()) return; - LogVar->FLogDlg->RefreshNum(LogVar->StartTime, LogVar->FileSize, LogVar->ByteCount); + fv->FLogDlg->RefreshNum(fv->StartTime, fv->FileSize, fv->ByteCount); // \x83\x8D\x83O\x81E\x83\x8D\x81[\x83e\x81[\x83g - LogRotate(); + LogRotate(fv); } static BOOL CreateLogBuf(void) @@ -1091,29 +1085,26 @@ cv_BCount = 0; } -static void FileTransEnd_(void) +static void FileTransEnd_(PFileVar fv) { - if (LogVar == NULL) { - return; - } - PFileVar fv = LogVar; fv->FileLog = FALSE; fv->BinLog = FALSE; cv.Log1Byte = NULL; cv.Log1Bin = NULL; cv.LogBinSkip = NULL; - PFileTransDlg FLogDlg = LogVar->FLogDlg; + PFileTransDlg FLogDlg = fv->FLogDlg; if (FLogDlg != NULL) { FLogDlg->DestroyWindow(); FLogDlg = NULL; - LogVar->FLogDlg = NULL; + fv->FLogDlg = NULL; } - CloseFileSync(LogVar); + CloseFileSync(fv); FreeLogBuf(); FreeBinBuf(); - free(LogVar->FullName); - LogVar->FullName = NULL; - free(LogVar); + free(fv->FullName); + fv->FullName = NULL; + free(fv); + LogVar = NULL; } @@ -1122,11 +1113,12 @@ */ void FLogPause(BOOL Pause) { - if (LogVar == NULL) { + PFileVar fv = LogVar; + if (fv == NULL) { return; } - LogVar->IsPause = Pause; - LogVar->FLogDlg->ChangeButton(Pause); + fv->IsPause = Pause; + fv->FLogDlg->ChangeButton(Pause); } /** @@ -1135,11 +1127,12 @@ */ void FLogRotateSize(size_t size) { - if (LogVar == NULL) { + PFileVar fv = LogVar; + if (fv == NULL) { return; } - LogVar->RotateMode = ROTATE_SIZE; - LogVar->RotateSize = (LONG)size; + fv->RotateMode = ROTATE_SIZE; + fv->RotateSize = (LONG)size; } /** @@ -1148,10 +1141,11 @@ */ void FLogRotateRotate(int step) { - if (LogVar == NULL) { + PFileVar fv = LogVar; + if (fv == NULL) { return; } - LogVar->RotateStep = step; + fv->RotateStep = step; } /** @@ -1160,12 +1154,13 @@ */ void FLogRotateHalt(void) { - if (LogVar == NULL) { + PFileVar fv = LogVar; + if (fv == NULL) { return; } - LogVar->RotateMode = ROTATE_NONE; - LogVar->RotateSize = 0; - LogVar->RotateStep = 0; + fv->RotateMode = ROTATE_NONE; + fv->RotateSize = 0; + fv->RotateStep = 0; } static INT_PTR CALLBACK OnCommentDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM) @@ -1215,7 +1210,8 @@ */ void FLogAddCommentDlg(HINSTANCE hInst, HWND hWnd) { - if (LogVar == NULL) { + PFileVar fv = LogVar; + if (fv == NULL) { return; } TTDialogBox(hInst, MAKEINTRESOURCE(IDD_COMMENT_DIALOG), @@ -1224,11 +1220,12 @@ void FLogClose(void) { - if (LogVar == NULL) { + PFileVar fv = LogVar; + if (fv == NULL) { return; } - FileTransEnd_(); + FileTransEnd_(fv); } /** @@ -1241,14 +1238,12 @@ */ BOOL FLogOpen(const wchar_t *fname, LogCode_t code, BOOL bom) { - BOOL ret; - if (LogVar != NULL) { return FALSE; } // - PFileVar fv = (PFileVar)malloc(sizeof(TFileVar)); + PFileVar fv = (PFileVar)malloc(sizeof(*fv)); if (fv == NULL) { return FALSE; } @@ -1260,9 +1255,9 @@ fv->log_code = code; fv->bom = bom; - ret = LogStart(fname); + BOOL ret = LogStart(fv, fname); if (ret == FALSE) { - FileTransEnd_(); + FileTransEnd_(fv); } return ret; @@ -1292,7 +1287,8 @@ void FLogInfo(char *param_ptr, size_t param_len) { - if (LogVar) { + PFileVar fv = LogVar; + if (fv) { param_ptr[0] = '0' + (ts.LogBinary != 0) + ((ts.Append != 0) << 1) @@ -1299,7 +1295,7 @@ + ((ts.LogTypePlainText != 0) << 2) + ((ts.LogTimestamp != 0) << 3) + ((ts.LogHideDialog != 0) << 4); - char *filenameU8 = ToU8W(LogVar->FullName); + char *filenameU8 = ToU8W(fv->FullName); strncpy_s(param_ptr + 1, param_len - 1, filenameU8, _TRUNCATE); free(filenameU8); } @@ -1495,7 +1491,7 @@ if (cv_LogBuf!=NULL) { if (fv->FileLog) { - LogToFile(); + LogToFile(fv); } } @@ -1502,7 +1498,7 @@ if (cv_BinBuf!=NULL) { if (fv->BinLog) { - LogToFile(); + LogToFile(fv); } } } @@ -1521,7 +1517,7 @@ if (ts.LogTimestamp && fv->eLineEnd) { // \x83^\x83C\x83\x80\x83X\x83^\x83\x93\x83v\x82\xF0\x8Fo\x97\xCD fv->eLineEnd = Line_Other; /* clear endmark*/ - wchar_t* strtime = TimeStampStr(); + wchar_t* strtime = TimeStampStr(fv); FLogWriteStr(strtime); free(strtime); } @@ -1562,9 +1558,8 @@ } } -static void FLogOutputBOM(void) +static void FLogOutputBOM(PFileVar fv) { - PFileVar fv = LogVar; DWORD wrote; switch(fv->log_code) {