11#include " Utils.h"
22
33#ifdef _WIN32
4- #include < windows.h>
54#include < intrin.h>
5+ #include < windows.h>
66#else
77#include < dlfcn.h>
88#endif
99
1010#include < cctype>
1111
12- std::string OELogger::getModuleName () {
12+ std::string OELogger::getModuleName ()
13+ {
1314#ifdef _WIN32
1415 HMODULE hModule = nullptr ;
1516 // Get handle to the module containing the current instruction pointer
16- GetModuleHandleEx (
17+ GetModuleHandleEx (
1718 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
18- (LPCTSTR)(void *)_ReturnAddress (), // Cast return address properly
19+ (LPCTSTR) (void *) _ReturnAddress (), // Cast return address properly
1920 &hModule);
20-
21+
2122 WCHAR modulePath[MAX_PATH + 256 ];
22- if (GetModuleFileNameW (hModule, modulePath, (DWORD)(MAX_PATH + 256 ))) {
23+ if (GetModuleFileNameW (hModule, modulePath, (DWORD) (MAX_PATH + 256 )))
24+ {
2325 // Convert WCHAR to std::string using UTF-8 encoding
24- int sizeNeeded = WideCharToMultiByte (CP_UTF8, 0 , modulePath, -1 , NULL , 0 , NULL , NULL );
25- std::string result (sizeNeeded, 0 );
26- WideCharToMultiByte (CP_UTF8, 0 , modulePath, -1 , &result[0 ], sizeNeeded, NULL , NULL );
27- return formatModuleName (result);
26+ int sizeNeeded = WideCharToMultiByte (CP_UTF8, 0 , modulePath, -1 , NULL , 0 , NULL , NULL );
27+ std::string result (sizeNeeded, 0 );
28+ WideCharToMultiByte (CP_UTF8, 0 , modulePath, -1 , &result[0 ], sizeNeeded, NULL , NULL );
29+ return formatModuleName (result);
2830 }
2931 return " [unknown]" ;
3032#else
3133 // macOS/Linux implementation
3234 Dl_info info;
33- if (dladdr (reinterpret_cast <void *>(__builtin_return_address (0 )), &info)) {
34- if (info.dli_fname ) {
35- return formatModuleName (std::string (info.dli_fname ));
35+ if (dladdr (reinterpret_cast <void *> (__builtin_return_address (0 )), &info))
36+ {
37+ if (info.dli_fname )
38+ {
39+ return formatModuleName (std::string (info.dli_fname ));
3640 }
3741 }
3842 return " [unknown]" ;
3943#endif
4044}
4145
42- std::string OELogger::formatModuleName (const std::string& path) {
43- size_t lastSlash = path.find_last_of (" /\\ " );
44- std::string basename = path.substr (lastSlash + 1 );
45-
46- // Remove .exe or .dll extension on Windows
47- #ifdef _WIN32
48- size_t lastDot = basename.find_last_of (' .' );
49- if (lastDot != std::string::npos) {
50- std::string ext = basename.substr (lastDot);
51- if (_stricmp (ext.c_str (), " .exe" ) == 0 || _stricmp (ext.c_str (), " .dll" ) == 0 ) {
52- basename = basename.substr (0 , lastDot);
46+ std::string OELogger::formatModuleName (const std::string& path)
47+ {
48+ size_t lastSlash = path.find_last_of (" /\\ " );
49+ std::string basename = path.substr (lastSlash + 1 );
50+
51+ // Remove .exe or .dll extension on Windows
52+ #ifdef _WIN32
53+ size_t lastDot = basename.find_last_of (' .' );
54+ if (lastDot != std::string::npos)
55+ {
56+ std::string ext = basename.substr (lastDot);
57+ if (_stricmp (ext.c_str (), " .exe" ) == 0 || _stricmp (ext.c_str (), " .dll" ) == 0 )
58+ {
59+ basename = basename.substr (0 , lastDot);
5360 }
5461 }
55- #endif
62+ #endif
5663
5764 std::string formatted;
58- for (size_t i = 0 ; i < basename.length (); ++i) {
65+ for (size_t i = 0 ; i < basename.length (); ++i)
66+ {
5967 char ch = basename[i];
60- if (std::isupper (ch)) {
61- if (i > 0 ) {
68+ if (std::isupper (ch))
69+ {
70+ if (i > 0 )
71+ {
6272 formatted += ' -' ;
6373 }
64- formatted += std::tolower (ch);
65- } else {
74+ formatted += std::tolower (ch);
75+ }
76+ else
77+ {
6678 formatted += ch;
6779 }
6880 }
6981 return " [" + formatted + " ]" ;
82+ }
83+
84+ std::string OELogger::getCurrentTimeIso ()
85+ {
86+ // Get current time with millisecond precision
87+ auto now = std::chrono::system_clock::now ();
88+ auto ms = std::chrono::duration_cast<std::chrono::milliseconds> (now.time_since_epoch ()) % 1000 ;
89+
90+ // Convert to time_t for std::put_time
91+ std::time_t time_now = std::chrono::system_clock::to_time_t (now);
92+ std::tm bt = *std::localtime (&time_now);
93+
94+ // Format as ISO 8601 with milliseconds
95+ std::ostringstream oss;
96+ oss << std::put_time (&bt, " %Y-%m-%dT%H:%M:%S" );
97+ oss << ' .' << std::setfill (' 0' ) << std::setw (3 ) << ms.count ();
98+
99+ return oss.str ();
70100}
0 commit comments