-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
410 lines (365 loc) · 9.08 KB
/
log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//
// AYA version 5
//
// ロギング用クラス CLog
// written by umeici. 2004
//
#ifndef POSIX
# include "stdafx.h"
#else
# include "posix_utils.h"
# include "messages.h"
# include <sys/time.h>
# include <iostream>
#endif
#include "basis.h"
#include "comment.h"
#include "wsex.h"
#include "misc.h"
#include "log.h"
#include "ccct.h"
/* -----------------------------------------------------------------------
* 関数名 : CLog::Start
* 機能概要: ロギングを開始します
* -----------------------------------------------------------------------
*/
#ifndef POSIX
void CLog::Start(const wstring &p, int cs, int ml, HWND hw, char il)
#else
void CLog::Start(const wstring &p, int cs, int ml, char il)
#endif
{
path = p;
charset = cs;
msglang = ml;
#ifndef POSIX
hWnd = hw;
#endif
iolog = il;
#ifdef POSIX
fix_filepath(path);
#endif
#ifndef POSIX
// もしhWndがNULLなら起動中のチェックツールを探して取得する
if (hWnd == NULL)
hWnd = GetCheckerWnd();
#endif
// ロギング有効/無効の判定
if (!path.size()) {
fileen = 0;
#ifndef POSIX
if (hWnd == NULL) {
enable = 0;
return;
}
#else
enable = 0;
return;
#endif
}
// 文字列作成
wstring str = (msglang) ? msge[0] : msgj[0];
str += GetDateString();
str += L"\n\n";
// ファイルへ書き込み
if (fileen) {
char *tmpstr = Ccct::Ucs2ToMbcs(str, charset);
if (tmpstr != NULL) {
FILE *fp = w_fopen((wchar_t *)path.c_str(), L"w");
if (fp != NULL) {
/* if (charset == CHARSET_UTF8)
write_utf8bom(fp);*/
fprintf(fp, "%s", tmpstr);
fclose(fp);
}
free(tmpstr);
}
}
#ifndef POSIX
// チェックツールへ送出 最初に文字コードを設定してから文字列を送出
if (charset == CHARSET_SJIS)
SendLogToWnd(L"", E_SJIS);
else if (charset == CHARSET_UTF8)
SendLogToWnd(L"", E_UTF8);
else // CHARSET_DEFAULT
SendLogToWnd(L"", E_DEFAULT);
SendLogToWnd(str, E_I);
#endif
}
/* -----------------------------------------------------------------------
* 関数名 : CLog::Termination
* 機能概要: ロギングを終了します
* -----------------------------------------------------------------------
*/
void CLog::Termination(void)
{
if (!enable)
return;
Message(1);
wstring str = GetDateString();
str += L"\n\n";
Write(str);
#ifndef POSIX
SendLogToWnd(L"", E_END);
#endif
}
/* -----------------------------------------------------------------------
* 関数名 : CLog::Write
* 機能概要: ログに文字列を書き込みます
* -----------------------------------------------------------------------
*/
void CLog::Write(const wstring &str, int mode)
{
if (!enable)
return;
// 文字列中の\rは消す
wstring cstr = str;
int len = cstr.size();
for(int i = 0; i < len; ) {
if (cstr[i] == L'\r') {
cstr.erase(i, 1);
len--;
continue;
}
i++;
}
// ファイルへ書き込み
if (fileen) {
char *tmpstr = Ccct::Ucs2ToMbcs(cstr, charset);
if (tmpstr != NULL) {
FILE *fp = w_fopen((wchar_t *)path.c_str(), L"a");
if (fp != NULL) {
fprintf(fp, "%s", tmpstr);
fclose(fp);
}
free(tmpstr);
}
}
#ifndef POSIX
// チェックツールへ送出
SendLogToWnd(cstr, mode);
#endif
}
//----
void CLog::Write(const wchar_t *str, int mode)
{
if (str == NULL)
return;
if (!wcslen(str))
return;
wstring lstr = str;
Write(lstr, mode);
}
/* -----------------------------------------------------------------------
* 関数名 : CLog::Filename
* 機能概要: 既定のフォーマットでファイル名をログに記録します
* -----------------------------------------------------------------------
*/
void CLog::Filename(const wstring &filename)
{
wstring str = L"- ";
str += filename;
str += L"\n";
Write(str);
}
/* -----------------------------------------------------------------------
* 関数名 : CLog::Message
* 機能概要: idで指定された既定のメッセージをログに書き込みます
* -----------------------------------------------------------------------
*/
void CLog::Message(int id)
{
Write((msglang) ? (wchar_t *)msge[id] : (wchar_t *)msgj[id], 0);
}
/* -----------------------------------------------------------------------
* 関数名 : CLog::Error
* 機能概要: ログにmodeとidで指定されたエラー文字列を書き込みます
*
* 引数 : ref 付加情報
* dicfilename エラーを起こした箇所を含む辞書ファイルの名前
* linecount エラーを起こした行番号
*
* refとdicfilenameはNULL、linecountは-1とすることで、これらを
* 非表示にできます
* -----------------------------------------------------------------------
*/
void CLog::Error(int mode, int id, const wchar_t *ref, const wstring &dicfilename, int linecount)
{
if (!enable)
return;
// ログに書き込み文字列を作成(辞書ファイル名と行番号)
wstring logstr;
if (dicfilename.empty())
logstr = L"-(-) : ";
else {
logstr = dicfilename + L"(";
if (linecount == -1)
logstr += L"-) : ";
else {
wstring lcstr;
ws_itoa(lcstr, linecount, 10);
logstr += lcstr;
logstr += L") : ";
}
}
// ログに書き込み文字列を作成(本文)
if (msglang) {
// 英語
if (mode == E_F)
logstr += msgfe[id];
else if (mode == E_E)
logstr += msgee[id];
else if (mode == E_W)
logstr += msgwe[id];
else
logstr += msgne[id];
}
else {
// 日本語
if (mode == E_F)
logstr += msgfj[id];
else if (mode == E_E)
logstr += msgej[id];
else if (mode == E_W)
logstr += msgwj[id];
else
logstr += msgnj[id];
}
// ログに書き込み文字列を作成(付加情報)
if (ref != NULL) {
logstr += L" : ";
logstr += ref;
}
// 書き込み
logstr += L'\n';
Write(logstr, mode);
}
//----
void CLog::Error(int mode, int id, const wstring& ref, const wstring& dicfilename, int linecount)
{
Error(mode, id, (wchar_t *)ref.c_str(), dicfilename, linecount);
}
//----
void CLog::Error(int mode, int id, const wchar_t *ref)
{
Error(mode, id, ref, wstring(), -1);
}
//----
void CLog::Error(int mode, int id, const wstring& ref)
{
Error(mode, id, (wchar_t *)ref.c_str(), wstring(), -1);
}
//----
void CLog::Error(int mode, int id, const wstring& dicfilename, int linecount)
{
Error(mode, id, (wchar_t *)NULL, dicfilename, linecount);
}
//----
void CLog::Error(int mode, int id)
{
Error(mode, id, (wchar_t *)NULL, wstring(), -1);
}
/* -----------------------------------------------------------------------
* 関数名 : CLog::Io
* 機能概要: 入出力文字列と実行時間をログに記録します
* 引数 : io 0/1=開始時/終了時
* -----------------------------------------------------------------------
*/
void CLog::Io(char io, const wstring &str)
{
if (!enable || !iolog)
return;
static int starttime = 0;
static wchar_t t_str[STRMAX];
if (!io) {
Write(L"// request\n");
Write(str + L"\n");
#ifndef POSIX
starttime = GetTickCount();
#else
struct timeval tv;
gettimeofday(&tv, NULL);
starttime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif
}
else {
#ifndef POSIX
swprintf(t_str, L"// response (Execution time : %d[ms])\n", GetTickCount() - starttime);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
swprintf(t_str, STRMAX, L"// response (Execution time : %d[ms])\n", tv.tv_sec * 1000 + tv.tv_usec / 1000 - starttime);
#endif
Write(t_str);
Write(str + L"\n");
}
}
/* -----------------------------------------------------------------------
* 関数名 : CLog::IoLib
* 機能概要: 外部ライブラリ入出力文字列と実行時間をログに記録します
* 引数 : io 0/1=開始時/終了時
* -----------------------------------------------------------------------
*/
void CLog::IoLib(char io, const wstring &str, const wstring &name)
{
if (!enable || !iolog)
return;
static int starttime = 0;
static wchar_t t_str[STRMAX];
if (!io) {
Write(L"// request to library\n// name : ");
Write(name + L"\n");
Write(str + L"\n");
#ifndef POSIX
starttime = GetTickCount();
#else
struct timeval tv;
gettimeofday(&tv, NULL);
starttime = tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif
}
else {
#ifndef POSIX
swprintf(t_str, L"// response from library (Execution time : %d[ms])\n", GetTickCount() - starttime);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
swprintf(t_str, STRMAX, L"// response from library (Execution time : %d[ms])\n", tv.tv_sec * 1000 + tv.tv_usec / 1000 - starttime);
#endif
Write(t_str);
Write(str + L"\n");
}
}
/* -----------------------------------------------------------------------
* 関数名 : CLog::SendLogToWnd
* 機能概要: チェックツールに制御メッセージおよびログ文字列をWM_COPYDATAで送信します
* -----------------------------------------------------------------------
*/
#ifndef POSIX
void CLog::SendLogToWnd(const wchar_t *str, int mode)
{
if (hWnd == NULL)
return;
COPYDATASTRUCT cds;
cds.dwData = mode;
cds.cbData = (wcslen(str) + 1)*sizeof(wchar_t);
cds.lpData = (LPVOID)str;
::SendMessage(hWnd, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&cds);
}
//----
void CLog::SendLogToWnd(const wstring &str, int mode)
{
SendLogToWnd((wchar_t *)str.c_str(), mode);
}
#endif
/* -----------------------------------------------------------------------
* 関数名 : CLog::GetCheckerWnd
* 機能概要: チェックツールのhWndを取得しますに
* -----------------------------------------------------------------------
*/
#ifndef POSIX
HWND CLog::GetCheckerWnd(void)
{
return FindWindow(CLASSNAME_CHECKTOOL, NULL);
}
#endif