1
1
#include " Utils.h"
2
2
3
3
#ifdef _WIN32
4
- #include < windows.h>
5
4
#include < intrin.h>
5
+ #include < windows.h>
6
6
#else
7
7
#include < dlfcn.h>
8
8
#endif
9
9
10
10
#include < cctype>
11
11
12
- std::string OELogger::getModuleName () {
12
+ std::string OELogger::getModuleName ()
13
+ {
13
14
#ifdef _WIN32
14
15
HMODULE hModule = nullptr ;
15
16
// Get handle to the module containing the current instruction pointer
16
- GetModuleHandleEx (
17
+ GetModuleHandleEx (
17
18
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
19
20
&hModule);
20
-
21
+
21
22
WCHAR modulePath[MAX_PATH + 256 ];
22
- if (GetModuleFileNameW (hModule, modulePath, (DWORD)(MAX_PATH + 256 ))) {
23
+ if (GetModuleFileNameW (hModule, modulePath, (DWORD) (MAX_PATH + 256 )))
24
+ {
23
25
// 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);
28
30
}
29
31
return " [unknown]" ;
30
32
#else
31
33
// macOS/Linux implementation
32
34
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 ));
36
40
}
37
41
}
38
42
return " [unknown]" ;
39
43
#endif
40
44
}
41
45
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);
53
60
}
54
61
}
55
- #endif
62
+ #endif
56
63
57
64
std::string formatted;
58
- for (size_t i = 0 ; i < basename.length (); ++i) {
65
+ for (size_t i = 0 ; i < basename.length (); ++i)
66
+ {
59
67
char ch = basename[i];
60
- if (std::isupper (ch)) {
61
- if (i > 0 ) {
68
+ if (std::isupper (ch))
69
+ {
70
+ if (i > 0 )
71
+ {
62
72
formatted += ' -' ;
63
73
}
64
- formatted += std::tolower (ch);
65
- } else {
74
+ formatted += std::tolower (ch);
75
+ }
76
+ else
77
+ {
66
78
formatted += ch;
67
79
}
68
80
}
69
81
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 ();
70
100
}
0 commit comments