[Ttssh2-commit] [8885] ログバッファからファイルへの書き込み(LogToFile())を修正

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2020年 8月 6日 (木) 00:07:17 JST


Revision: 8885
          https://osdn.net/projects/ttssh2/scm/svn/commits/8885
Author:   zmatsuo
Date:     2020-08-06 00:07:17 +0900 (Thu, 06 Aug 2020)
Log Message:
-----------
ログバッファからファイルへの書き込み(LogToFile())を修正

- スレッドと直接書き込みの2つに別れていたところを1つにまとめた
- タイムスタンプ文字列生成を別関数に分離した TimeStampStr()

Modified Paths:
--------------
    branches/filesys_log/teraterm/teraterm/filesys_log.cpp

-------------- next part --------------
Modified: branches/filesys_log/teraterm/teraterm/filesys_log.cpp
===================================================================
--- branches/filesys_log/teraterm/teraterm/filesys_log.cpp	2020-08-05 15:07:09 UTC (rev 8884)
+++ branches/filesys_log/teraterm/teraterm/filesys_log.cpp	2020-08-05 15:07:17 UTC (rev 8885)
@@ -781,7 +781,7 @@
  */
 void FLogOutputAllBuffer(void)
 {
-	DWORD ofs, written_size;
+	DWORD ofs;
 	int size;
 	wchar_t buf[512];
 	for (ofs = 0 ;  ; ofs++ ) {
@@ -920,6 +920,35 @@
 	logfile_unlock();
 }
 
+static char *TimeStampStr()
+{
+	char *strtime = NULL;
+	switch (ts.LogTimestampType) {
+	case TIMESTAMP_LOCAL:
+	default:
+		strtime = mctimelocal(ts.LogTimestampFormat, FALSE);
+		break;
+	case TIMESTAMP_UTC:
+		strtime = mctimelocal(ts.LogTimestampFormat, TRUE);
+		break;
+	case TIMESTAMP_ELAPSED_LOGSTART:
+		strtime = strelapsed(LogVar->StartTime);
+		break;
+	case TIMESTAMP_ELAPSED_CONNECTED:
+		strtime = strelapsed(cv.ConnectedTime);
+		break;
+	}
+
+	char tmp[128];
+	tmp[0] = 0;
+	strncat_s(tmp, sizeof(tmp), "[", _TRUNCATE);
+	strncat_s(tmp, sizeof(tmp), strtime, _TRUNCATE);
+	strncat_s(tmp, sizeof(tmp), "] ", _TRUNCATE);
+
+	return strdup(tmp);
+//	return ToWcharA(tmp);
+}
+
 /**
  * \x83o\x83b\x83t\x83@\x93\xE0\x82̃\x8D\x83O\x82\xF0\x83t\x83@\x83C\x83\x8B\x82֏\x91\x82\xAB\x8D\x9E\x82\xDE
  */
@@ -928,10 +957,6 @@
 	PCHAR Buf;
 	int Start, Count;
 	BYTE b;
-	PCHAR WriteBuf;
-	DWORD WriteBufMax, WriteBufLen;
-	CHAR tmp[128];
-	DWORD wrote;
 	PFileVar fv = LogVar;
 
 	if (FileLog)
@@ -955,104 +980,52 @@
 	// \x83\x8D\x83b\x83N\x82\xF0\x8E\xE6\x82\xE9(2004.8.6 yutaka)
 	logfile_lock();
 
-	if (ts.DeferredLogWriteMode) {
-		WriteBufMax = 8192;
-		WriteBufLen = 0;
-		WriteBuf = (PCHAR)malloc(WriteBufMax);
-		while (Get1(Buf,&Start,&Count,&b)) {
-			if (!FLogIsPause() && (! cv.ProtoFlag))
-			{
-				tmp[0] = 0;
-				if ( ts.LogTimestamp && fv->eLineEnd ) {
-					char *strtime = NULL;
+	// \x8F\x91\x82\xAB\x8D\x9E\x82݃f\x81[\x83^\x82\xF0\x8D쐬\x82\xB7\x82\xE9
+	DWORD WriteBufMax = 8192;
+	DWORD WriteBufLen = 0;
+	PCHAR WriteBuf = (PCHAR)malloc(WriteBufMax);
+	while (Get1(Buf,&Start,&Count,&b)) {
+		if (FLogIsPause() || (cv.ProtoFlag)) {
+			continue;
+		}
 
-					switch (ts.LogTimestampType) {
-					case TIMESTAMP_LOCAL:
-						strtime = mctimelocal(ts.LogTimestampFormat, FALSE);
-						break;
-					case TIMESTAMP_UTC:
-						strtime = mctimelocal(ts.LogTimestampFormat, TRUE);
-						break;
-					case TIMESTAMP_ELAPSED_LOGSTART:
-						strtime = strelapsed(LogVar->StartTime);
-						break;
-					case TIMESTAMP_ELAPSED_CONNECTED:
-						strtime = strelapsed(cv.ConnectedTime);
-						break;
-					}
+		if (WriteBufLen >= (WriteBufMax*4/5)) {
+			WriteBufMax *= 2;
+			WriteBuf = (PCHAR)realloc(WriteBuf, WriteBufMax);
+		}
 
-					/* 2007.05.24 Gentaro */
-					if(fv->eLineEnd == Line_FileHead ){
-						strncat_s(tmp, sizeof(tmp), "\r\n", _TRUNCATE);
-					}
-					strncat_s(tmp, sizeof(tmp), "[", _TRUNCATE);
-					strncat_s(tmp, sizeof(tmp), strtime, _TRUNCATE);
-					strncat_s(tmp, sizeof(tmp), "] ", _TRUNCATE);
-				}
+		// add time stamp string
+		if ( ts.LogTimestamp && fv->eLineEnd ) {
+			char *strtime = TimeStampStr();
+			size_t len = strlen(strtime);
+			memcpy(&WriteBuf[WriteBufLen], strtime, len);
+			free(strtime);
+			WriteBufLen += len;
+		}
 
-				/* 2007.05.24 Gentaro */
-				if( b == 0x0a ){
-					fv->eLineEnd = Line_LineHead; /* set endmark*/
-				}
-				else {
-					fv->eLineEnd = Line_Other; /* clear endmark*/
-				}
-
-				if (WriteBufLen >= (WriteBufMax*4/5)) {
-					WriteBufMax *= 2;
-					WriteBuf = (PCHAR)realloc(WriteBuf, WriteBufMax);
-				}
-				memcpy(&WriteBuf[WriteBufLen], tmp, strlen(tmp));
-				WriteBufLen += strlen(tmp);
-				WriteBuf[WriteBufLen++] = b;
-
-				(LogVar->ByteCount)++;
-			}
+		/* 2007.05.24 Gentaro */
+		if( b == 0x0a ){
+			fv->eLineEnd = Line_LineHead; /* set endmark*/
 		}
+		else {
+			fv->eLineEnd = Line_Other; /* clear endmark*/
+		}
 
-		PostThreadMessage(LogVar->LogThreadId, WM_DPC_LOGTHREAD_SEND, (WPARAM)WriteBuf, WriteBufLen);
+		WriteBuf[WriteBufLen++] = b;
 
-	} else {
+		(LogVar->ByteCount)++;
+	}
 
-		while (Get1(Buf,&Start,&Count,&b))
-		{
-			if (!FLogIsPause() && (! cv.ProtoFlag))
-			{
-				if ( ts.LogTimestamp && fv->eLineEnd ) {
-					char *strtime = NULL;
-
-					switch (ts.LogTimestampType) {
-					case TIMESTAMP_LOCAL:
-						strtime = mctimelocal(ts.LogTimestampFormat, FALSE);
-						break;
-					case TIMESTAMP_UTC:
-						strtime = mctimelocal(ts.LogTimestampFormat, TRUE);
-						break;
-					case TIMESTAMP_ELAPSED_LOGSTART:
-						strtime = strelapsed(LogVar->StartTime);
-						break;
-					case TIMESTAMP_ELAPSED_CONNECTED:
-						strtime = strelapsed(cv.ConnectedTime);
-						break;
-					}
-					WriteFile(LogVar->FileHandle, "[", 1, &wrote, NULL);
-					WriteFile(LogVar->FileHandle, strtime, strlen(strtime), &wrote, NULL);
-					WriteFile(LogVar->FileHandle, "] ", 2, &wrote, NULL);
-				}
-
-				/* 2007.05.24 Gentaro */
-				if( b == 0x0a ){
-					fv->eLineEnd = Line_LineHead; /* set endmark*/
-				}
-				else {
-					fv->eLineEnd = Line_Other; /* clear endmark*/
-				}
-
-				WriteFile(LogVar->FileHandle, (PCHAR)&b, 1, &wrote, NULL);
-				(LogVar->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);
 		}
-
+		else {
+			DWORD wrote;
+			WriteFile(LogVar->FileHandle, WriteBuf, WriteBufLen, &wrote, NULL);
+			free(WriteBuf);
+		}
 	}
 
 	logfile_unlock();


Ttssh2-commit メーリングリストの案内
Back to archive index