Skip to content

Commit 4fdee71

Browse files
committed
Fix plugin logging on Windows
1 parent 3ccabd9 commit 4fdee71

3 files changed

Lines changed: 75 additions & 32 deletions

File tree

Source/Utils/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_sources(
1010
BroadcastParser.cpp
1111
BroadcastPayload.h
1212
BroadcastPayload.cpp
13-
Utils.h)
14-
13+
Utils.h
14+
Utils.cpp
15+
)
1516
# add nested directories

Source/Utils/Utils.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

Source/Utils/Utils.h

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@
3131
#include <map>
3232
#include <mutex>
3333
#include <string>
34-
#include <dlfcn.h>
3534

3635
#include "../Processors/PluginManager/OpenEphysPlugin.h"
3736

3837
/* Thread-safe logger */
39-
class OELogger
38+
class PLUGIN_API OELogger
4039
{
4140
public:
4241
static OELogger& instance()
@@ -83,34 +82,8 @@ class OELogger
8382
logFile << "[open-ephys] Session start time: " << ctime (&now);
8483
}
8584

86-
std::string getModuleName() const {
87-
Dl_info info;
88-
if (dladdr(reinterpret_cast<void*>(__builtin_return_address(0)), &info))
89-
{
90-
if (info.dli_fname) {
91-
return formatModuleName(std::string(info.dli_fname));
92-
}
93-
}
94-
return "[unknown]";
95-
}
96-
97-
std::string formatModuleName(const std::string& path) const {
98-
size_t lastSlash = path.find_last_of("/\\");
99-
std::string basename = path.substr(lastSlash + 1);
100-
std::string formatted;
101-
for (size_t i = 0; i < basename.length(); ++i) {
102-
char ch = basename[i];
103-
if (std::isupper(ch)) {
104-
if (i > 0) {
105-
formatted += '-'; // Add hyphen before uppercase letters (except the first one)
106-
}
107-
formatted += std::tolower(ch); // Convert to lowercase
108-
} else {
109-
formatted += ch;
110-
}
111-
}
112-
return "[" + formatted + "]";
113-
}
85+
static std::string getModuleName();
86+
static std::string formatModuleName(const std::string& path);
11487

11588
private:
11689
std::mutex mt;

0 commit comments

Comments
 (0)