[Ttssh2-commit] [5206] ログファイルの遅延書き込みをサポートした。

Zurück zum Archiv-Index

svnno****@sourc***** svnno****@sourc*****
2013年 4月 20日 (土) 00:31:58 JST


Revision: 5206
          http://sourceforge.jp/projects/ttssh2/scm/svn/commits/5206
Author:   yutakapon
Date:     2013-04-20 00:31:54 +0900 (Sat, 20 Apr 2013)
Log Message:
-----------
ログファイルの遅延書き込みをサポートした。
ネットワーク経由でのログ採取時、Tera Term自身がスローダウンする問題への改善。
チケット #25434

Ticket Links:
------------
    http://sourceforge.jp/projects/ttssh2/tracker/detail/25434

Modified Paths:
--------------
    trunk/installer/release/TERATERM.INI
    trunk/teraterm/common/ttftypes.h
    trunk/teraterm/common/tttypes.h
    trunk/teraterm/teraterm/filesys.cpp
    trunk/teraterm/ttpset/ttset.c

-------------- next part --------------
Modified: trunk/installer/release/TERATERM.INI
===================================================================
--- trunk/installer/release/TERATERM.INI	2013-04-18 13:03:30 UTC (rev 5205)
+++ trunk/installer/release/TERATERM.INI	2013-04-19 15:31:54 UTC (rev 5206)
@@ -354,7 +354,10 @@
 ; Step: 0(none), >=1(count times)
 LogRotateStep=0
 
+; Deferred Log Write Mode (on/off)
+DeferredLogWriteMode=on
 
+
 ; XMODEM option (checksum/crc/1k)
 XmodemOpt=checksum
 ; Binary flag for XMODEM Receive and ZMODEM Send (on/off)

Modified: trunk/teraterm/common/ttftypes.h
===================================================================
--- trunk/teraterm/common/ttftypes.h	2013-04-18 13:03:30 UTC (rev 5205)
+++ trunk/teraterm/common/ttftypes.h	2013-04-19 15:31:54 UTC (rev 5206)
@@ -111,6 +111,9 @@
   LONG RotateSize;
   int RotateStep;
 
+  HANDLE LogThread;
+  DWORD LogThreadId;
+
 } TFileVar;
 typedef TFileVar far *PFileVar;
 

Modified: trunk/teraterm/common/tttypes.h
===================================================================
--- trunk/teraterm/common/tttypes.h	2013-04-18 13:03:30 UTC (rev 5205)
+++ trunk/teraterm/common/tttypes.h	2013-04-19 15:31:54 UTC (rev 5206)
@@ -561,6 +561,7 @@
 	DWORD LogRotateSize;
 	WORD LogRotateSizeType;
 	WORD LogRotateStep;
+	WORD DeferredLogWriteMode;
 };
 
 typedef struct tttset TTTSet, *PTTSet;
@@ -1021,6 +1022,7 @@
  *   added tttset.LogRotateSize
  *   added tttset.LogRotateSizeType
  *   added tttset.LogRotateStep
+ *   added tttset.DeferredLogWriteMode
  *
  * - At version 4.74, ttset_memfilemap was replaced with ttset_memfilemap_19.
  *   added tttset.FontQuality

Modified: trunk/teraterm/teraterm/filesys.cpp
===================================================================
--- trunk/teraterm/teraterm/filesys.cpp	2013-04-18 13:03:30 UTC (rev 5205)
+++ trunk/teraterm/teraterm/filesys.cpp	2013-04-19 15:31:54 UTC (rev 5206)
@@ -23,6 +23,7 @@
 #include "ftlib.h"
 
 #include <io.h>
+#include <process.h>
 
 #define FS_BRACKET_NONE  0
 #define FS_BRACKET_START 1
@@ -88,6 +89,13 @@
 
 enum enumLineEnd eLineEnd = Line_LineHead;
 
