summaryrefslogtreecommitdiff
path: root/Editor/Utils
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-17 11:14:00 +0800
committerchai <chaifix@163.com>2021-10-17 11:14:00 +0800
commitd35db57d457132dd9d83fa2bd51491b148530655 (patch)
treeb5a29675f091f268577b5991988c723f273b0bd1 /Editor/Utils
parent7b0a6d1fe0117cf42a5776aaabda2db78599e5b8 (diff)
*GUI
Diffstat (limited to 'Editor/Utils')
-rw-r--r--Editor/Utils/HelperFuncs.cpp136
-rw-r--r--Editor/Utils/HelperFuncs.h8
-rw-r--r--Editor/Utils/Log.cpp94
-rw-r--r--Editor/Utils/Log.h22
4 files changed, 260 insertions, 0 deletions
diff --git a/Editor/Utils/HelperFuncs.cpp b/Editor/Utils/HelperFuncs.cpp
new file mode 100644
index 0000000..fd91d85
--- /dev/null
+++ b/Editor/Utils/HelperFuncs.cpp
@@ -0,0 +1,136 @@
+#include "HelperFuncs.h"
+#include <string.h>
+#include <windows.h>
+#include <dbghelp.h>
+#include <process.h>
+#include <exception>
+#include <locale.h>
+#include <stdlib.h>
+
+using namespace std;
+
+#pragma comment(lib,"Dbghelp.lib")
+
+std::string to_string(const char* cstr)
+{
+ string s(cstr);
+ return s;
+}
+
+void _Exceptor()
+{
+ throw exception();
+}
+
+//https://stackoverflow.com/questions/22467604/
+const int MaxNameLen = 256;
+void printStack(CONTEXT* ctx) //Prints stack trace based on context record
+{
+ BOOL result;
+ HANDLE process;
+ HANDLE thread;
+ HMODULE hModule;
+
+ STACKFRAME64 stack;
+ ULONG frame;
+ DWORD64 displacement;
+
+ DWORD disp;
+ IMAGEHLP_LINE64 *line;
+
+ char buffer[sizeof(SYMBOL_INFO) + 2000 * sizeof(TCHAR)];
+ char name[MaxNameLen];
+ char module[MaxNameLen];
+ PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
+
+ memset(&stack, 0, sizeof(STACKFRAME64));
+
+ process = GetCurrentProcess();
+ thread = GetCurrentThread();
+ displacement = 0;
+#if !defined(_M_AMD64)
+ stack.AddrPC.Offset = (*ctx).Eip;
+ stack.AddrPC.Mode = AddrModeFlat;
+ stack.AddrStack.Offset = (*ctx).Esp;
+ stack.AddrStack.Mode = AddrModeFlat;
+ stack.AddrFrame.Offset = (*ctx).Ebp;
+ stack.AddrFrame.Mode = AddrModeFlat;
+#endif
+
+ SymInitialize(process, NULL, TRUE); //load symbols
+
+ for (frame = 0; ; frame++)
+ {
+ //get next call from stack
+ result = StackWalk64
+ (
+#if defined(_M_AMD64)
+ IMAGE_FILE_MACHINE_AMD64
+#else
+ IMAGE_FILE_MACHINE_I386
+#endif
+ ,
+ process,
+ thread,
+ &stack,
+ ctx,
+ NULL,
+ SymFunctionTableAccess64,
+ SymGetModuleBase64,
+ NULL
+ );
+
+ if (!result) break;
+
+ //get symbol name for address
+ pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
+ pSymbol->MaxNameLen = 2000;
+ SymFromAddr(process, (ULONG64)stack.AddrPC.Offset, &displacement, pSymbol);
+
+ line = (IMAGEHLP_LINE64 *)malloc(sizeof(IMAGEHLP_LINE64));
+ line->SizeOfStruct = sizeof(IMAGEHLP_LINE64);
+
+ //try to get line
+ if (SymGetLineFromAddr64(process, stack.AddrPC.Offset, &disp, line))
+ {
+ printf("\tat %s in %s: line: %lu: address: 0x%0X\n", pSymbol->Name, line->FileName, line->LineNumber, pSymbol->Address);
+ }
+ else
+ {
+ //failed to get line
+ printf("\tat %s, address 0x%0X.\n", pSymbol->Name, pSymbol->Address);
+ hModule = NULL;
+ lstrcpyA(module, "");
+ GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
+ (LPCTSTR)(stack.AddrPC.Offset), &hModule);
+
+ //at least print module name
+ if (hModule != NULL)GetModuleFileNameA(hModule, module, MaxNameLen);
+
+ printf("in %s\n", module);
+ }
+
+ free(line);
+ line = NULL;
+ }
+}
+
+int seh_filter(_EXCEPTION_POINTERS* ex)
+{
+ printf("*** Exception 0x%x occured ***\n\n", ex->ExceptionRecord->ExceptionCode);
+ printStack(ex->ContextRecord);
+
+ return EXCEPTION_EXECUTE_HANDLER;
+}
+
+void PrintCallStack()
+{
+ __try
+ {
+ _Exceptor();
+ }
+ __except (seh_filter(GetExceptionInformation()))
+ {
+ printf("Exception \n");
+ }
+} \ No newline at end of file
diff --git a/Editor/Utils/HelperFuncs.h b/Editor/Utils/HelperFuncs.h
new file mode 100644
index 0000000..2ee7932
--- /dev/null
+++ b/Editor/Utils/HelperFuncs.h
@@ -0,0 +1,8 @@
+#pragma once
+
+#include <iostream>
+#include <windows.h>
+
+std::string to_string(const char* cstr);
+
+extern void PrintCallStack(); \ No newline at end of file
diff --git a/Editor/Utils/Log.cpp b/Editor/Utils/Log.cpp
new file mode 100644
index 0000000..9ec7d42
--- /dev/null
+++ b/Editor/Utils/Log.cpp
@@ -0,0 +1,94 @@
+#include "log.h"
+#include <iostream>
+#include <ctime>
+#include <unordered_set>
+#include "HelperFuncs.h"
+
+using namespace std;
+
+//long g_LogTags = LogTag::All;
+unordered_set<string> s_OpenTags;
+
+#ifdef GAMELAB_DEBUG
+// https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c
+// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
+const std::string currentDateTime() {
+ time_t now = time(0);
+ struct tm tstruct;
+ char buf[80];
+ tstruct = *localtime(&now);
+ // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
+ // for more information about date/time format
+ strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
+
+ return buf;
+}
+
+void log_open_tag(std::string tag)
+{
+ s_OpenTags.insert(tag);
+}
+
+// https://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c
+
+void log_info(std::string log)
+{
+ cout << "\x1B[97m" << currentDateTime() << " [Info] " << log << "\033[0m" << endl;
+}
+
+void log_warning(std::string log)
+{
+ cout << "\x1B[93m" << currentDateTime() << " [Wanning] " << log << "\033[0m" << endl;
+}
+
+void log_error(std::string log)
+{
+ cout << "\x1B[91m" << currentDateTime() << " [Error] " << log << "\033[0m" << endl;
+}
+
+void log_error_null_param(std::string funcName, std::string param)
+{
+ log_error("Null parameter in " + funcName + " called " + param);
+}
+
+void log_info(string tag, std::string log)
+{
+ if (s_OpenTags.count(tag) <= 0)
+ return;
+ log_info("[" + tag + "] " + log);
+}
+void log_warning(string tag, std::string log)
+{
+ if (s_OpenTags.count(tag) <= 0)
+ return;
+ log_warning("[" + tag + "] " + log);
+}
+void log_error(string tag, std::string log)
+{
+ if (s_OpenTags.count(tag) <= 0)
+ return;
+ log_error("[" + tag + "] " + log);
+}
+#else
+void log_open_tag(std::string tag) {}
+void log_info(std::string log)
+{
+}
+
+void log_warning(std::string log)
+{
+}
+
+void log_error(std::string log)
+{
+}
+
+void log_error_null_param(std::string funcName, std::string param)
+{
+}
+
+void log_info(string tag, std::string log) {}
+void log_warning(string tag, std::string log) {}
+void log_error(string tag, std::string log) {}
+
+#endif \ No newline at end of file
diff --git a/Editor/Utils/Log.h b/Editor/Utils/Log.h
new file mode 100644
index 0000000..d66f705
--- /dev/null
+++ b/Editor/Utils/Log.h
@@ -0,0 +1,22 @@
+#pragma once
+#include <string>
+
+//enum LogTag : unsigned long
+//{
+//
+// All = ~0
+//};
+//
+//extern long g_LogTags;
+
+void log_open_tag(std::string tag);
+
+void log_info(std::string log);
+void log_warning(std::string log);
+void log_error(std::string log);
+
+void log_error_null_param(std::string funcName, std::string param);
+
+void log_info(std::string tag, std::string log);
+void log_warning(std::string tag, std::string log);
+void log_error(std::string tag, std::string log);