summaryrefslogtreecommitdiff
path: root/plugins/YAMN/src/debug.cpp
blob: e074390facd7a9184e44bbe6043cfb172f961599 (plain)
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
/*
 * YAMN plugin main file
 * Miranda homepage: http://miranda-icq.sourceforge.net/
 *
 * Debug functions used in DEBUG release (you need to global #define DEBUG to get debug version)
 *
 * (c) majvan 2002-2004
 */

#include "stdafx.h"

#ifdef _DEBUG

//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------

TCHAR DebugUserDirectory[MAX_PATH] = _T(".");
CRITICAL_SECTION FileAccessCS;

#ifdef DEBUG_SYNCHRO
TCHAR DebugSynchroFileName2[]=_T("%s\\yamn-debug.synchro.log");
HANDLE SynchroFile;
#endif

#ifdef DEBUG_COMM
TCHAR DebugCommFileName2[]=_T("%s\\yamn-debug.comm.log");
HANDLE CommFile;
#endif

#ifdef DEBUG_DECODE
TCHAR DebugDecodeFileName2[]=_T("%s\\yamn-debug.decode.log");
HANDLE DecodeFile;
#endif

//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------

void InitDebug()
{
#if defined (DEBUG_SYNCHRO) || defined (DEBUG_COMM) || defined (DEBUG_DECODE)
	TCHAR DebugFileName[MAX_PATH];
#endif
	InitializeCriticalSection(&FileAccessCS);

#ifdef DEBUG_SYNCHRO
	mir_sntprintf(DebugFileName, DebugSynchroFileName2, DebugUserDirectory);
	
	SynchroFile=CreateFile(DebugFileName,GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,CREATE_ALWAYS,0,NULL);
	DebugLog(SynchroFile,"Synchro debug file created by %s\n",YAMN_VER);
#endif

#ifdef DEBUG_COMM
	mir_sntprintf(DebugFileName, DebugCommFileName2, DebugUserDirectory);

	CommFile=CreateFile(DebugFileName,GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,CREATE_ALWAYS,0,NULL);
	DebugLog(CommFile,"Communication debug file created by %s\n",YAMN_VER);
#endif

#ifdef DEBUG_DECODE
	mir_sntprintf(DebugFileName, DebugDecodeFileName2, DebugUserDirectory);

	DecodeFile=CreateFile(DebugFileName,GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,CREATE_ALWAYS,0,NULL);
	DebugLog(DecodeFile,"Decoding kernel debug file created by %s\n",YAMN_VER);
#endif
}

void UnInitDebug()
{
	DeleteCriticalSection(&FileAccessCS);
#ifdef DEBUG_SYNCHRO
	DebugLog(SynchroFile,"File is being closed normally.");
	CloseHandle(SynchroFile);
#endif
#ifdef DEBUG_COMM
	DebugLog(CommFile,"File is being closed normally.");
	CloseHandle(CommFile);
#endif
#ifdef DEBUG_DECODE
	DebugLog(DecodeFile,"File is being closed normally.");
	CloseHandle(DecodeFile);
#endif
}


void DebugLog(HANDLE File,const char *fmt,...)
{
	char *str;
	char tids[32];
	va_list vararg;
	int strsize;
	DWORD Written;

	va_start(vararg,fmt);
	str=(char *)malloc(strsize=65536);
	mir_snprintf(tids, "[%x]",GetCurrentThreadId());
	while(mir_vsnprintf(str, strsize, fmt, vararg)==-1)
		str=(char *)realloc(str,strsize+=65536);
	va_end(vararg);
	EnterCriticalSection(&FileAccessCS);
	WriteFile(File,tids,(DWORD)mir_strlen(tids),&Written,NULL);
	WriteFile(File,str,(DWORD)mir_strlen(str),&Written,NULL);
	LeaveCriticalSection(&FileAccessCS);
	free(str);
}

void DebugLogW(HANDLE File,const WCHAR *fmt,...)
{
	WCHAR *str;
	char tids[32];
	va_list vararg;
	int strsize;
	DWORD Written;

	va_start(vararg,fmt);
	str=(WCHAR *)malloc((strsize=65536)*sizeof(WCHAR));
	mir_snprintf(tids, "[%x]",GetCurrentThreadId());
	while(mir_vsnwprintf(str, strsize, fmt, vararg)==-1)
		str=(WCHAR *)realloc(str,(strsize+=65536)*sizeof(WCHAR));
	va_end(vararg);
	EnterCriticalSection(&FileAccessCS);
	WriteFile(File,tids,(DWORD)mir_strlen(tids),&Written,NULL);
	WriteFile(File,str,(DWORD)mir_wstrlen(str)*sizeof(WCHAR),&Written,NULL);
	LeaveCriticalSection(&FileAccessCS);
	free(str);
}

#endif	//ifdef DEBUG