1+ #include " Utils.h"
2+
3+ #ifdef _WIN32
4+ #include < windows.h>
5+ #else
6+ #include < dlfcn.h>
7+ #endif
8+
9+ #include < cctype>
10+
11+ std::string OELogger::getModuleName () {
12+ #ifdef _WIN32
13+ HMODULE hModule = nullptr ;
14+ // Get handle to the module containing the current instruction pointer
15+ GetModuleHandleEx (
16+ GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
17+ (LPCTSTR)(void *)_ReturnAddress (), // Cast return address properly
18+ &hModule);
19+
20+ WCHAR modulePath[MAX_PATH + 256 ];
21+ if (GetModuleFileNameW (hModule, modulePath, (DWORD)(MAX_PATH + 256 ))) {
22+ // Convert WCHAR to std::string using UTF-8 encoding
23+ int sizeNeeded = WideCharToMultiByte (CP_UTF8, 0 , modulePath, -1 , NULL , 0 , NULL , NULL );
24+ std::string result (sizeNeeded, 0 );
25+ WideCharToMultiByte (CP_UTF8, 0 , modulePath, -1 , &result[0 ], sizeNeeded, NULL , NULL );
26+ return formatModuleName (result);
27+ }
28+ return " [unknown]" ;
29+ #else
30+ // macOS/Linux implementation
31+ Dl_info info;
32+ if (dladdr (reinterpret_cast <void *>(__builtin_return_address (0 )), &info)) {
33+ if (info.dli_fname ) {
34+ return formatModuleName (std::string (info.dli_fname ));
35+ }
36+ }
37+ return " [unknown]" ;
38+ #endif
39+ }
40+
41+ std::string OELogger::formatModuleName (const std::string& path) {
42+ size_t lastSlash = path.find_last_of (" /\\ " );
43+ std::string basename = path.substr (lastSlash + 1 );
44+
45+ // Remove .exe or .dll extension on Windows
46+ #ifdef _WIN32
47+ size_t lastDot = basename.find_last_of (' .' );
48+ if (lastDot != std::string::npos) {
49+ std::string ext = basename.substr (lastDot);
50+ if (_stricmp (ext.c_str (), " .exe" ) == 0 || _stricmp (ext.c_str (), " .dll" ) == 0 ) {
51+ basename = basename.substr (0 , lastDot);
52+ }
53+ }
54+ #endif
55+
56+ std::string formatted;
57+ for (size_t i = 0 ; i < basename.length (); ++i) {
58+ char ch = basename[i];
59+ if (std::isupper (ch)) {
60+ if (i > 0 ) {
61+ formatted += ' -' ;
62+ }
63+ formatted += std::tolower (ch);
64+ } else {
65+ formatted += ch;
66+ }
67+ }
68+ return " [" + formatted + " ]" ;
69+ }
0 commit comments