Skip to content

Commit

Permalink
Fix plugin logging on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
medengineer committed Nov 20, 2024
1 parent 3ccabd9 commit 4fdee71
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
5 changes: 3 additions & 2 deletions Source/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_sources(
BroadcastParser.cpp
BroadcastPayload.h
BroadcastPayload.cpp
Utils.h)

Utils.h
Utils.cpp
)
# add nested directories
69 changes: 69 additions & 0 deletions Source/Utils/Utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "Utils.h"

#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif

#include <cctype>

std::string OELogger::getModuleName() {
#ifdef _WIN32
HMODULE hModule = nullptr;
// Get handle to the module containing the current instruction pointer
GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCTSTR)(void*)_ReturnAddress(), // Cast return address properly
&hModule);

WCHAR modulePath[MAX_PATH + 256];
if (GetModuleFileNameW(hModule, modulePath, (DWORD)(MAX_PATH + 256))) {
// Convert WCHAR to std::string using UTF-8 encoding
int sizeNeeded = WideCharToMultiByte(CP_UTF8, 0, modulePath, -1, NULL, 0, NULL, NULL);
std::string result(sizeNeeded, 0);
WideCharToMultiByte(CP_UTF8, 0, modulePath, -1, &result[0], sizeNeeded, NULL, NULL);
return formatModuleName(result);
}
return "[unknown]";
#else
// macOS/Linux implementation
Dl_info info;
if (dladdr(reinterpret_cast<void*>(__builtin_return_address(0)), &info)) {
if (info.dli_fname) {
return formatModuleName(std::string(info.dli_fname));
}
}
return "[unknown]";
#endif
}

std::string OELogger::formatModuleName(const std::string& path) {
size_t lastSlash = path.find_last_of("/\\");
std::string basename = path.substr(lastSlash + 1);

// Remove .exe or .dll extension on Windows
#ifdef _WIN32
size_t lastDot = basename.find_last_of('.');
if (lastDot != std::string::npos) {
std::string ext = basename.substr(lastDot);
if (_stricmp(ext.c_str(), ".exe") == 0 || _stricmp(ext.c_str(), ".dll") == 0) {
basename = basename.substr(0, lastDot);
}
}
#endif

std::string formatted;
for (size_t i = 0; i < basename.length(); ++i) {
char ch = basename[i];
if (std::isupper(ch)) {
if (i > 0) {
formatted += '-';
}
formatted += std::tolower(ch);
} else {
formatted += ch;
}
}
return "[" + formatted + "]";
}
33 changes: 3 additions & 30 deletions Source/Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@
#include <map>
#include <mutex>
#include <string>
#include <dlfcn.h>

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

/* Thread-safe logger */
class OELogger
class PLUGIN_API OELogger
{
public:
static OELogger& instance()
Expand Down Expand Up @@ -83,34 +82,8 @@ class OELogger
logFile << "[open-ephys] Session start time: " << ctime (&now);
}

std::string getModuleName() const {
Dl_info info;
if (dladdr(reinterpret_cast<void*>(__builtin_return_address(0)), &info))
{
if (info.dli_fname) {
return formatModuleName(std::string(info.dli_fname));
}
}
return "[unknown]";
}

std::string formatModuleName(const std::string& path) const {
size_t lastSlash = path.find_last_of("/\\");
std::string basename = path.substr(lastSlash + 1);
std::string formatted;
for (size_t i = 0; i < basename.length(); ++i) {
char ch = basename[i];
if (std::isupper(ch)) {
if (i > 0) {
formatted += '-'; // Add hyphen before uppercase letters (except the first one)
}
formatted += std::tolower(ch); // Convert to lowercase
} else {
formatted += ch;
}
}
return "[" + formatted + "]";
}
static std::string getModuleName();
static std::string formatModuleName(const std::string& path);

private:
std::mutex mt;
Expand Down

0 comments on commit 4fdee71

Please sign in to comment.