aboutsummaryrefslogtreecommitdiff
path: root/c
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-30 16:04:23 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-30 16:04:23 +0200
commit9003b747e6d330bac33c30018b4575e2ecfd95eb (patch)
tree70ec37ae4d45aeadfe5d80f1a2fb60694aa5e0e5 /c
parent1561f2500fd3f5c2b7d76d8cdf452ea41a56d479 (diff)
downloadEpisodeBrowser-9003b747e6d330bac33c30018b4575e2ecfd95eb.tar.gz
Add debugging functions.
Diffstat (limited to 'c')
-rw-r--r--c/debug.cpp202
-rw-r--r--c/debug.h18
2 files changed, 220 insertions, 0 deletions
diff --git a/c/debug.cpp b/c/debug.cpp
new file mode 100644
index 0000000..e22234a
--- /dev/null
+++ b/c/debug.cpp
@@ -0,0 +1,202 @@
+#include <cstddef>
+#include <windows.h>
+
+#include "common.h"
+#include "debug.h"
+
+struct Avg {
+ int id;
+ int count;
+ long long sum;
+};
+
+static long long llFrequency;
+
+Benchmark::Benchmark(const char* const szName, const int iId, const int iAvgMax)
+ : id(iId), avgmax(iAvgMax), name(szName)
+{
+ if (!llFrequency) {
+ static LARGE_INTEGER liFrequency;
+ if (!QueryPerformanceFrequency(&liFrequency))
+ throw Win32Error();
+ llFrequency = liFrequency.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 ll = liTicks.QuadPart-ticks;
+ ll *= 1'000'000;
+ ll /= llFrequency;
+
+ static Avg aAvg[256] = {0};
+ for (size_t i = 0; i < sizeof(aAvg)/sizeof(*aAvg); i++) {
+ Avg& avg = aAvg[i];
+ if (avg.id == id || !avg.id) {
+ avg.id = id;
+ if (avg.count < avgmax) {
+ avg.count++;
+ avg.sum += ll;
+ } else {
+ avg.count = 1;
+ avg.sum = ll;
+ }
+ printf("%s: %lld (%lld)\n",
+ name, ll, avg.sum/avg.count);
+ break;
+ }
+ }
+}
+
+const char* SzFromUmsg(const unsigned uMsg)
+{
+ static const unsigned aKeyMsg[] = {
+ 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 aValueMsg[] = {
+ "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 szUnknown = "unknown message";
+
+ for (size_t i = 0; i < sizeof(aKeyMsg)/sizeof(*aKeyMsg); i++)
+ if (aKeyMsg[i] == uMsg) {
+ return aValueMsg[i];
+ }
+ return szUnknown;
+}
diff --git a/c/debug.h b/c/debug.h
new file mode 100644
index 0000000..d539573
--- /dev/null
+++ b/c/debug.h
@@ -0,0 +1,18 @@
+#ifndef DEBUG_H
+#define DEBUG_H
+
+struct Benchmark
+{
+ Benchmark(const char* szName, int iId, int iAvgMax);
+ ~Benchmark();
+ void Disable();
+ long long ticks;
+ bool disabled = false;
+ int id;
+ int avgmax;
+ const char* name;
+};
+
+const char* SzFromUmsg(unsigned uMsg);
+
+#endif