Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/teraterm/teraterm/filesys_log.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10614 - (hide annotations) (download) (as text)
Wed Mar 1 14:18:19 2023 UTC (13 months, 1 week ago) by zmatsuo
File MIME type: text/x-c++src
File size: 40687 byte(s)
lngファイル名変数を ANSI版から Unicode 版へ切り替え

- tttset.UILanguageFile 参照部分を tttset.UILanguageFileW へ変更
- SetI18nMenuStrs() -> SetI18nMenuStrsW()
- GetI18nStrWA() -> GetI18nStrWW()
- MessageBox() -> MessageBoxW()
- GetCommonDialogFilterW() -> GetCommonDialogFilterWW()
  - GetCommonDialogFilterW() は ANSI版lngファイル名を参照
  - GetCommonDialogFilterW() 削除
1 doda 6806 /*
2 nmaya 9048 * (C) 2020- TeraTerm Project
3 doda 6806 * All rights reserved.
4     *
5 doda 6841 * Redistribution and use in source and binary forms, with or without
6     * modification, are permitted provided that the following conditions
7     * are met:
8 doda 6806 *
9 doda 6841 * 1. Redistributions of source code must retain the above copyright
10     * notice, this list of conditions and the following disclaimer.
11     * 2. Redistributions in binary form must reproduce the above copyright
12     * notice, this list of conditions and the following disclaimer in the
13     * documentation and/or other materials provided with the distribution.
14     * 3. The name of the author may not be used to endorse or promote products
15     * derived from this software without specific prior written permission.
16 doda 6806 *
17 doda 6841 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20     * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 doda 6806 */
28 maya 3227
29 zmatsuo 8897 /* TERATERM.EXE, log routines */
30 zmatsuo 7526 #include <stdio.h>
31 zmatsuo 8900 #if !defined(_CRTDBG_MAP_ALLOC)
32     #define _CRTDBG_MAP_ALLOC
33     #endif
34     #include <stdlib.h>
35     #include <crtdbg.h>
36 zmatsuo 7526 #include <process.h>
37 zmatsuo 8852 #include <windows.h>
38     #include <htmlhelp.h>
39     #include <assert.h>
40 zmatsuo 7526
41 maya 3227 #include "teraterm.h"
42     #include "tttypes.h"
43     #include "ftdlg.h"
44     #include "ttwinman.h"
45     #include "commlib.h"
46     #include "ttcommon.h"
47     #include "ttlib.h"
48     #include "dlglib.h"
49 doda 3904 #include "vtterm.h"
50 maya 3227 #include "ftlib.h"
51 yutakapon 5392 #include "buffer.h"
52 zmatsuo 8852 #include "helpid.h"
53 zmatsuo 8863 #include "codeconv.h"
54 zmatsuo 8899 #include "asprintf.h"
55 zmatsuo 9857 #include "win32helper.h"
56 yutakapon 5392
57 zmatsuo 8852 #include "filesys_log_res.h"
58 zmatsuo 8900 #include "filesys_log.h"
59 zmatsuo 9103 #include "filesys.h" // for ProtoGetProtoFlag()
60 zmatsuo 8866
61 zmatsuo 9104 #define TitLog L"Log"
62    
63 zmatsuo 8901 /*
64     Line Head flag for timestamping
65     2007.05.24 Gentaro
66     */
67     enum enumLineEnd {
68     Line_Other = 0,
69     Line_LineHead = 1,
70     Line_FileHead = 2,
71     };
72    
73 zmatsuo 8894 typedef struct {
74 zmatsuo 8901 wchar_t *FullName;
75 maya 3227
76 zmatsuo 8901 HANDLE FileHandle;
77     LONG FileSize, ByteCount;
78 maya 6071
79 zmatsuo 8901 DWORD StartTime;
80 maya 3227
81 zmatsuo 8901 enum enumLineEnd eLineEnd;
82 maya 7182
83 zmatsuo 8901 // log rotate
84     int RotateMode; // enum rotate_mode RotateMode;
85     LONG RotateSize;
86     int RotateStep;
87 zmatsuo 8900
88 zmatsuo 8901 HANDLE LogThread;
89     DWORD LogThreadId;
90     HANDLE LogThreadEvent;
91    
92     BOOL IsPause;
93    
94     PFileTransDlg FLogDlg;
95 zmatsuo 8904
96 zmatsuo 8912 LogCode_t log_code;
97     BOOL bom;
98 zmatsuo 8904
99 zmatsuo 9872 BOOL FileLog;
100     BOOL BinLog;
101 zmatsuo 9873 } TFileVar;
102     typedef TFileVar *PFileVar;
103 zmatsuo 8894
104     static PFileVar LogVar = NULL;
105    
106 zmatsuo 8906 static PCHAR cv_LogBuf;
107     static int cv_LogPtr, cv_LStart, cv_LCount;
108     static PCHAR cv_BinBuf;
109     static int cv_BinPtr, cv_BStart, cv_BCount;
110     static int cv_BinSkip;
111    
112 yutakapon 5206 // �x�����������p�X���b�h�����b�Z�[�W
113     #define WM_DPC_LOGTHREAD_SEND (WM_APP + 1)
114    
115 zmatsuo 8897 static void Log1Bin(BYTE b);
116     static void LogBinSkip(int add);
117     static BOOL CreateLogBuf(void);
118     static BOOL CreateBinBuf(void);
119 zmatsuo 8905 void LogPut1(BYTE b);
120 zmatsuo 8907 static void OutputStr(const wchar_t *str);
121 zmatsuo 9873 static void LogToFile(PFileVar fv);
122     static void FLogOutputBOM(PFileVar fv);
123 yutakapon 5206
124 zmatsuo 8894 static BOOL OpenFTDlg_(PFileVar fv)
125 maya 3227 {
126 zmatsuo 8899 PFileTransDlg FTDlg = new CFileTransDlg();
127     if (FTDlg == NULL) {
128     return FALSE;
129     }
130 maya 3227
131 zmatsuo 8899 wchar_t *DlgCaption;
132 zmatsuo 10614 wchar_t *uimsg;
133     GetI18nStrWW("Tera Term", "FILEDLG_TRANS_TITLE_LOG", TitLog, ts.UILanguageFileW, &uimsg);
134 zmatsuo 8899 aswprintf(&DlgCaption, L"Tera Term: %s", uimsg);
135 zmatsuo 10614 free(uimsg);
136 maya 3227
137 zmatsuo 9070 CFileTransDlg::Info info;
138 zmatsuo 10614 info.UILanguageFileW = ts.UILanguageFileW;
139 zmatsuo 9070 info.OpId = CFileTransDlg::OpLog;
140 zmatsuo 8899 info.DlgCaption = DlgCaption;
141 zmatsuo 8911 info.FileName = NULL;
142 zmatsuo 8899 info.FullName = fv->FullName;
143     info.HideDialog = ts.LogHideDialog ? TRUE : FALSE;
144     info.HMainWin = HVTWin;
145     FTDlg->Create(hInst, &info);
146     FTDlg->RefreshNum(0, fv->FileSize, fv->ByteCount);
147 doda 4454
148 zmatsuo 8901 fv->FLogDlg = FTDlg;
149 doda 5383
150 zmatsuo 8899 free(DlgCaption);
151     return TRUE;
152 maya 3227 }
153    
154 zmatsuo 8852 /**
155 zmatsuo 10443 * �t�@�C�������������u������,���O�p
156     * �����������u��������
157     * &h �z�X�g�����u��
158     * &p TCP�|�[�g�������u��
159 zmatsuo 8852 * &u ���O�I���������[�U��
160 zmatsuo 10443 *
161     * @param pcv
162     * @param src �u���������O��������(�t�@�C����)
163     * @return �u������������������
164     * �s�v����������free()��������
165 zmatsuo 8852 */
166 zmatsuo 10444 static wchar_t *ConvertLognameW(const TComVar *pcv, const wchar_t *src)
167 maya 3227 {
168 zmatsuo 10443 const TTTSet *pts = pcv->ts;
169     size_t dest_len = wcslen(src) + 1;
170     wchar_t *dest = (wchar_t *)malloc(sizeof(wchar_t) * dest_len);
171 maya 3227
172 zmatsuo 10443 const wchar_t *s = src;
173     size_t i = 0;
174 maya 3227
175 zmatsuo 10443 while(*s != '\0') {
176     if (*s == '&' && *(s+1) != '\0') {
177     wchar_t c = *(s+1);
178     wchar_t *add_text = NULL;
179     switch (c) {
180     case 'h':
181     s += 2;
182     if (pcv->Open) {
183     switch(pcv->PortType) {
184     case IdTCPIP: {
185     // �z�X�g����IPv6�A�h���X�����A�t�@�C�������g�p������������(:)�����������u��
186     wchar_t *host = ToWcharA(pts->HostName);
187     wchar_t *host_fix = replaceInvalidFileNameCharW(host, '_');
188     free(host);
189     add_text = host_fix;
190     break;
191 maya 3227 }
192 zmatsuo 10443 case IdSerial: {
193     wchar_t *port;
194     aswprintf(&port, L"COM%d", ts.ComPort);
195     add_text = port;
196     break;
197 maya 3227 }
198 zmatsuo 10443 default:
199     ;
200     }
201 maya 3227 }
202     break;
203 zmatsuo 10443 case 'p':
204     s += 2;
205     if (pcv->Open) {
206     if (pcv->PortType == IdTCPIP) {
207     wchar_t *port;
208     aswprintf(&port, L"%d", ts.TCPPort);
209     add_text = port;
210 maya 3473 }
211     }
212     break;
213 zmatsuo 10443 case 'u': {
214     s += 2;
215     wchar_t user[256 + 1]; // 256=UNLEN
216     DWORD l = _countof(user);
217     if (GetUserNameW(user, &l) != 0) {
218 zmatsuo 10468 add_text = _wcsdup(user);
219 maya 6590 }
220     break;
221 maya 3227 }
222 zmatsuo 10443 default:
223     // pass '&'
224     s++;
225     add_text = NULL;
226     break;
227     }
228    
229 zmatsuo 10444 if (add_text != NULL) {
230 zmatsuo 10443 size_t l = wcslen(add_text);
231     dest_len += l;
232     dest = (wchar_t *)realloc(dest, sizeof(wchar_t) * dest_len);
233     wcscpy(&dest[i], add_text);
234 zmatsuo 10444 free(add_text);
235 zmatsuo 10443 i += l;
236     }
237 maya 3227 }
238     else {
239 zmatsuo 10443 dest[i] = *s++;
240     i++;
241 maya 3227 }
242     }
243 zmatsuo 10443 dest[i] = 0;
244     return dest;
245 maya 3227 }
246    
247 zmatsuo 8858 static void FixLogOption(void)
248 maya 3227 {
249 doda 3887 if (ts.LogBinary) {
250 maya 3227 ts.LogTypePlainText = false;
251     ts.LogTimestamp = false;
252     }
253     }
254    
255 yutakapon 5206
256     // �X���b�h���I�����t�@�C�����N���[�Y
257 zmatsuo 9873 static void CloseFileSync(PFileVar fv)
258 yutakapon 5206 {
259 yutakapon 6489 BOOL ret;
260    
261 zmatsuo 9873 if (fv->FileHandle == INVALID_HANDLE_VALUE) {
262 yutakapon 5206 return;
263 zmatsuo 8901 }
264 yutakapon 5206
265 zmatsuo 9873 if (fv->LogThread != INVALID_HANDLE_VALUE) {
266 yutakapon 5206 // �X���b�h���I������
267 zmatsuo 9873 ret = PostThreadMessage(fv->LogThreadId, WM_QUIT, 0, 0);
268 yutakapon 6489 if (ret != 0) {
269     // �X���b�h�L���[���G���L���[���������������������������s���B
270 zmatsuo 9873 WaitForSingleObject(fv->LogThread, INFINITE);
271 yutakapon 6489 }
272     else {
273 zmatsuo 8852 //DWORD code = GetLastError();
274 yutakapon 6489 }
275 zmatsuo 9873 CloseHandle(fv->LogThread);
276     fv->LogThread = INVALID_HANDLE_VALUE;
277 yutakapon 5206 }
278 zmatsuo 9873 CloseHandle(fv->FileHandle);
279     fv->FileHandle = INVALID_HANDLE_VALUE;
280 yutakapon 5206 }
281    
282     // �x�����������p�X���b�h
283 doda 6435 static unsigned _stdcall DeferredLogWriteThread(void *arg)
284 yutakapon 5206 {
285     MSG msg;
286     PFileVar fv = (PFileVar)arg;
287     PCHAR buf;
288     DWORD buflen;
289 maya 5273 DWORD wrote;
290 yutakapon 5206
291     PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
292    
293 yutakapon 6489 // �X���b�h�L���[���������I�������������X���b�h�����������m�����B
294     if (fv->LogThreadEvent != NULL) {
295     SetEvent(fv->LogThreadEvent);
296     }
297    
298 yutakapon 5206 while (GetMessage(&msg, NULL, 0, 0) > 0) {
299     switch (msg.message) {
300     case WM_DPC_LOGTHREAD_SEND:
301     buf = (PCHAR)msg.wParam;
302     buflen = (DWORD)msg.lParam;
303 zmatsuo 9873 WriteFile(fv->FileHandle, buf, buflen, &wrote, NULL);
304 yutakapon 5206 free(buf); // ����������������
305     break;
306    
307     case WM_QUIT:
308     goto end;
309     break;
310     }
311     }
312    
313     end:
314     _endthreadex(0);
315     return (0);
316     }
317    
318 zmatsuo 8901 // �x�����������p�X���b�h���N�����B
319     // (2013.4.19 yutaka)
320     // DeferredLogWriteThread �X���b�h���N�������A�X���b�h�L���[�����������������O���A
321     // ���O�t�@�C�����N���[�Y(CloseFileSync)���s���������A�G���L���[�����s���A�f�b�h���b�N
322     // �����������������C�������B
323     // �X���b�h�����������s�������A���O�����C�x���g�I�u�W�F�N�g���g�����A�X���b�h�L���[��
324     // ���������������������������������B���O�t���C�x���g�I�u�W�F�N�g���g���������A
325     // �V�X�e��(Windows OS)�������j�[�N�����O�������K�v�������B
326     // (2016.9.23 yutaka)
327     static void StartThread(PFileVar fv)
328     {
329     unsigned tid;
330     fv->LogThreadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
331     fv->LogThread = (HANDLE)_beginthreadex(NULL, 0, DeferredLogWriteThread, fv, 0, &tid);
332     fv->LogThreadId = tid;
333     if (fv->LogThreadEvent != NULL) {
334     WaitForSingleObject(fv->LogThreadEvent, INFINITE);
335     CloseHandle(fv->LogThreadEvent);
336     }
337     }
338    
339 zmatsuo 8852 /**
340     * �_�C�A���O�����e�� ts ����������
341     *
342     * TODO
343     * �_�C�A���O�����������l�������I��������
344     * ����������������������������������������������?
345     */
346     static void SetLogFlags(HWND Dialog)
347 maya 3227 {
348 zmatsuo 8852 WORD BinFlag, val;
349 maya 3227
350 zmatsuo 8852 GetRB(Dialog, &BinFlag, IDC_FOPTBIN, IDC_FOPTBIN);
351     ts.LogBinary = BinFlag;
352 maya 3227
353 zmatsuo 8866 GetRB(Dialog, &val, IDC_APPEND, IDC_APPEND);
354 zmatsuo 8852 ts.Append = val;
355    
356     if (!BinFlag) {
357     GetRB(Dialog, &val, IDC_PLAINTEXT, IDC_PLAINTEXT);
358     ts.LogTypePlainText = val;
359    
360     GetRB(Dialog, &val, IDC_TIMESTAMP, IDC_TIMESTAMP);
361     ts.LogTimestamp = val;
362 maya 3227 }
363    
364 zmatsuo 8852 GetRB(Dialog, &val, IDC_HIDEDIALOG, IDC_HIDEDIALOG);
365     ts.LogHideDialog = val;
366    
367     GetRB(Dialog, &val, IDC_ALLBUFF_INFIRST, IDC_ALLBUFF_INFIRST);
368     ts.LogAllBuffIncludedInFirst = val;
369    
370 zmatsuo 8912 ts.LogTimestampType = (WORD)(GetCurSel(Dialog, IDC_TIMESTAMPTYPE) - 1);
371 zmatsuo 8852 }
372    
373     /**
374     * ���O�t�@�C���`�F�b�N
375     *
376     * @param[in] filename
377     * @param[out] exist TURE/FALSE
378     * @param[out] bom 0 no BOM (or file not exist)
379     * 1 UTF-8
380     * 2 UTF-16LE
381     * 3 UTF-16BE
382     */
383 zmatsuo 8863 static void CheckLogFile(const wchar_t *filename, BOOL *exist, int *bom)
384 zmatsuo 8852 {
385 zmatsuo 8863 *exist = FALSE;
386     *bom = 0;
387    
388 zmatsuo 8852 // �t�@�C������������?
389 zmatsuo 9324 DWORD logdir = GetFileAttributesW(filename);
390 zmatsuo 8852 if ((logdir != INVALID_FILE_ATTRIBUTES) && ((logdir & FILE_ATTRIBUTE_DIRECTORY) == 0)) {
391 zmatsuo 8863 // �t�@�C����������
392 zmatsuo 8852 *exist = TRUE;
393    
394     // BOM�L��/�����`�F�b�N
395 zmatsuo 9324 FILE *fp = _wfopen(filename, L"rb");
396 zmatsuo 8863 if (fp != NULL) {
397     unsigned char tmp[4];
398     size_t l = fread(tmp, 1, sizeof(tmp), fp);
399     fclose(fp);
400     if (l < 2) {
401     *bom = 0;
402     } else if (l >= 2 && tmp[0] == 0xff && tmp[1] == 0xfe) {
403     // UTF-16LE
404     *bom = 2;
405     } else if (l >= 2 && tmp[0] == 0xfe && tmp[1] == 0xff) {
406     // UTF-16BE
407     *bom = 3;
408     } else if (l >= 3 && tmp[0] == 0xef && tmp[1] == 0xbb && tmp[2] == 0xbf) {
409     // UTF-8
410     *bom = 1;
411     } else {
412     *bom = 0;
413     }
414 zmatsuo 8852 }
415 maya 3227 }
416 zmatsuo 8863 }
417    
418     typedef struct {
419     FLogDlgInfo_t *info;
420     // work
421     BOOL file_exist;
422 nmaya 10144 int current_bom; // ���������t�@�C�����G���R�[�f�B���O�i�t�@�C����BOM���������j
423 zmatsuo 8863 TTTSet *pts;
424 nmaya 10009 TComVar *pcv;
425 zmatsuo 8863 } LogDlgWork_t;
426    
427 nmaya 10144
428     /*
429     * Log �_�C�A���O�������AEnable/Disable �����������R���g���[��
430 zmatsuo 10443 *
431 nmaya 10144 * - Append
432     * �w���������t�@�C������������������ Enable
433     * �w���������t�@�C�������������������� Disable
434 zmatsuo 10443 *
435 nmaya 10144 * - BOM, Encoding
436     * Text ���� New/Overwrite �������� Enable
437     * ���������������� Disable
438 nmaya 10153 * BOM ���t�@�C��������������������������������������
439     * Encoding �����L�����������������A�����t�@�C�����G���R�[�f�B���O��
440     * �����I���_�C�A���O�����f���������A���[�U�������w������������
441 zmatsuo 10443 *
442 nmaya 10144 * - Plain Text, Timestamp, Timestamp ����
443     * Text �������� Enable
444     * Binary �������� Disable
445 zmatsuo 10443 *
446 nmaya 10144 * - Timestamp ����
447     * Timestamp=on �������� Enable
448     * Timestamp=off �������� Disable
449     */
450    
451     static void ArrangeControls(HWND Dialog, LogDlgWork_t *work,
452     WORD Append, WORD LogBinary,
453     WORD LogTypePlainText, WORD LogTimestamp)
454 zmatsuo 8863 {
455     if (work->file_exist) {
456 nmaya 10144 // Append ���W�I�{�^�����A�t�@�C�����������������L��������
457 zmatsuo 8863 EnableWindow(GetDlgItem(Dialog, IDC_APPEND), TRUE);
458 nmaya 10144
459     if (Append > 0) {
460 zmatsuo 8866 CheckRadioButton(Dialog, IDC_NEW_OVERWRITE, IDC_APPEND, IDC_APPEND);
461     }
462 nmaya 10144 else {
463     CheckRadioButton(Dialog, IDC_NEW_OVERWRITE, IDC_APPEND, IDC_NEW_OVERWRITE);
464     }
465 zmatsuo 8863 }
466 nmaya 10144 else { // �t�@�C�������� -> �V�K
467 zmatsuo 8863 EnableWindow(GetDlgItem(Dialog, IDC_APPEND), FALSE);
468     CheckRadioButton(Dialog, IDC_NEW_OVERWRITE, IDC_APPEND, IDC_NEW_OVERWRITE);
469 maya 3227 }
470 zmatsuo 8852
471 nmaya 10144 if (!LogBinary && !Append) {
472     EnableWindow(GetDlgItem(Dialog, IDC_BOM), TRUE);
473     EnableWindow(GetDlgItem(Dialog, IDC_TEXTCODING_DROPDOWN), TRUE);
474     }
475     else {
476     EnableWindow(GetDlgItem(Dialog, IDC_BOM), FALSE);
477     EnableWindow(GetDlgItem(Dialog, IDC_TEXTCODING_DROPDOWN), FALSE);
478     }
479    
480     if (LogBinary) {
481     CheckRadioButton(Dialog, IDC_FOPTBIN, IDC_FOPTTEXT, IDC_FOPTBIN);
482    
483     DisableDlgItem(Dialog, IDC_PLAINTEXT, IDC_PLAINTEXT);
484     DisableDlgItem(Dialog, IDC_TIMESTAMP, IDC_TIMESTAMP);
485     DisableDlgItem(Dialog, IDC_TIMESTAMPTYPE, IDC_TIMESTAMPTYPE);
486     }
487     else {
488     CheckRadioButton(Dialog, IDC_FOPTBIN, IDC_FOPTTEXT, IDC_FOPTTEXT);
489    
490     EnableDlgItem(Dialog, IDC_PLAINTEXT, IDC_PLAINTEXT);
491     EnableDlgItem(Dialog, IDC_TIMESTAMP, IDC_TIMESTAMP);
492     EnableDlgItem(Dialog, IDC_TIMESTAMPTYPE, IDC_TIMESTAMPTYPE);
493    
494     if (LogTypePlainText) {
495     SetRB(Dialog, 1, IDC_PLAINTEXT, IDC_PLAINTEXT);
496 zmatsuo 8852 }
497 nmaya 10144 if (LogTimestamp) {
498     SetRB(Dialog, 1, IDC_TIMESTAMP, IDC_TIMESTAMP);
499     }
500 zmatsuo 8852 else {
501 nmaya 10144 DisableDlgItem(Dialog, IDC_TIMESTAMPTYPE, IDC_TIMESTAMPTYPE);
502 zmatsuo 8852 }
503     }
504 nmaya 10144
505     if (work->file_exist && Append) {
506     // �����t�@�C�����G���R�[�f�B���O�����f����
507     int bom = work->current_bom;
508     int cur =
509     bom == 1 ? 0 :
510     bom == 2 ? 1 :
511     bom == 3 ? 2 : 0;
512     SendDlgItemMessage(Dialog, IDC_TEXTCODING_DROPDOWN, CB_SETCURSEL, cur, 0);
513 maya 3227 }
514 zmatsuo 8852 }
515 maya 3227
516 zmatsuo 8863 static void CheckLogFile(HWND Dialog, const wchar_t *filename, LogDlgWork_t *work)
517     {
518     BOOL exist;
519     int bom;
520     CheckLogFile(filename, &exist, &bom);
521     work->file_exist = exist;
522     work->current_bom = bom;
523     }
524 zmatsuo 8852
525     static INT_PTR CALLBACK LogFnHook(HWND Dialog, UINT Message, WPARAM wParam, LPARAM lParam)
526     {
527     static const DlgTextInfo TextInfos[] = {
528     { 0, "DLG_TABSHEET_TITLE_LOG" },
529 zmatsuo 9885 { IDC_SENDFILE_FILENAME_TITLE, "DLG_FOPT_FILENAME_TITLE" },
530     { IDC_FOPTTEXT, "DLG_FOPT_TEXT" },
531 zmatsuo 8852 { IDC_FOPTBIN, "DLG_FOPT_BINARY" },
532 zmatsuo 9885 { IDC_BOM, "DLG_FOPT_BOM" },
533 zmatsuo 8866 { IDC_APPEND, "DLG_FOPT_APPEND" },
534 zmatsuo 8852 { IDC_PLAINTEXT, "DLG_FOPT_PLAIN" },
535     { IDC_HIDEDIALOG, "DLG_FOPT_HIDEDIALOG" },
536     { IDC_ALLBUFF_INFIRST, "DLG_FOPT_ALLBUFFINFIRST" },
537     { IDC_TIMESTAMP, "DLG_FOPT_TIMESTAMP" },
538 zmatsuo 9885 { IDC_NEW_OVERWRITE, "DLG_FOPT_NEW_OVERWRITE" },
539 zmatsuo 10170 { IDC_APPEND_GROUP, "DLG_FOPT_APPEND_LABEL" },
540     { IDC_BINARY_GROUP, "DLG_FOPT_BINARY_LABEL" },
541 zmatsuo 8852 };
542     static const I18nTextInfo timestamp_list[] = {
543     { "DLG_FOPT_TIMESTAMP_LOCAL", L"Local Time" },
544     { "DLG_FOPT_TIMESTAMP_UTC", L"UTC" },
545     { "DLG_FOPT_TIMESTAMP_ELAPSED_LOGGING", L"Elapsed Time (Logging)" },
546     { "DLG_FOPT_TIMESTAMP_ELAPSED_CONNECTION", L"Elapsed Time (Connection)" },
547     };
548 zmatsuo 8863 LogDlgWork_t *work = (LogDlgWork_t *)GetWindowLongPtr(Dialog, DWLP_USER);
549 zmatsuo 8852
550 nmaya 10144 if (Message == RegisterWindowMessage(HELPMSGSTRING)) {
551 zmatsuo 8852 // �R�����_�C�A���O�������w���v���b�Z�[�W���t��������
552     Message = WM_COMMAND;
553     wParam = IDHELP;
554     }
555     switch (Message) {
556     case WM_INITDIALOG: {
557 zmatsuo 8863 work = (LogDlgWork_t *)lParam;
558     TTTSet *pts = work->pts;
559     SetWindowLongPtr(Dialog, DWLP_USER, (LONG_PTR)work);
560 zmatsuo 8852 ::DragAcceptFiles(Dialog, TRUE);
561    
562 zmatsuo 9359 SetDlgTextsW(Dialog, TextInfos, _countof(TextInfos), pts->UILanguageFileW);
563     SetI18nListW("Tera Term", Dialog, IDC_TIMESTAMPTYPE, timestamp_list, _countof(timestamp_list),
564 nmaya 10144 pts->UILanguageFileW, 0);
565 zmatsuo 8852
566     SendDlgItemMessage(Dialog, IDC_TEXTCODING_DROPDOWN, CB_ADDSTRING, 0, (LPARAM)"UTF-8");
567 zmatsuo 8863 SendDlgItemMessage(Dialog, IDC_TEXTCODING_DROPDOWN, CB_ADDSTRING, 0, (LPARAM)"UTF-16LE");
568     SendDlgItemMessage(Dialog, IDC_TEXTCODING_DROPDOWN, CB_ADDSTRING, 0, (LPARAM)"UTF-16BE");
569 zmatsuo 8852 SendDlgItemMessage(Dialog, IDC_TEXTCODING_DROPDOWN, CB_SETCURSEL, 0, 0);
570    
571 zmatsuo 9324 SetDlgItemTextW(Dialog, IDC_FOPT_FILENAME_EDIT, work->info->filename);
572 zmatsuo 8852
573 nmaya 10144 // timestamp ����
574     int tstype = pts->LogTimestampType == TIMESTAMP_LOCAL ? 0 :
575     pts->LogTimestampType == TIMESTAMP_UTC ? 1 :
576     pts->LogTimestampType == TIMESTAMP_ELAPSED_LOGSTART ? 2 :
577     pts->LogTimestampType == TIMESTAMP_ELAPSED_CONNECTED ? 3 : 0;
578     SendDlgItemMessage(Dialog, IDC_TIMESTAMPTYPE, CB_SETCURSEL, tstype, 0);
579 zmatsuo 8852
580    
581 nmaya 10144 CheckLogFile(Dialog, work->info->filename, work);
582     ArrangeControls(Dialog, work, pts->Append, pts->LogBinary, pts->LogTypePlainText, pts->LogTimestamp);
583     work->info->filename = NULL;
584    
585     // Hide dialog �`�F�b�N�{�b�N�X
586 zmatsuo 8863 if (pts->LogHideDialog) {
587 zmatsuo 8852 SetRB(Dialog, 1, IDC_HIDEDIALOG, IDC_HIDEDIALOG);
588 doda 6946 }
589 zmatsuo 8852
590 nmaya 10144 // Include screen buffer �`�F�b�N�{�b�N�X
591 zmatsuo 8863 if (pts->LogAllBuffIncludedInFirst) {
592 zmatsuo 8852 SetRB(Dialog, 1, IDC_ALLBUFF_INFIRST, IDC_ALLBUFF_INFIRST);
593 doda 6946 }
594 maya 3227
595 zmatsuo 8852 CenterWindow(Dialog, GetParent(Dialog));
596    
597     return TRUE;
598     }
599    
600     case WM_COMMAND:
601     switch (LOWORD(wParam)) {
602     case IDOK: {
603 zmatsuo 9857 wchar_t *filename;
604     hGetDlgItemTextW(Dialog, IDC_FOPT_FILENAME_EDIT, &filename);
605     work->info->filename = filename;
606 zmatsuo 8866 work->info->append = IsDlgButtonChecked(Dialog, IDC_APPEND) == BST_CHECKED;
607 zmatsuo 8863 work->info->bom = IsDlgButtonChecked(Dialog, IDC_BOM) == BST_CHECKED;
608 zmatsuo 8912 work->info->code = (LogCode_t)SendDlgItemMessageA(Dialog, IDC_TEXTCODING_DROPDOWN, CB_GETCURSEL, 0, 0);
609 zmatsuo 8852 SetLogFlags(Dialog);
610     EndDialog(Dialog, IDOK);
611 doda 6947 break;
612 zmatsuo 8852 }
613     case IDCANCEL:
614     EndDialog(Dialog, IDCANCEL);
615 doda 6947 break;
616 zmatsuo 8852 case IDHELP:
617 nmaya 10009 OpenHelpCV(work->pcv, HH_HELP_CONTEXT, HlpFileLog);
618 doda 6947 break;
619 zmatsuo 8852 case IDC_FOPT_FILENAME_BUTTON: {
620     /* save current dir */
621 zmatsuo 9856 const wchar_t *UILanguageFile = work->pts->UILanguageFileW;
622 zmatsuo 9857 wchar_t *curdir;
623     hGetCurrentDirectoryW(&curdir);
624 doda 6947
625 zmatsuo 8863 wchar_t fname[MAX_PATH];
626     GetDlgItemTextW(Dialog, IDC_FOPT_FILENAME_EDIT, fname, _countof(fname));
627 maya 3227
628 zmatsuo 9856 const wchar_t* simple_log_filter = L"*.txt;*.log";
629     wchar_t *FNFilter = GetCommonDialogFilterWW(simple_log_filter, UILanguageFile);
630 maya 3227
631 zmatsuo 9856 wchar_t *caption;
632     wchar_t *uimsg;
633     GetI18nStrWW("Tera Term", "FILEDLG_TRANS_TITLE_LOG",
634     TitLog, UILanguageFile, &uimsg);
635     aswprintf(&caption, L"Tera Term: %s", uimsg);
636     free(uimsg);
637 doda 6947
638 zmatsuo 8863 OPENFILENAMEW ofn = {};
639     ofn.lStructSize = get_OPENFILENAME_SIZEW();
640 zmatsuo 8852 //ofn.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
641     ofn.Flags |= OFN_EXPLORER | OFN_ENABLESIZING;
642     ofn.Flags |= OFN_SHOWHELP;
643     ofn.Flags |= OFN_NOCHANGEDIR; // ��������������������������������
644     ofn.hwndOwner = Dialog;
645     ofn.lpstrFilter = FNFilter;
646     ofn.nFilterIndex = 1;
647     ofn.lpstrFile = fname;
648 zmatsuo 8863 ofn.nMaxFile = _countof(fname);
649 zmatsuo 8852 ofn.lpstrTitle = caption;
650 zmatsuo 9858 ofn.lpstrInitialDir = work->pts->LogDefaultPathW;
651 zmatsuo 9324 BOOL Ok = GetSaveFileNameW(&ofn);
652 zmatsuo 9856 free(caption);
653 zmatsuo 8943 free(FNFilter);
654 zmatsuo 8852 if (Ok) {
655 zmatsuo 8863 SetDlgItemTextW(Dialog, IDC_FOPT_FILENAME_EDIT, fname);
656 doda 6947 }
657 zmatsuo 8852
658     /* restore dir */
659 zmatsuo 9324 SetCurrentDirectoryW(curdir);
660 zmatsuo 9857 free(curdir);
661 zmatsuo 8852
662     break;
663 doda 6947 }
664 nmaya 10144 case IDC_NEW_OVERWRITE:
665     case IDC_APPEND:
666     case IDC_FOPTTEXT:
667 zmatsuo 8852 case IDC_FOPTBIN:
668     case IDC_TIMESTAMP:
669 nmaya 10144 {
670     WORD Appnd, LogBinary, LogTypePlainText, LogTimestamp;
671     GetRB(Dialog, &Appnd, IDC_APPEND, IDC_APPEND);
672     GetRB(Dialog, &LogBinary, IDC_FOPTBIN, IDC_FOPTBIN);
673     GetRB(Dialog, &LogTypePlainText, IDC_PLAINTEXT, IDC_PLAINTEXT);
674     GetRB(Dialog, &LogTimestamp, IDC_TIMESTAMP, IDC_TIMESTAMP);
675     ArrangeControls(Dialog, work, Appnd, LogBinary, LogTypePlainText, LogTimestamp);
676 doda 6947 }
677 zmatsuo 8852 break;
678     case IDC_FOPT_FILENAME_EDIT:
679     if (HIWORD(wParam) == EN_CHANGE){
680 zmatsuo 9857 wchar_t *filename;
681     hGetDlgItemTextW(Dialog, IDC_FOPT_FILENAME_EDIT, &filename);
682 zmatsuo 8863 CheckLogFile(Dialog, filename, work);
683 zmatsuo 9857 free(filename);
684 nmaya 10144 {
685     WORD Appnd, LogBinary, LogTypePlainText, LogTimestamp;
686     GetRB(Dialog, &Appnd, IDC_APPEND, IDC_APPEND);
687     GetRB(Dialog, &LogBinary, IDC_FOPTBIN, IDC_FOPTBIN);
688     GetRB(Dialog, &LogTypePlainText, IDC_PLAINTEXT, IDC_PLAINTEXT);
689     GetRB(Dialog, &LogTimestamp, IDC_TIMESTAMP, IDC_TIMESTAMP);
690     ArrangeControls(Dialog, work, Appnd, LogBinary, LogTypePlainText, LogTimestamp);
691     }
692 zmatsuo 8852 }
693     break;
694 doda 6947 }
695 zmatsuo 8852 break;
696     case WM_DROPFILES: {
697     // �����h���b�v��������������1������������
698     HDROP hDrop = (HDROP)wParam;
699 zmatsuo 9324 const UINT len = DragQueryFileW(hDrop, 0, NULL, 0);
700 zmatsuo 8852 if (len == 0) {
701     DragFinish(hDrop);
702     return TRUE;
703     }
704     wchar_t *filename = (wchar_t *)malloc(sizeof(wchar_t) * (len + 1));
705 zmatsuo 9324 DragQueryFileW(hDrop, 0, filename, len + 1);
706 zmatsuo 8852 filename[len] = '\0';
707 zmatsuo 8863 CheckRadioButton(Dialog, IDC_NEW_OVERWRITE, IDC_APPEND, IDC_APPEND);
708 zmatsuo 9324 SetDlgItemTextW(Dialog, IDC_FOPT_FILENAME_EDIT, filename);
709 zmatsuo 8852 SendDlgItemMessage(Dialog, IDC_FOPT_FILENAME_EDIT, EM_SETSEL, len, len);
710     free(filename);
711     DragFinish(hDrop);
712     return TRUE;
713 maya 3227 }
714 zmatsuo 8852 }
715     return FALSE;
716     }
717 maya 3227
718 zmatsuo 8901 static void OpenLogFile(PFileVar fv)
719     {
720 nmaya 9134 // LogLockExclusive ���L���������������������L���������A
721     // ���������������O�t�@�C���������G�f�B�^���J����������
722 zmatsuo 8901 int dwShareMode = FILE_SHARE_READ;
723     if (!ts.LogLockExclusive) {
724     dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
725     }
726 zmatsuo 9873 fv->FileHandle = CreateFileW(fv->FullName, GENERIC_WRITE, dwShareMode, NULL,
727     OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
728 zmatsuo 8901 }
729    
730 zmatsuo 9873 static BOOL LogStart(PFileVar fv, const wchar_t *fname)
731 zmatsuo 8852 {
732 zmatsuo 8901 fv->FullName = _wcsdup(fname);
733 zmatsuo 8852 FixLogOption();
734    
735 doda 3887 if (ts.LogBinary > 0)
736 maya 3227 {
737 zmatsuo 9872 fv->BinLog = TRUE;
738     fv->FileLog = FALSE;
739 maya 3227 if (! CreateBinBuf())
740     {
741     return FALSE;
742     }
743     }
744     else {
745 zmatsuo 9872 fv->BinLog = FALSE;
746     fv->FileLog = TRUE;
747 maya 3227 if (! CreateLogBuf())
748     {
749     return FALSE;
750     }
751     }
752 zmatsuo 8906 cv_LStart = cv_LogPtr;
753     cv_LCount = 0;
754 maya 3227
755 zmatsuo 8901 OpenLogFile(fv);
756 zmatsuo 9871 if (fv->FileHandle == INVALID_HANDLE_VALUE) {
757 zmatsuo 8901 return FALSE;
758     }
759    
760 maya 3227 /* 2007.05.24 Gentaro */
761 zmatsuo 8901 fv->eLineEnd = Line_LineHead;
762 maya 3227 if (ts.Append > 0)
763     {
764 zmatsuo 9871 SetFilePointer(fv->FileHandle, 0, NULL, FILE_END);
765 zmatsuo 8901 /* 2007.05.24 Gentaro
766     If log file already exists,
767     a newline is inserted before the first timestamp.
768     */
769     fv->eLineEnd = Line_FileHead;
770 maya 3227 }
771 zmatsuo 9869
772     // BOM�o��
773     if (ts.Append == 0 && ts.LogBinary == 0 && fv->bom) {
774     // ���L��������(�V�K) && �o�C�i���������� && BOM ���o����
775 zmatsuo 9873 FLogOutputBOM(fv);
776 zmatsuo 8912 }
777 maya 3227
778 yutakapon 5171 // Log rotate configuration
779 zmatsuo 9871 fv->RotateMode = ts.LogRotate;
780     fv->RotateSize = ts.LogRotateSize;
781     fv->RotateStep = ts.LogRotateStep;
782 yutakapon 5162
783 yutakapon 6365 // Log rotate���L���������A�����t�@�C���T�C�Y�����������B
784     // �������t�@�C�������������T�C�Y�����[�e�[�g�������������C���B
785     // (2016.4.9 yutaka)
786 zmatsuo 9871 if (fv->RotateMode != ROTATE_NONE) {
787     DWORD size = GetFileSize(fv->FileHandle, NULL);
788 zmatsuo 8901 if (size == -1) {
789     return FALSE;
790     }
791 zmatsuo 9871 fv->ByteCount = size;
792 yutakapon 6365 }
793 zmatsuo 8901 else {
794 zmatsuo 9871 fv->ByteCount = 0;
795 zmatsuo 8901 }
796 yutakapon 6365
797 zmatsuo 9871 if (! OpenFTDlg_(fv)) {
798 maya 3227 return FALSE;
799     }
800    
801 zmatsuo 9871 fv->IsPause = FALSE;
802     fv->StartTime = GetTickCount();
803 zmatsuo 8899
804 zmatsuo 8901 if (ts.DeferredLogWriteMode) {
805 zmatsuo 9871 StartThread(fv);
806 yutakapon 6489 }
807 yutakapon 5206
808 zmatsuo 9872 if (fv->FileLog) {
809 zmatsuo 8897 cv.Log1Byte = LogPut1;
810     }
811 zmatsuo 9872 if (fv->BinLog) {
812 zmatsuo 8897 cv.Log1Bin = Log1Bin;
813     cv.LogBinSkip = LogBinSkip;
814     }
815    
816 maya 3227 return TRUE;
817     }
818    
819 zmatsuo 8897 /**
820 zmatsuo 8908 * �����o�b�t�@�������f�[�^�����������O�������o��
821     * (2013.9.29 yutaka)
822     *
823     * TODO
824     * 1�s������
825     */
826     void FLogOutputAllBuffer(void)
827     {
828 zmatsuo 9873 PFileVar fv = LogVar;
829 zmatsuo 8909 DWORD ofs;
830 zmatsuo 8908 int size;
831     wchar_t buf[512];
832     for (ofs = 0 ; ; ofs++ ) {
833     // 1�����s�����������B���������������A�G�X�P�[�v�V�[�P���X�������������B
834     size = BuffGetAnyLineDataW(ofs, buf, _countof(buf));
835     if (size == -1)
836     break;
837    
838     OutputStr(buf);
839     OutputStr(L"\r\n");
840 zmatsuo 9873 LogToFile(fv);
841 zmatsuo 8908 }
842     }
843    
844     /**
845 zmatsuo 8897 * ���O��1byte��������
846     * �o�b�t�@��������������
847     * ���������������� LogToFile() ���s������
848     */
849 maya 3227 void LogPut1(BYTE b)
850     {
851 zmatsuo 9872 PFileVar fv = LogVar;
852    
853 zmatsuo 8906 cv_LogBuf[cv_LogPtr] = b;
854     cv_LogPtr++;
855     if (cv_LogPtr>=InBuffSize)
856     cv_LogPtr = cv_LogPtr-InBuffSize;
857 maya 3227
858 zmatsuo 9872 if (fv->FileLog)
859 maya 3227 {
860 zmatsuo 8906 if (cv_LCount>=InBuffSize)
861 maya 3227 {
862 zmatsuo 8906 cv_LCount = InBuffSize;
863     cv_LStart = cv_LogPtr;
864 maya 3227 }
865     else
866 zmatsuo 8906 cv_LCount++;
867 maya 3227 }
868     else
869 zmatsuo 8906 cv_LCount = 0;
870 maya 3227 }
871    
872     static BOOL Get1(PCHAR Buf, int *Start, int *Count, PBYTE b)
873     {
874     if (*Count<=0) return FALSE;
875     *b = Buf[*Start];
876     (*Start)++;
877     if (*Start>=InBuffSize)
878     *Start = *Start-InBuffSize;
879     (*Count)--;
880     return TRUE;
881     }
882    
883    
884    
885     static CRITICAL_SECTION g_filelog_lock; /* ���b�N�p���� */
886    
887     void logfile_lock_initialize(void)
888     {
889     InitializeCriticalSection(&g_filelog_lock);
890     }
891    
892     static inline void logfile_lock(void)
893     {
894     EnterCriticalSection(&g_filelog_lock);
895     }
896    
897     static inline void logfile_unlock(void)
898     {
899     LeaveCriticalSection(&g_filelog_lock);
900     }
901    
902 yutakapon 5162 // ���O�����[�e�[�g�����B
903     // (2013.3.21 yutaka)
904 zmatsuo 9873 static void LogRotate(PFileVar fv)
905 yutakapon 5162 {
906 yutakapon 5165 int loopmax = 10000; // XXX
907     int i, k;
908 yutakapon 5162
909 zmatsuo 9873 if (fv->RotateMode == ROTATE_NONE)
910 yutakapon 5162 return;
911    
912 zmatsuo 9873 if (fv->RotateMode == ROTATE_SIZE) {
913     if (fv->ByteCount <= fv->RotateSize)
914 yutakapon 5162 return;
915 zmatsuo 9873 //OutputDebugPrintf("%s: mode %d size %ld\n", __FUNCTION__, fv->RotateMode, fv->ByteCount);
916 yutakapon 5162 } else {
917     return;
918     }
919    
920     logfile_lock();
921     // ���O�T�C�Y���������������B
922 zmatsuo 9873 fv->ByteCount = 0;
923 yutakapon 5162
924     // �������������t�@�C�����N���[�Y�����A�������t�@�C�����I�[�v�������B
925 zmatsuo 9873 CloseFileSync(fv);
926 yutakapon 5162
927 yutakapon 5165 // �������[�e�[�V�������X�e�b�v�����w����������
928 zmatsuo 9873 if (fv->RotateStep > 0)
929     loopmax = fv->RotateStep;
930 yutakapon 5165
931     for (i = 1 ; i <= loopmax ; i++) {
932 zmatsuo 8899 wchar_t *filename;
933 zmatsuo 9873 aswprintf(&filename, L"%s.%d", fv->FullName, i);
934 zmatsuo 9324 DWORD attr = GetFileAttributesW(filename);
935 zmatsuo 8899 free(filename);
936 zmatsuo 8901 if (attr == INVALID_FILE_ATTRIBUTES)
937 yutakapon 5162 break;
938     }
939 yutakapon 5165 if (i > loopmax) {
940     // �������������������������A�������t�@�C�������p�������B
941     i = loopmax;
942 yutakapon 5162 }
943    
944     // ���t�@�C�������l�[���B
945 yutakapon 5165 for (k = i-1 ; k >= 0 ; k--) {
946 zmatsuo 8899 wchar_t *oldfile;
947 yutakapon 5165 if (k == 0)
948 zmatsuo 9873 oldfile = _wcsdup(fv->FullName);
949 yutakapon 5165 else
950 zmatsuo 9873 aswprintf(&oldfile, L"%s.%d", fv->FullName, k);
951 zmatsuo 8899 wchar_t *newfile;
952 zmatsuo 9873 aswprintf(&newfile, L"%s.%d", fv->FullName, k+1);
953 zmatsuo 9324 DeleteFileW(newfile);
954     if (MoveFileW(oldfile, newfile) == 0) {
955 yutakapon 5165 OutputDebugPrintf("%s: rename %d\n", __FUNCTION__, errno);
956     }
957 zmatsuo 8899 free(oldfile);
958     free(newfile);
959 yutakapon 5165 }
960 yutakapon 5162
961     // ���I�[�v��
962 zmatsuo 9873 OpenLogFile(fv);
963     if (fv->bom) {
964     FLogOutputBOM(fv);
965 zmatsuo 8912 }
966 zmatsuo 8901 if (ts.DeferredLogWriteMode) {
967 zmatsuo 9873 StartThread(fv);
968 yutakapon 5162 }
969    
970     logfile_unlock();
971     }
972    
973 zmatsuo 9873 static wchar_t *TimeStampStr(PFileVar fv)
974 zmatsuo 8909 {
975     char *strtime = NULL;
976     switch (ts.LogTimestampType) {
977     case TIMESTAMP_LOCAL:
978     default:
979     strtime = mctimelocal(ts.LogTimestampFormat, FALSE);
980     break;
981     case TIMESTAMP_UTC:
982     strtime = mctimelocal(ts.LogTimestampFormat, TRUE);
983     break;
984     case TIMESTAMP_ELAPSED_LOGSTART:
985 zmatsuo 9873 strtime = strelapsed(fv->StartTime);
986 zmatsuo 8909 break;
987     case TIMESTAMP_ELAPSED_CONNECTED:
988     strtime = strelapsed(cv.ConnectedTime);
989     break;
990     }
991    
992     char tmp[128];
993     tmp[0] = 0;
994     strncat_s(tmp, sizeof(tmp), "[", _TRUNCATE);
995     strncat_s(tmp, sizeof(tmp), strtime, _TRUNCATE);
996     strncat_s(tmp, sizeof(tmp), "] ", _TRUNCATE);
997    
998 zmatsuo 8910 return ToWcharA(tmp);
999 zmatsuo 8909 }
1000    
1001 zmatsuo 8897 /**
1002     * �o�b�t�@�������O���t�@�C������������
1003     */
1004 zmatsuo 9873 static void LogToFile(PFileVar fv)
1005 maya 3227 {
1006     PCHAR Buf;
1007     int Start, Count;
1008     BYTE b;
1009    
1010 zmatsuo 9872 if (fv->FileLog)
1011 maya 3227 {
1012 zmatsuo 8906 Buf = cv_LogBuf;
1013     Start = cv_LStart;
1014     Count = cv_LCount;
1015 maya 3227 }
1016 zmatsuo 9872 else if (fv->BinLog)
1017 maya 3227 {
1018 zmatsuo 8906 Buf = cv_BinBuf;
1019     Start = cv_BStart;
1020     Count = cv_BCount;
1021 maya 3227 }
1022     else
1023     return;
1024    
1025     if (Buf==NULL) return;
1026     if (Count==0) return;
1027    
1028     // ���b�N������(2004.8.6 yutaka)
1029     logfile_lock();
1030    
1031 zmatsuo 8909 // ���������f�[�^����������
1032     DWORD WriteBufMax = 8192;
1033     DWORD WriteBufLen = 0;
1034     PCHAR WriteBuf = (PCHAR)malloc(WriteBufMax);
1035     while (Get1(Buf,&Start,&Count,&b)) {
1036 zmatsuo 9103 if (FLogIsPause() || ProtoGetProtoFlag()) {
1037 zmatsuo 8909 continue;
1038     }
1039 doda 6947
1040 zmatsuo 8909 if (WriteBufLen >= (WriteBufMax*4/5)) {
1041     WriteBufMax *= 2;
1042     WriteBuf = (PCHAR)realloc(WriteBuf, WriteBufMax);
1043     }
1044 doda 6947
1045 zmatsuo 8909 WriteBuf[WriteBufLen++] = b;
1046 yutakapon 5206
1047 zmatsuo 9873 (fv->ByteCount)++;
1048 zmatsuo 8909 }
1049 yutakapon 5206
1050 zmatsuo 8909 // ��������
1051     if (WriteBufLen > 0) {
1052     if (ts.DeferredLogWriteMode) {
1053 zmatsuo 9873 PostThreadMessage(fv->LogThreadId, WM_DPC_LOGTHREAD_SEND, (WPARAM)WriteBuf, WriteBufLen);
1054 yutakapon 5206 }
1055 zmatsuo 8909 else {
1056     DWORD wrote;
1057 zmatsuo 9873 WriteFile(fv->FileHandle, WriteBuf, WriteBufLen, &wrote, NULL);
1058 zmatsuo 8909 free(WriteBuf);
1059     }
1060 maya 3227 }
1061    
1062     logfile_unlock();
1063    
1064 zmatsuo 9872 if (fv->FileLog)
1065 maya 3227 {
1066 zmatsuo 8906 cv_LStart = Start;
1067     cv_LCount = Count;
1068 maya 3227 }
1069     else {
1070 zmatsuo 8906 cv_BStart = Start;
1071     cv_BCount = Count;
1072 maya 3227 }
1073 zmatsuo 9103 if (FLogIsPause() || ProtoGetProtoFlag()) return;
1074 zmatsuo 9873 fv->FLogDlg->RefreshNum(fv->StartTime, fv->FileSize, fv->ByteCount);
1075 yutakapon 5162
1076 zmatsuo 8896
1077 yutakapon 5162 // ���O�E���[�e�[�g
1078 zmatsuo 9873 LogRotate(fv);
1079 maya 3227 }
1080    
1081 zmatsuo 8897 static BOOL CreateLogBuf(void)
1082 maya 3227 {
1083 zmatsuo 8906 if (cv_LogBuf==NULL)
1084 maya 3227 {
1085 zmatsuo 8906 cv_LogBuf = (char *)malloc(InBuffSize);
1086     cv_LogPtr = 0;
1087     cv_LStart = 0;
1088     cv_LCount = 0;
1089 maya 3227 }
1090 zmatsuo 8906 return (cv_LogBuf!=NULL);
1091 maya 3227 }
1092    
1093 zmatsuo 8897 static void FreeLogBuf(void)
1094 maya 3227 {
1095 zmatsuo 8906 free(cv_LogBuf);
1096     cv_LogBuf = NULL;
1097     cv_LogPtr = 0;
1098     cv_LStart = 0;
1099     cv_LCount = 0;
1100 maya 3227 }
1101    
1102 zmatsuo 8897 static BOOL CreateBinBuf(void)
1103 maya 3227 {
1104 zmatsuo 8906 if (cv_BinBuf==NULL)
1105 maya 3227 {
1106 zmatsuo 8906 cv_BinBuf = (PCHAR)malloc(InBuffSize);
1107     cv_BinPtr = 0;
1108     cv_BStart = 0;
1109     cv_BCount = 0;
1110 maya 3227 }
1111 zmatsuo 8906 return (cv_BinBuf!=NULL);
1112 maya 3227 }
1113    
1114 zmatsuo 8897 static void FreeBinBuf(void)
1115 maya 3227 {
1116 zmatsuo 8906 free(cv_BinBuf);
1117     cv_BinBuf = NULL;
1118     cv_BinPtr = 0;
1119     cv_BStart = 0;
1120     cv_BCount = 0;
1121 maya 3227 }
1122    
1123 zmatsuo 9873 static void FileTransEnd_(PFileVar fv)
1124 maya 3227 {
1125 zmatsuo 9872 fv->FileLog = FALSE;
1126     fv->BinLog = FALSE;
1127 zmatsuo 8897 cv.Log1Byte = NULL;
1128     cv.Log1Bin = NULL;
1129     cv.LogBinSkip = NULL;
1130 zmatsuo 9873 PFileTransDlg FLogDlg = fv->FLogDlg;
1131 zmatsuo 8901 if (FLogDlg != NULL) {
1132 zmatsuo 8897 FLogDlg->DestroyWindow();
1133     FLogDlg = NULL;
1134 zmatsuo 9873 fv->FLogDlg = NULL;
1135 maya 3227 }
1136 zmatsuo 9873 CloseFileSync(fv);
1137 zmatsuo 8897 FreeLogBuf();
1138     FreeBinBuf();
1139 zmatsuo 9873 free(fv->FullName);
1140     fv->FullName = NULL;
1141     free(fv);
1142    
1143 zmatsuo 8900 LogVar = NULL;
1144 maya 3227 }
1145    
1146 zmatsuo 8857 /**
1147     * ���O���|�[�Y����
1148     */
1149     void FLogPause(BOOL Pause)
1150 maya 3227 {
1151 zmatsuo 9873 PFileVar fv = LogVar;
1152     if (fv == NULL) {
1153 zmatsuo 8899 return;
1154     }
1155 zmatsuo 9873 fv->IsPause = Pause;
1156     fv->FLogDlg->ChangeButton(Pause);
1157 maya 3227 }
1158    
1159 zmatsuo 8857 /**
1160 zmatsuo 8852 * ���O���[�e�[�g������
1161     * ���O���T�C�Y��<size>�o�C�g���������������A���[�e�[�V��������������������
1162     */
1163 zmatsuo 8858 void FLogRotateSize(size_t size)
1164 zmatsuo 8852 {
1165 zmatsuo 9873 PFileVar fv = LogVar;
1166     if (fv == NULL) {
1167 zmatsuo 8852 return;
1168     }
1169 zmatsuo 9873 fv->RotateMode = ROTATE_SIZE;
1170     fv->RotateSize = (LONG)size;
1171 zmatsuo 8852 }
1172    
1173     /**
1174     * ���O���[�e�[�g������
1175     * ���O�t�@�C������������������
1176     */
1177 zmatsuo 8858 void FLogRotateRotate(int step)
1178 zmatsuo 8852 {
1179 zmatsuo 9873 PFileVar fv = LogVar;
1180     if (fv == NULL) {
1181 zmatsuo 8852 return;
1182     }
1183 zmatsuo 9873 fv->RotateStep = step;
1184 zmatsuo 8852 }
1185    
1186     /**
1187     * ���O���[�e�[�g������
1188     * ���[�e�[�V���������~
1189     */
1190 zmatsuo 8858 void FLogRotateHalt(void)
1191 zmatsuo 8852 {
1192 zmatsuo 9873 PFileVar fv = LogVar;
1193     if (fv == NULL) {
1194 zmatsuo 8852 return;
1195     }
1196 zmatsuo 9873 fv->RotateMode = ROTATE_NONE;
1197     fv->RotateSize = 0;
1198     fv->RotateStep = 0;
1199 zmatsuo 8852 }
1200    
1201 zmatsuo 9854 static INT_PTR CALLBACK OnCommentDlgProc(HWND hDlgWnd, UINT msg, WPARAM wp, LPARAM)
1202 zmatsuo 8852 {
1203     static const DlgTextInfo TextInfos[] = {
1204     { 0, "DLG_COMMENT_TITLE" },
1205     { IDOK, "BTN_OK" }
1206     };
1207    
1208     switch (msg) {
1209     case WM_INITDIALOG:
1210     // �G�f�B�b�g�R���g���[�����t�H�[�J�X��������
1211     SetFocus(GetDlgItem(hDlgWnd, IDC_EDIT_COMMENT));
1212 zmatsuo 9350 SetDlgTextsW(hDlgWnd, TextInfos, _countof(TextInfos), ts.UILanguageFileW);
1213 zmatsuo 8852 return FALSE;
1214    
1215     case WM_COMMAND:
1216     switch (LOWORD(wp)) {
1217 zmatsuo 8901 case IDOK: {
1218 zmatsuo 9324 size_t len = SendDlgItemMessageW(hDlgWnd, IDC_EDIT_COMMENT, WM_GETTEXTLENGTH, 0, 0);
1219 zmatsuo 8907 len += 1;
1220     wchar_t *buf = (wchar_t *)malloc(len * sizeof(wchar_t));
1221 zmatsuo 9324 GetDlgItemTextW(hDlgWnd, IDC_EDIT_COMMENT, buf, (int)len);
1222 zmatsuo 8905 FLogWriteStr(buf);
1223     FLogWriteStr(L"\n"); // TODO ���s�R�[�h
1224     free(buf);
1225 zmatsuo 8852 TTEndDialog(hDlgWnd, IDOK);
1226     break;
1227 zmatsuo 8901 }
1228 zmatsuo 8852 default:
1229     return FALSE;
1230     }
1231     break;
1232 zmatsuo 8907
1233 zmatsuo 8852 case WM_CLOSE:
1234     TTEndDialog(hDlgWnd, 0);
1235     return TRUE;
1236    
1237     default:
1238     return FALSE;
1239     }
1240     return TRUE;
1241     }
1242    
1243 zmatsuo 8905 /**
1244     * ���O�t�@�C�����R�����g���������� (2004.8.6 yutaka)
1245     */
1246 zmatsuo 8858 void FLogAddCommentDlg(HINSTANCE hInst, HWND hWnd)
1247 zmatsuo 8852 {
1248 zmatsuo 9873 PFileVar fv = LogVar;
1249     if (fv == NULL) {
1250 zmatsuo 8905 return;
1251     }
1252 zmatsuo 8852 TTDialogBox(hInst, MAKEINTRESOURCE(IDD_COMMENT_DIALOG),
1253 zmatsuo 9854 hWnd, OnCommentDlgProc);
1254 zmatsuo 8852 }
1255    
1256 zmatsuo 8858 void FLogClose(void)
1257 zmatsuo 8852 {
1258 zmatsuo 9873 PFileVar fv = LogVar;
1259     if (fv == NULL) {
1260 zmatsuo 8897 return;
1261     }
1262    
1263 zmatsuo 9873 FileTransEnd_(fv);
1264 zmatsuo 8852 }
1265    
1266 zmatsuo 8897 /**
1267     * ���O���I�[�v������
1268     * @param[in] fname ���O�t�@�C����, CreateFile()���n������
1269     *
1270     * ���O�t�@�C������strftime���W�J�������s���������B
1271     * FLogGetLogFilename() �� FLogOpenDialog() ��
1272     * �t�@�C�����������������B
1273     */
1274 zmatsuo 8912 BOOL FLogOpen(const wchar_t *fname, LogCode_t code, BOOL bom)
1275 zmatsuo 8852 {
1276 zmatsuo 8894 if (LogVar != NULL) {
1277 zmatsuo 8852 return FALSE;
1278     }
1279    
1280 zmatsuo 8897 //
1281 zmatsuo 9873 PFileVar fv = (PFileVar)malloc(sizeof(*fv));
1282 zmatsuo 8897 if (fv == NULL) {
1283     return FALSE;
1284     }
1285     LogVar = fv;
1286     memset(fv, 0, sizeof(TFileVar));
1287 zmatsuo 8901 fv->FileHandle = INVALID_HANDLE_VALUE;
1288     fv->LogThread = INVALID_HANDLE_VALUE;
1289     fv->eLineEnd = Line_LineHead;
1290 zmatsuo 8897
1291 zmatsuo 8912 fv->log_code = code;
1292     fv->bom = bom;
1293 zmatsuo 9873 BOOL ret = LogStart(fv, fname);
1294 zmatsuo 8901 if (ret == FALSE) {
1295 zmatsuo 9873 FileTransEnd_(fv);
1296 zmatsuo 8901 }
1297 zmatsuo 8897
1298 zmatsuo 8852 return ret;
1299     }
1300    
1301 zmatsuo 8858 BOOL FLogIsOpend(void)
1302 zmatsuo 8852 {
1303     return LogVar != NULL;
1304     }
1305    
1306 zmatsuo 8904 BOOL FLogIsOpendText(void)
1307     {
1308 zmatsuo 9872 return LogVar != NULL && LogVar->FileLog;
1309 zmatsuo 8904 }
1310    
1311     BOOL FLogIsOpendBin(void)
1312     {
1313 zmatsuo 9872 return LogVar != NULL && LogVar->BinLog;
1314 zmatsuo 8904 }
1315    
1316 zmatsuo 8905 void FLogWriteStr(const wchar_t *str)
1317 zmatsuo 8852 {
1318 zmatsuo 8905 if (LogVar != NULL) {
1319 zmatsuo 8907 OutputStr(str);
1320 zmatsuo 8852 }
1321     }
1322    
1323 zmatsuo 8858 void FLogInfo(char *param_ptr, size_t param_len)
1324 zmatsuo 8852 {
1325 zmatsuo 9873 PFileVar fv = LogVar;
1326     if (fv) {
1327 zmatsuo 8852 param_ptr[0] = '0'
1328     + (ts.LogBinary != 0)
1329     + ((ts.Append != 0) << 1)
1330     + ((ts.LogTypePlainText != 0) << 2)
1331     + ((ts.LogTimestamp != 0) << 3)
1332     + ((ts.LogHideDialog != 0) << 4);
1333 zmatsuo 9873 char *filenameU8 = ToU8W(fv->FullName);
1334 zmatsuo 8899 strncpy_s(param_ptr + 1, param_len - 1, filenameU8, _TRUNCATE);
1335     free(filenameU8);
1336 zmatsuo 8852 }
1337     else {
1338     param_ptr[0] = '0' - 1;
1339     param_ptr[1] = 0;
1340     }
1341     }
1342    
1343     /**
1344     * ���������O�t�@�C����������
1345     */
1346 zmatsuo 8899 const wchar_t *FLogGetFilename(void)
1347 zmatsuo 8852 {
1348     if (LogVar == NULL) {
1349     return NULL;
1350     }
1351     return LogVar->FullName;
1352     }
1353    
1354     /**
1355     * ���O�_�C�A���O���J��
1356 zmatsuo 8863 * @param[in,out] info.filename �t�@�C���������l
1357     * OK���A�t�@�C�����A�s�v����������free()��������
1358 zmatsuo 8852 * @retval TRUE [ok] ����������
1359     * @retval FALSE �L�����Z��������
1360     */
1361 zmatsuo 8863 BOOL FLogOpenDialog(HINSTANCE hInst, HWND hWnd, FLogDlgInfo_t *info)
1362 zmatsuo 8852 {
1363 zmatsuo 8863 LogDlgWork_t *work = (LogDlgWork_t *)calloc(sizeof(LogDlgWork_t), 1);
1364 zmatsuo 8899 wchar_t *srcfnameW = FLogGetLogFilename(info->filename);
1365 zmatsuo 8863 work->info = info;
1366     work->info->filename = srcfnameW;
1367     work->pts = &ts;
1368 nmaya 10009 work->pcv = &cv;
1369 zmatsuo 8852 INT_PTR ret = TTDialogBoxParam(
1370     hInst, MAKEINTRESOURCE(IDD_LOGDLG),
1371 zmatsuo 8863 hWnd, LogFnHook, (LPARAM)work);
1372     free(srcfnameW);
1373     free(work);
1374 zmatsuo 8852 return ret == IDOK ? TRUE : FALSE;
1375     }
1376    
1377     /**
1378 zmatsuo 10444 * ���O�t�@�C�����p���C�����s��,�t�@�C������������
1379     * - strftime() ���������t�W�J
1380     * - �������������O�t�@�C���t�H���_������
1381     * - �z�X�g��,�|�[�g�����W�J
1382     *
1383     * @param filename �t�@�C����(�p�X����������)
1384     * @return �C�������t�@�C����
1385     */
1386     wchar_t *FLogGetLogFilenameBase(const wchar_t *filename)
1387     {
1388     // �t�@�C�������������o
1389 zmatsuo 10464 const wchar_t *last_path_sep = wcsrchr(filename, L'\\');
1390     wchar_t *format;
1391     if (last_path_sep == NULL) {
1392 zmatsuo 10468 format = _wcsdup(filename);
1393 zmatsuo 10444 }
1394 zmatsuo 10464 else {
1395 zmatsuo 10468 format = _wcsdup(last_path_sep + 1);
1396 zmatsuo 10464 }
1397 zmatsuo 10444
1398     // strftime ���g�p������������������
1399     deleteInvalidStrftimeCharW(format);
1400    
1401 zmatsuo 10464 // ����������0��������?
1402     if (format[0] == 0) {
1403     free(format);
1404 zmatsuo 10468 return _wcsdup(L"");
1405 zmatsuo 10464 }
1406    
1407 zmatsuo 10444 // ��������������
1408     time_t time_local;
1409     time(&time_local);
1410     struct tm tm_local;
1411     localtime_s(&tm_local, &time_local);
1412    
1413     // strftime()������
1414 zmatsuo 10464 // ���������������g��
1415     size_t len = 32;
1416     wchar_t *formated = NULL;
1417     while (1) {
1418     wchar_t *formated_realloc = (wchar_t *)realloc(formated, sizeof(wchar_t) * len);
1419     if (formated_realloc == NULL) {
1420     free(format);
1421     free(formated);
1422 zmatsuo 10468 return _wcsdup(L"");
1423 zmatsuo 10464 }
1424     formated = formated_realloc;
1425     size_t r = wcsftime(formated, len, format, &tm_local);
1426     if (r != 0) {
1427     // �t�H�[�}�b�g������
1428     break;
1429     }
1430     len *= 2;
1431 zmatsuo 10444 }
1432     free(format);
1433    
1434     // �z�X�g������
1435     wchar_t *host = ConvertLognameW(&cv, formated);
1436     free(formated);
1437    
1438     // �t�@�C�������g�p���������������u��
1439     //wchar_t *replaced = replaceInvalidFileNameCharW(host, 0); // ����
1440     wchar_t *replaced = replaceInvalidFileNameCharW(host, L'_');
1441     free(host);
1442    
1443     return replaced;
1444     }
1445    
1446     /**
1447 zmatsuo 8852 * ���O�t�@�C����������
1448     * ���O�t�@�C�����p���C�����s��
1449     * - strftime() ���������t�W�J
1450     * - �������������O�t�@�C���t�H���_������
1451     * - �z�X�g��,�|�[�g�����W�J
1452     *
1453     * @param[in] log_filename �t�@�C����(����/��������������ok)
1454     * NULL�������f�t�H���g�t�@�C����������
1455     * strftime�`��ok
1456     * @return �t���p�X�t�@�C����
1457     * �s�v���������� free() ��������
1458     */
1459 zmatsuo 8899 wchar_t *FLogGetLogFilename(const wchar_t *log_filename)
1460 zmatsuo 8852 {
1461 zmatsuo 10444 wchar_t *dir;
1462     wchar_t *fname;
1463 zmatsuo 8852 if (log_filename == NULL) {
1464 zmatsuo 10468 dir = _wcsdup(ts.LogDefaultPathW);
1465     fname = _wcsdup(ts.LogDefaultNameW);
1466 zmatsuo 10444 } else if (!IsRelativePathW(log_filename)) {
1467     // �����p�X������������
1468     dir = ExtractDirNameW(log_filename);
1469     fname = ExtractFileNameW(log_filename);
1470 zmatsuo 8852 }
1471     else {
1472 zmatsuo 10468 dir = _wcsdup(ts.LogDefaultPathW);
1473     fname = _wcsdup(log_filename);
1474 zmatsuo 8852 }
1475    
1476 zmatsuo 10444 wchar_t *formated = FLogGetLogFilenameBase(fname);
1477     free(fname);
1478 zmatsuo 8852
1479 zmatsuo 10444 // �A������
1480     wchar_t *logfull = NULL;
1481     awcscats(&logfull, dir, L"\\", formated, NULL);
1482 zmatsuo 10606 free(formated);
1483 zmatsuo 10444 free(dir);
1484    
1485     // ���K��
1486     wchar_t *normal;
1487     hGetFullPathNameW(logfull, &normal, NULL);
1488     free(logfull);
1489    
1490     return normal;
1491 zmatsuo 8852 }
1492 zmatsuo 8857
1493     BOOL FLogIsPause()
1494     {
1495 zmatsuo 8900 if (LogVar == NULL) {
1496     return FALSE;
1497     }
1498     return LogVar->IsPause;
1499 zmatsuo 8857 }
1500    
1501     void FLogWindow(int nCmdShow)
1502     {
1503 zmatsuo 8900 if (LogVar == NULL) {
1504     return;
1505     }
1506 zmatsuo 8857
1507 zmatsuo 8901 HWND HWndLog = LogVar->FLogDlg->m_hWnd;
1508 zmatsuo 8857 ShowWindow(HWndLog, nCmdShow);
1509     if (nCmdShow == SW_RESTORE) {
1510     // �g���X�^�C�� WS_EX_NOACTIVATE ��������������
1511     SetForegroundWindow(HWndLog);
1512     }
1513     }
1514    
1515     void FLogShowDlg(void)
1516     {
1517 zmatsuo 8900 if (LogVar == NULL) {
1518     return;
1519     }
1520 zmatsuo 8901 HWND HWndLog = LogVar->FLogDlg->m_hWnd;
1521     ShowWindow(HWndLog, SW_SHOWNORMAL);
1522     SetForegroundWindow(HWndLog);
1523 zmatsuo 8857 }
1524 zmatsuo 8897
1525     /**
1526     * ���O��1byte��������
1527     * LogPut1() ������?
1528     */
1529     //void Log1Bin(PComVar cv, BYTE b)
1530     static void Log1Bin(BYTE b)
1531     {
1532 zmatsuo 9103 if (LogVar->IsPause || ProtoGetProtoFlag()) {
1533 zmatsuo 8897 return;
1534     }
1535 zmatsuo 8906 if (cv_BinSkip > 0) {
1536     cv_BinSkip--;
1537 zmatsuo 8897 return;
1538     }
1539 zmatsuo 8906 cv_BinBuf[cv_BinPtr] = b;
1540     cv_BinPtr++;
1541     if (cv_BinPtr>=InBuffSize) {
1542     cv_BinPtr = cv_BinPtr-InBuffSize;
1543 zmatsuo 8897 }
1544 zmatsuo 8906 if (cv_BCount>=InBuffSize) {
1545     cv_BCount = InBuffSize;
1546     cv_BStart = cv_BinPtr;
1547 zmatsuo 8897 }
1548     else {
1549 zmatsuo 8906 cv_BCount++;
1550 zmatsuo 8897 }
1551     }
1552    
1553     static void LogBinSkip(int add)
1554     {
1555 zmatsuo 8906 if (cv_BinBuf != NULL) {
1556     cv_BinSkip += add;
1557 zmatsuo 8897 }
1558     }
1559    
1560     /**
1561     * ���O�o�b�t�@���������������f�[�^���o�C�g��������
1562     */
1563     int FLogGetCount(void)
1564     {
1565 zmatsuo 9872 PFileVar fv = LogVar;
1566     if (fv == NULL) {
1567     return 0;
1568     }
1569     if (fv->FileLog) {
1570 zmatsuo 8906 return cv_LCount;
1571 zmatsuo 8897 }
1572 zmatsuo 9872 if (fv->BinLog) {
1573 zmatsuo 8906 return cv_BCount;
1574 zmatsuo 8897 }
1575     return 0;
1576     }
1577    
1578     /**
1579 zmatsuo 8906 * ���O�o�b�t�@�������o�C�g��������
1580     */
1581     int FLogGetFreeCount(void)
1582     {
1583 zmatsuo 9872 PFileVar fv = LogVar;
1584     if (fv == NULL) {
1585     return 0;
1586     }
1587     if (fv->FileLog) {
1588 zmatsuo 8906 return InBuffSize - cv_LCount;
1589     }
1590 zmatsuo 9872 if (fv->BinLog) {
1591 zmatsuo 8906 return InBuffSize - cv_BCount;
1592     }
1593     return 0;
1594     }
1595    
1596     /**
1597 zmatsuo 8897 * �o�b�t�@�������O���t�@�C������������
1598     */
1599     void FLogWriteFile(void)
1600     {
1601 zmatsuo 9872 PFileVar fv = LogVar;
1602     if (fv == NULL) {
1603     return;
1604     }
1605 zmatsuo 8906 if (cv_LogBuf!=NULL)
1606 zmatsuo 8897 {
1607 zmatsuo 9872 if (fv->FileLog) {
1608 zmatsuo 9873 LogToFile(fv);
1609 zmatsuo 8897 }
1610     }
1611    
1612 zmatsuo 8906 if (cv_BinBuf!=NULL)
1613 zmatsuo 8897 {
1614 zmatsuo 9872 if (fv->BinLog) {
1615 zmatsuo 9873 LogToFile(fv);
1616 zmatsuo 8897 }
1617     }
1618     }
1619 zmatsuo 8904
1620     void FLogPutUTF32(unsigned int u32)
1621     {
1622     PFileVar fv = LogVar;
1623 zmatsuo 8906 BOOL log_available = (cv_LogBuf != 0);
1624 zmatsuo 8904
1625     if (!log_available) {
1626 zmatsuo 8905 // ���O�����o��������
1627 zmatsuo 8904 return;
1628     }
1629    
1630 zmatsuo 8912 // �s����?(���s���o����������)
1631 zmatsuo 8910 if (ts.LogTimestamp && fv->eLineEnd) {
1632 zmatsuo 8912 // �^�C���X�^���v���o��
1633 zmatsuo 8910 fv->eLineEnd = Line_Other; /* clear endmark*/
1634 zmatsuo 9873 wchar_t* strtime = TimeStampStr(fv);
1635 zmatsuo 8910 FLogWriteStr(strtime);
1636     free(strtime);
1637     }
1638    
1639 zmatsuo 8904 switch(fv->log_code) {
1640 zmatsuo 8912 case LOG_UTF8: {
1641 zmatsuo 8904 // UTF-8
1642     char u8_buf[4];
1643     size_t u8_len = UTF32ToUTF8(u32, u8_buf, _countof(u8_buf));
1644 zmatsuo 9854 for (size_t i = 0; i < u8_len; i++) {
1645 zmatsuo 8904 BYTE b = u8_buf[i];
1646     LogPut1(b);
1647     }
1648     break;
1649     }
1650 zmatsuo 8912 case LOG_UTF16LE:
1651     case LOG_UTF16BE: {
1652 zmatsuo 8904 // UTF-16
1653     wchar_t u16[2];
1654     size_t u16_len = UTF32ToUTF16(u32, u16, _countof(u16));
1655 zmatsuo 9854 for (size_t i = 0; i < u16_len; i++) {
1656 zmatsuo 8912 if (fv->log_code == LOG_UTF16LE) {
1657 zmatsuo 8904 // UTF-16LE
1658     LogPut1(u16[i] & 0xff);
1659     LogPut1((u16[i] >> 8) & 0xff);
1660     }
1661     else {
1662     // UTF-16BE
1663     LogPut1((u16[i] >> 8) & 0xff);
1664     LogPut1(u16[i] & 0xff);
1665     }
1666     }
1667     }
1668     }
1669 zmatsuo 8910
1670     if (u32 == 0x0a) {
1671     fv->eLineEnd = Line_LineHead; /* set endmark*/
1672     }
1673 zmatsuo 8904 }
1674    
1675 zmatsuo 9873 static void FLogOutputBOM(PFileVar fv)
1676 zmatsuo 8904 {
1677 zmatsuo 8910 DWORD wrote;
1678 zmatsuo 8904
1679     switch(fv->log_code) {
1680 zmatsuo 8910 case 0: {
1681 zmatsuo 8904 // UTF-8
1682 zmatsuo 8910 const char *bom = "\xef\xbb\xbf";
1683 zmatsuo 9871 WriteFile(fv->FileHandle, bom, 3, &wrote, NULL);
1684     fv->ByteCount += 3;
1685 zmatsuo 8904 break;
1686 zmatsuo 8910 }
1687     case 1: {
1688 zmatsuo 8904 // UTF-16LE
1689 zmatsuo 8910 const char *bom = "\xff\xfe";
1690 zmatsuo 9871 WriteFile(fv->FileHandle, bom, 2, &wrote, NULL);
1691     fv->ByteCount += 2;
1692 zmatsuo 8904 break;
1693 zmatsuo 8910 }
1694     case 2: {
1695 zmatsuo 8904 // UTF-16BE
1696 zmatsuo 8910 const char *bom = "\xfe\xff";
1697 zmatsuo 9871 WriteFile(fv->FileHandle, bom, 2, &wrote, NULL);
1698     fv->ByteCount += 2;
1699 zmatsuo 8904 break;
1700 zmatsuo 8910 }
1701 zmatsuo 8904 default:
1702     break;
1703     }
1704     }
1705    
1706 zmatsuo 8907 static void OutputStr(const wchar_t *str)
1707     {
1708     size_t len;
1709    
1710     assert(str != NULL);
1711    
1712     len = wcslen(str);
1713     while(*str != 0) {
1714     unsigned int u32;
1715     size_t u16_len = UTF16ToUTF32(str, len, &u32);
1716     switch (u16_len) {
1717     case 0:
1718     default:
1719     // ������������
1720     str++;
1721     len--;
1722     break;
1723     case 1:
1724     case 2: {
1725     FLogPutUTF32(u32);
1726     str += u16_len;
1727     len -= u16_len;
1728     break;
1729     }
1730     }
1731     }
1732     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26