Skip to content

Commit

Permalink
Allow the logfile to be disabled/enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
paladine committed Oct 6, 2024
1 parent cada283 commit 93d305e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
14 changes: 0 additions & 14 deletions vstudio/wbtrv32/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,10 @@
#include "framework.h"
#include "wbtrv32.h"

// #define DEBUG_ATTACH

#ifdef DEBUG_ATTACH
#include "psapi.h"
#endif

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
#ifdef DEBUG_ATTACH
{
TCHAR buf[MAX_PATH];

GetProcessImageFileName(GetCurrentProcess(), buf, ARRAYSIZE(buf));
MessageBox(NULL, TEXT("Attach now"), buf, MB_OK);
}
#endif
wbtrv32::processAttach();
break;
case DLL_PROCESS_DETACH:
Expand Down
35 changes: 34 additions & 1 deletion vstudio/wbtrv32/wbtrv32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,47 @@
#include "combaseapi.h"
#include "framework.h"

// Define this to pop up a messagebox when wbtrv32.dll is loaded, to allow you
// to attach a debugger on startup
//
// #define DEBUG_ATTACH

// Define this to log errors to wbtrv32.log
// #define LOG_TO_FILE

#ifdef DEBUG_ATTACH
#include "Psapi.h"
#endif

using namespace btrieve;

static std::unordered_map<std::wstring, std::shared_ptr<BtrieveDriver>>
_openFiles;

#ifdef LOG_TO_FILE
static std::unique_ptr<FILE, decltype(&fclose)> _logFile(nullptr, &fclose);
#endif

void wbtrv32::processAttach() {
#ifdef DEBUG_ATTACH
{
TCHAR buf[MAX_PATH];

GetProcessImageFileName(GetCurrentProcess(), buf, ARRAYSIZE(buf));
MessageBox(NULL, TEXT("Attach now"), buf, MB_OK);
}
#endif

#ifdef LOG_TO_FILE
_logFile.reset(_wfsopen(L"wbtrv32.log", L"ab", _SH_DENYWR));
#endif
}

void wbtrv32::processDetach() { _logFile.reset(nullptr); }
void wbtrv32::processDetach() {
#ifdef LOG_TO_FILE
_logFile.reset(nullptr);
#endif
}

static BtrieveDriver *getOpenDatabase(LPVOID lpPositioningBlock) {
WCHAR guidStr[64];
Expand Down Expand Up @@ -64,9 +93,11 @@ static void debug(const BtrieveCommand &command, const char *format, ...) {
len = snprintf(buf, sizeof(buf),
"[%S]: ", driver->getOpenedFilename().c_str());
OutputDebugStringA(buf);
#ifdef LOG_TO_FILE
if (_logFile) {
fwrite(buf, 1, len, _logFile.get());
}
#endif
}

va_list args;
Expand All @@ -81,10 +112,12 @@ static void debug(const BtrieveCommand &command, const char *format, ...) {

OutputDebugStringA(buf);

#ifdef LOG_TO_FILE
if (_logFile) {
fwrite(buf, 1, len + 2, _logFile.get());
fflush(_logFile.get());
}
#endif
}

static void AddToOpenFiles(BtrieveCommand &command,
Expand Down

0 comments on commit 93d305e

Please sign in to comment.