aboutsummaryrefslogtreecommitdiff
path: root/c/debug.cpp
blob: 07006a9bb5f071e46eb780787e2c280b06e81677 (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
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
#include <cstddef>
#include <windows.h>

#include "common.h"
#include "debug.h"

struct Avg {
	int id;
	int count;
	long long sum;
};

static long long freq;

Benchmark::Benchmark(const char* const name, const int id, const int avgmax)
	: id(id), avgmax(avgmax), name(name)
{
	if (!freq) {
		static LARGE_INTEGER liFreq;
		if (!QueryPerformanceFrequency(&liFreq))
			throw Win32Error();
		freq = liFreq.QuadPart;
	}

	LARGE_INTEGER liTicks;
	if (!QueryPerformanceCounter(&liTicks))
		throw Win32Error();
	ticks = liTicks.QuadPart;
}

void Benchmark::Disable()
{
	disabled = true;
}

Benchmark::~Benchmark()
{
	if (disabled)
		return;

	LARGE_INTEGER liTicks;
	if (!QueryPerformanceCounter(&liTicks))
		return;

	long long dif = liTicks.QuadPart-ticks;
	dif *= 1'000'000;
	dif /= freq;

	static Avg avgs[256] = {0};
	for (size_t i = 0; i < sizeof(avgs)/sizeof(*avgs); i++) {
		Avg& avg = avgs[i];
		if (avg.id == id || !avg.id) {
			avg.id = id;
			if (avg.count < avgmax) {
				avg.count++;
				avg.sum += dif;
			} else {
				avg.count = 1;
				avg.sum = dif;
			}
			printf("%s: %lld (%lld)\n",
			    name, dif, avg.sum/avg.count);
			break;
		}
	}
}

const char* MsgName(const unsigned uMsg)
{
	static const unsigned vKey[] = {
		0, 1, 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18,
		19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
		35, 36, 38, 39, 40, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
		55, 57, 61, 65, 68, 70, 71, 72, 73, 74, 75, 78, 80, 81, 82,
		83, 84, 85, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
		133, 134, 135, 136, 160, 161, 162, 163, 164, 165, 166, 167,
		168, 169, 171, 172, 173, 176, 177, 178, 179, 180, 181, 182,
		183, 185, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196,
		197, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
		209, 209, 210, 211, 212, 213, 214, 215, 216, 217, 224, 225,
		226, 227, 228, 230, 233, 234, 235, 240, 241, 242, 243, 244,
		245, 246, 247, 248, 255, 256, 256, 257, 258, 259, 260, 261,
		262, 263, 264, 265, 265, 266, 267, 268, 269, 270, 271, 271,
		272, 273, 274, 275, 276, 277, 278, 279, 280, 287, 288, 289,
		290, 291, 292, 293, 294, 295, 296, 297, 306, 307, 308, 309,
		310, 311, 312, 512, 512, 513, 514, 515, 516, 517, 518, 519,
		520, 521, 521, 522, 523, 524, 525, 528, 529, 530, 531, 532,
		533, 534, 536, 537, 544, 545, 546, 547, 548, 549, 550, 551,
		552, 553, 560, 561, 562, 563, 564, 640, 641, 642, 643, 644,
		645, 646, 648, 656, 656, 657, 657, 672, 673, 674, 675, 768,
		769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780,
		781, 782, 783, 784, 785, 786, 791, 792, 793, 856, 863, 864,
		895, 896, 897, 898, 899, 899, 900, 901, 901, 902, 903, 904,
		911, 1024
	};

	static const char* const vValue[] = {
		"WM_NUL", "WM_CREATE", "WM_DESTROY", "WM_MOVE", "WM_SIZE",
		"WM_ACTIVATE", "WM_SETFOCUS", "WM_KILLFOCUS", "WM_ENABLE",
		"WM_SETREDRAW", "WM_SETTEXT", "WM_GETTEXT",
		"WM_GETTEXTLENGTH", "WM_PAINT", "WM_CLOSE",
		"WM_QUERYENDSESSION", "WM_QUIT", "WM_QUERYOPEN",
		"WM_ERASEBKGND", "WM_SYSCOLORCHANGE", "WM_ENDSESSION",
		"WM_SHOWWINDOW", "WM_CTLCOLOR", "WM_WININICHANGE",
		"WM_DEVMODECHANGE", "WM_ACTIVATEAPP", "WM_FONTCHANGE",
		"WM_TIMECHANGE", "WM_CANCELMODE", "WM_SETCURSOR",
		"WM_MOUSEACTIVATE", "WM_CHILDACTIVATE", "WM_QUEUESYNC",
		"WM_GETMINMAXINFO", "WM_PAINTICON", "WM_ICONERASEBKGND",
		"WM_NEXTDLGCT", "WM_SPOOLERSTATUS", "WM_DRAWITEM",
		"WM_MEASUREITEM", "WM_DELETEITEM", "WM_VKEYTOITEM",
		"WM_CHARTOITEM", "WM_SETFONT", "WM_GETFONT", "WM_SETHOTKEY",
		"WM_GETHOTKEY", "WM_QUERYDRAGICON", "WM_COMPAREITEM",
		"WM_GETOBJECT", "WM_COMPACTING", "WM_COMMNOTIFY",
		"WM_WINDOWPOSCHANGING", "WM_WINDOWPOSCHANGED", "WM_POWER",
		"WM_COPYGLOBALDATA", "WM_COPYDATA", "WM_CANCELJOURNA",
		"WM_NOTIFY", "WM_INPUTLANGCHANGEREQUEST",
		"WM_INPUTLANGCHANGE", "WM_TCARD", "WM_HELP", "WM_USERCHANGED",
		"WM_NOTIFYFORMAT", "WM_CONTEXTMENU", "WM_STYLECHANGING",
		"WM_STYLECHANGED", "WM_DISPLAYCHANGE", "WM_GETICON",
		"WM_SETICON", "WM_NCCREATE", "WM_NCDESTROY", "WM_NCCALCSIZE",
		"WM_NCHITTEST", "WM_NCPAINT", "WM_NCACTIVATE",
		"WM_GETDLGCODE", "WM_SYNCPAINT", "WM_NCMOUSEMOVE",
		"WM_NCLBUTTONDOWN", "WM_NCLBUTTONUP", "WM_NCLBUTTONDBLCLK",
		"WM_NCRBUTTONDOWN", "WM_NCRBUTTONUP", "WM_NCRBUTTONDBLCLK",
		"WM_NCMBUTTONDOWN", "WM_NCMBUTTONUP", "WM_NCMBUTTONDBLCLK",
		"WM_NCXBUTTONDOWN", "WM_NCXBUTTONUP", "WM_NCXBUTTONDBLCLK",
		"EM_GETSE", "EM_SETSE", "EM_GETRECT", "EM_SETRECT",
		"EM_SETRECTNP", "EM_SCROL", "EM_LINESCROL", "EM_SCROLLCARET",
		"EM_GETMODIFY", "EM_SETMODIFY", "EM_GETLINECOUNT",
		"EM_LINEINDEX", "EM_SETHANDLE", "EM_GETHANDLE", "EM_GETTHUMB",
		"EM_LINELENGTH", "EM_REPLACESE", "EM_SETFONT", "EM_GETLINE",
		"EM_LIMITTEXT", "EM_SETLIMITTEXT", "EM_CANUNDO", "EM_UNDO",
		"EM_FMTLINES", "EM_LINEFROMCHAR", "EM_SETWORDBREAK",
		"EM_SETTABSTOPS", "EM_SETPASSWORDCHAR", "EM_EMPTYUNDOBUFFER",
		"EM_GETFIRSTVISIBLELINE", "EM_SETREADONLY",
		"EM_SETWORDBREAKPROC", "EM_GETWORDBREAKPROC",
		"EM_GETPASSWORDCHAR", "EM_SETMARGINS", "EM_GETMARGINS",
		"EM_GETLIMITTEXT", "EM_POSFROMCHAR", "EM_CHARFROMPOS",
		"EM_SETIMESTATUS", "EM_GETIMESTATUS", "SBM_SETPOS",
		"SBM_GETPOS", "SBM_SETRANGE", "SBM_GETRANGE",
		"SBM_ENABLE_ARROWS", "SBM_SETRANGEREDRAW",
		"SBM_SETSCROLLINFO", "SBM_GETSCROLLINFO",
		"SBM_GETSCROLLBARINFO", "BM_GETCHECK", "BM_SETCHECK",
		"BM_GETSTATE", "BM_SETSTATE", "BM_SETSTYLE", "BM_CLICK",
		"BM_GETIMAGE", "BM_SETIMAGE", "BM_SETDONTCLICK", "WM_INPUT",
		"WM_KEYDOWN", "WM_KEYFIRST", "WM_KEYUP", "WM_CHAR",
		"WM_DEADCHAR", "WM_SYSKEYDOWN", "WM_SYSKEYUP", "WM_SYSCHAR",
		"WM_SYSDEADCHAR", "WM_KEYLAST", "WM_UNICHAR",
		"WM_WNT_CONVERTREQUESTEX", "WM_CONVERTREQUEST",
		"WM_CONVERTRESULT", "WM_INTERIM", "WM_IME_STARTCOMPOSITION",
		"WM_IME_ENDCOMPOSITION", "WM_IME_COMPOSITION",
		"WM_IME_KEYLAST", "WM_INITDIALOG", "WM_COMMAND",
		"WM_SYSCOMMAND", "WM_TIMER", "WM_HSCROL", "WM_VSCROL",
		"WM_INITMENU", "WM_INITMENUPOPUP", "WM_SYSTIMER",
		"WM_MENUSELECT", "WM_MENUCHAR", "WM_ENTERIDLE",
		"WM_MENURBUTTONUP", "WM_MENUDRAG", "WM_MENUGETOBJECT",
		"WM_UNINITMENUPOPUP", "WM_MENUCOMMAND", "WM_CHANGEUISTATE",
		"WM_UPDATEUISTATE", "WM_QUERYUISTATE", "WM_CTLCOLORMSGBOX",
		"WM_CTLCOLOREDIT", "WM_CTLCOLORLISTBOX", "WM_CTLCOLORBTN",
		"WM_CTLCOLORDLG", "WM_CTLCOLORSCROLLBAR", "WM_CTLCOLORSTATIC",
		"WM_MOUSEFIRST", "WM_MOUSEMOVE", "WM_LBUTTONDOWN",
		"WM_LBUTTONUP", "WM_LBUTTONDBLCLK", "WM_RBUTTONDOWN",
		"WM_RBUTTONUP", "WM_RBUTTONDBLCLK", "WM_MBUTTONDOWN",
		"WM_MBUTTONUP", "WM_MBUTTONDBLCLK", "WM_MOUSELAST",
		"WM_MOUSEWHEE", "WM_XBUTTONDOWN", "WM_XBUTTONUP",
		"WM_XBUTTONDBLCLK", "WM_PARENTNOTIFY", "WM_ENTERMENULOOP",
		"WM_EXITMENULOOP", "WM_NEXTMENU", "WM_SIZING",
		"WM_CAPTURECHANGED", "WM_MOVING", "WM_POWERBROADCAST",
		"WM_DEVICECHANGE", "WM_MDICREATE", "WM_MDIDESTROY",
		"WM_MDIACTIVATE", "WM_MDIRESTORE", "WM_MDINEXT",
		"WM_MDIMAXIMIZE", "WM_MDITILE", "WM_MDICASCADE",
		"WM_MDIICONARRANGE", "WM_MDIGETACTIVE", "WM_MDISETMENU",
		"WM_ENTERSIZEMOVE", "WM_EXITSIZEMOVE", "WM_DROPFILES",
		"WM_MDIREFRESHMENU", "WM_IME_REPORT", "WM_IME_SETCONTEXT",
		"WM_IME_NOTIFY", "WM_IME_CONTRO", "WM_IME_COMPOSITIONFUL",
		"WM_IME_SELECT", "WM_IME_CHAR", "WM_IME_REQUEST",
		"WM_IMEKEYDOWN", "WM_IME_KEYDOWN", "WM_IMEKEYUP",
		"WM_IME_KEYUP", "WM_NCMOUSEHOVER", "WM_MOUSEHOVER",
		"WM_NCMOUSELEAVE", "WM_MOUSELEAVE", "WM_CUT", "WM_COPY",
		"WM_PASTE", "WM_CLEAR", "WM_UNDO", "WM_RENDERFORMAT",
		"WM_RENDERALLFORMATS", "WM_DESTROYCLIPBOARD",
		"WM_DRAWCLIPBOARD", "WM_PAINTCLIPBOARD",
		"WM_VSCROLLCLIPBOARD", "WM_SIZECLIPBOARD",
		"WM_ASKCBFORMATNAME", "WM_CHANGECBCHAIN",
		"WM_HSCROLLCLIPBOARD", "WM_QUERYNEWPALETTE",
		"WM_PALETTEISCHANGING", "WM_PALETTECHANGED", "WM_HOTKEY",
		"WM_PRINT", "WM_PRINTCLIENT", "WM_APPCOMMAND",
		"WM_HANDHELDFIRST", "WM_HANDHELDLAST", "WM_AFXFIRST",
		"WM_AFXLAST", "WM_PENWINFIRST", "WM_RCRESULT",
		"WM_HOOKRCRESULT", "WM_GLOBALRCCHANGE", "WM_PENMISCINFO",
		"WM_SKB", "WM_HEDITCT", "WM_PENCT", "WM_PENMISC",
		"WM_CTLINIT", "WM_PENEVENT", "WM_PENWINLAST", "WM_USER"
	};

	static const char* const unknown = "unknown message";

	for (size_t i = 0; i < sizeof(vKey)/sizeof(*vKey); i++)
		if (vKey[i] == uMsg) {
			return vValue[i];
		}
	return unknown;
}