+
+// \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 CloseFileSync(PFileVar ptr);
+
+
 BOOL LoadTTFILE()
 {
 	BOOL Err;
@@ -290,7 +298,8 @@
 {
 	if ((*fv)!=NULL)
 	{
-		if ((*fv)->FileOpen) _lclose((*fv)->FileHandle);
+		CloseFileSync(*fv);
+		//if ((*fv)->FileOpen) _lclose((*fv)->FileHandle);
 		if ((*fv)->FnStrMemHandle>0)
 		{
 			GlobalUnlock((*fv)->FnStrMemHandle);
@@ -362,11 +371,60 @@
 	}
 }
 
+
+// \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)
+{
+	if (!ptr->FileOpen)
+		return;
+
+	if (ptr->LogThread != (HANDLE)-1) {
+		// \x83X\x83\x8C\x83b\x83h\x82̏I\x97\xB9\x91҂\xBF
+		PostThreadMessage(ptr->LogThreadId, WM_QUIT, 0, 0);
+		WaitForSingleObject(ptr->LogThread, INFINITE);
+		CloseHandle(ptr->LogThread);
+		ptr->LogThread = (HANDLE)-1;
+	}
+	_lclose(ptr->FileHandle);
+}
+
+// \x92x\x89\x84\x8F\x91\x82\xAB\x8D\x9E\x82ݗp\x83X\x83\x8C\x83b\x83h
+static unsigned _stdcall DeferredLogWriteThread(void *arg) 
+{
+	MSG msg;
+	PFileVar fv = (PFileVar)arg;
+	PCHAR buf;
+	DWORD buflen;
+
+	PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
+
+	while (GetMessage(&msg, NULL, 0, 0) > 0) {
+		switch (msg.message) {
+			case WM_DPC_LOGTHREAD_SEND:
+				buf = (PCHAR)msg.wParam;
+				buflen = (DWORD)msg.lParam;
+				_lwrite(fv->FileHandle, buf, buflen );
+				free(buf);   // \x82\xB1\x82\xB1\x82Ń\x81\x83\x82\x83\x8A\x89\xF0\x95\xFA
+				break;
+
+			case WM_QUIT:
+				goto end;
+				break;
+		}
+	}
+
+end:
+	_endthreadex(0);
+	return (0);
+}
+
+
 extern "C" {
 BOOL LogStart()
 {
 	LONG Option;
 	char *logdir;
+	unsigned tid;
 
 	if ((FileLog) || (BinLog)) return FALSE;
 
@@ -542,6 +600,11 @@
 		return FALSE;
 	}
 
+	// \x92x\x89\x84\x8F\x91\x82\xAB\x8D\x9E\x82ݗp\x83X\x83\x8C\x83b\x83h\x82\xF0\x8BN\x82\xB1\x82\xB7\x81B
+	// (2013.4.19 yutaka)
+	LogVar->LogThread = (HANDLE)_beginthreadex(NULL, 0, DeferredLogWriteThread, LogVar, 0, &tid);
+	LogVar->LogThreadId = tid;
+
 	return TRUE;
 }
 }
@@ -663,6 +726,7 @@
 	char newfile[1024], oldfile[1024];
 	int i, k;
 	int dwShareMode = FILE_SHARE_READ;
+	unsigned tid;
 
 	if (! LogVar->FileOpen) return;
 
@@ -682,7 +746,8 @@
 	LogVar->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
-	_lclose(LogVar->FileHandle);
+	CloseFileSync(LogVar);
+	//_lclose(LogVar->FileHandle);
 
 	// \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)
@@ -718,6 +783,11 @@
 	LogVar->FileHandle = (int)CreateFile(LogVar->FullName, GENERIC_WRITE, dwShareMode, NULL,
 	                                     CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 
+	// \x92x\x89\x84\x8F\x91\x82\xAB\x8D\x9E\x82ݗp\x83X\x83\x8C\x83b\x83h\x82\xF0\x8BN\x82\xB1\x82\xB7\x81B
+	// (2013.4.19 yutaka)
+	LogVar->LogThread = (HANDLE)_beginthreadex(NULL, 0, DeferredLogWriteThread, LogVar, 0, &tid);
+	LogVar->LogThreadId = tid;
+
 	logfile_unlock();
 
 }
@@ -727,6 +797,9 @@
 	PCHAR Buf;
 	int Start, Count;
 	BYTE b;
+	PCHAR WriteBuf;
+	DWORD WriteBufMax, WriteBufLen;
+	CHAR tmp[128];
 
 	if (! LogVar->FileOpen) return;
 	if (FileLog)
@@ -750,53 +823,98 @@
 	// \x83\x8D\x83b\x83N\x82\xF0\x8E\xE6\x82\xE9(2004.8.6 yutaka)
 	logfile_lock();
 
-	while (Get1(Buf,&Start,&Count,&b))
-	{
-		if (((cv.FilePause & OpLog)==0) && (! cv.ProtoFlag))
+	if (ts.DeferredLogWriteMode) {
+		WriteBufMax = 8192;
+		WriteBufLen = 0;
+		WriteBuf = (PCHAR)malloc(WriteBufMax);
+		while (Get1(Buf,&Start,&Count,&b)) {
+			if (((cv.FilePause & OpLog)==0) && (! cv.ProtoFlag))
+			{
+				tmp[0] = 0;
+				if ( ts.LogTimestamp && eLineEnd ) {
+					char *strtime = mctimelocal();
+					/* 2007.05.24 Gentaro */
+					if( 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);
+				}
+				
+				/* 2007.05.24 Gentaro */
+				if( b == 0x0a ){
+					eLineEnd = Line_LineHead; /* set endmark*/
+				}
+				else {
+					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)++;
+			}
+		}
+
+		PostThreadMessage(LogVar->LogThreadId, WM_DPC_LOGTHREAD_SEND, (WPARAM)WriteBuf, WriteBufLen);
+
+	} else {
+
+		while (Get1(Buf,&Start,&Count,&b))
 		{
-			// \x8E\x9E\x8D\x8F\x82\xF0\x8F\x91\x82\xAB\x8Fo\x82\xB7(2006.7.23 maya)
-			// \x93\xFA\x95t\x83t\x83H\x81[\x83}\x83b\x83g\x82\xF0\x93\xFA\x96{\x82ł͂Ȃ\xAD\x90\xA2\x8AE\x95W\x8F\x80\x82ɕύX\x82\xB5\x82\xBD (2006.7.23 yutaka)
-			/* 2007.05.24 Gentaro */
-			// \x83~\x83\x8A\x95b\x82\xE0\x95\\x8E\xA6\x82\xB7\x82\xE9\x82悤\x82ɕύX (2009.5.23 maya)
-			if ( ts.LogTimestamp && eLineEnd ) {
-#if 1
-#if 0
-				SYSTEMTIME	LocalTime;
-				GetLocalTime(&LocalTime);
-				char strtime[27];
+			if (((cv.FilePause & OpLog)==0) && (! cv.ProtoFlag))
+			{
+				// \x8E\x9E\x8D\x8F\x82\xF0\x8F\x91\x82\xAB\x8Fo\x82\xB7(2006.7.23 maya)
+				// \x93\xFA\x95t\x83t\x83H\x81[\x83}\x83b\x83g\x82\xF0\x93\xFA\x96{\x82ł͂Ȃ\xAD\x90\xA2\x8AE\x95W\x8F\x80\x82ɕύX\x82\xB5\x82\xBD (2006.7.23 yutaka)
+				/* 2007.05.24 Gentaro */
+				// \x83~\x83\x8A\x95b\x82\xE0\x95\\x8E\xA6\x82\xB7\x82\xE9\x82悤\x82ɕύX (2009.5.23 maya)
+				if ( ts.LogTimestamp && eLineEnd ) {
+	#if 1
+	#if 0
+					SYSTEMTIME	LocalTime;
+					GetLocalTime(&LocalTime);
+					char strtime[27];
 
-				// format time
-				sprintf(strtime, "[%04d/%02d/%02d %02d:%02d:%02d.%03d] ",
-						LocalTime.wYear, LocalTime.wMonth,LocalTime.wDay,
-						LocalTime.wHour, LocalTime.wMinute, LocalTime.wSecond,
-						LocalTime.wMilliseconds);
-#else
-				char *strtime = mctimelocal();
-#endif
-#else
-					time_t tick = time(NULL);
-					char *strtime = ctime(&tick); 
-#endif
+					// format time
+					sprintf(strtime, "[%04d/%02d/%02d %02d:%02d:%02d.%03d] ",
+							LocalTime.wYear, LocalTime.wMonth,LocalTime.wDay,
+							LocalTime.wHour, LocalTime.wMinute, LocalTime.wSecond,
+							LocalTime.wMilliseconds);
+	#else
+					char *strtime = mctimelocal();
+	#endif
+	#else
+						time_t tick = time(NULL);
+						char *strtime = ctime(&tick); 
+	#endif
+					/* 2007.05.24 Gentaro */
+					if( eLineEnd == Line_FileHead ){
+						_lwrite(LogVar->FileHandle,"\r\n",2);
+					}
+					_lwrite(LogVar->FileHandle,"[",1);
+					_lwrite(LogVar->FileHandle, strtime, strlen(strtime));
+					_lwrite(LogVar->FileHandle,"] ",2);
+				}
+				
 				/* 2007.05.24 Gentaro */
-				if( eLineEnd == Line_FileHead ){
-					_lwrite(LogVar->FileHandle,"\r\n",2);
+				if( b == 0x0a ){
+					eLineEnd = Line_LineHead; /* set endmark*/
 				}
-				_lwrite(LogVar->FileHandle,"[",1);
-				_lwrite(LogVar->FileHandle, strtime, strlen(strtime));
-				_lwrite(LogVar->FileHandle,"] ",2);
+				else {
+					eLineEnd = Line_Other; /* clear endmark*/
+				}
+
+				_lwrite(LogVar->FileHandle,(PCHAR)&b,1);
+				(LogVar->ByteCount)++;
 			}
-			
-			/* 2007.05.24 Gentaro */
-			if( b == 0x0a ){
-				eLineEnd = Line_LineHead; /* set endmark*/
-			}
-			else {
-				eLineEnd = Line_Other; /* clear endmark*/
-			}
+		}
 
-			_lwrite(LogVar->FileHandle,(PCHAR)&b,1);
-			(LogVar->ByteCount)++;
-		}
 	}
 
 	logfile_unlock();

Modified: trunk/teraterm/ttpset/ttset.c
===================================================================
--- trunk/teraterm/ttpset/ttset.c	2013-04-18 13:03:30 UTC (rev 5205)
+++ trunk/teraterm/ttpset/ttset.c	2013-04-19 15:31:54 UTC (rev 5206)
@@ -781,7 +781,10 @@
 	ts->LogRotateSizeType = GetPrivateProfileInt(Section, "LogRotateSizeType", 0, FName);
 	ts->LogRotateStep = GetPrivateProfileInt(Section, "LogRotateStep", 0, FName);
 
+	/* Deferred Log Write Mode (2013.4.20 yutaka) */
+	ts->DeferredLogWriteMode = GetOnOff(Section, "DeferredLogWriteMode", FName, TRUE);
 
+
 	/* XMODEM option */
 	GetPrivateProfileString(Section, "XmodemOpt", "",
 	                        Temp, sizeof(Temp), FName);
@@ -2119,6 +2122,9 @@
 	WriteInt(Section, "LogRotateSizeType", FName, ts->LogRotateSizeType);
 	WriteInt(Section, "LogRotateStep", FName, ts->LogRotateStep);
 
+	/* Deferred Log Write Mode (2013.4.20 yutaka) */
+	WriteOnOff(Section, "DeferredLogWriteMode", FName, ts->DeferredLogWriteMode);
+
 	/* XMODEM option */
 	switch (ts->XmodemOpt) {
 	case XoptCRC:



Ttssh2-commit メーリングリストの案内
Zurück zum Archiv-Index