Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log redirection (v2.0) #845

Open
wants to merge 5 commits into
base: Corillian-asyncio
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions dokan/dokan.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ volatile PDokanMalloc g_DokanMalloc = NULL;
volatile PDokanFree g_DokanFree = NULL;
volatile PDokanRealloc g_DokanRealloc = NULL;

volatile PDokanDbgPrint g_PDokanDbgPrint = NULL;
volatile PDokanDbgPrintW g_PDokanDbgPrintW = NULL;

volatile LONG g_DokanInitialized = 0;

// TODO NEXT:
Expand Down Expand Up @@ -2016,8 +2019,7 @@ DOKAN_API PTP_POOL DOKAN_CALLBACK DokanGetThreadPool() {
return threadPool;
}

void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS *memoryCallbacks) {

void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS* memoryCallbacks, DOKAN_LOG_CALLBACKS* logCallbacks) {
// ensure 64-bit alignment
assert(offsetof(EVENT_INFORMATION, Buffer) % 8 == 0);

Expand Down Expand Up @@ -2047,6 +2049,18 @@ void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS *memoryCallbacks) {
InterlockedExchange((volatile LONG*)&g_DokanRealloc, (LONG)memoryCallbacks->Realloc);
#else
#error Unsupported architecture!
#endif
}

if (logCallbacks) {
#if INTPTR_MAX == INT64_MAX
InterlockedExchange64((volatile LONG64*)&g_PDokanDbgPrint, (LONG64)logCallbacks->DbgPrint);
InterlockedExchange64((volatile LONG64*)&g_PDokanDbgPrintW, (LONG64)logCallbacks->DbgPrintW);
#elif INTPTR_MAX == INT32_MAX
InterlockedExchange((volatile LONG*)&g_PDokanDbgPrint, (LONG)logCallbacks->DbgPrint);
InterlockedExchange((volatile LONG*)&g_PDokanDbgPrintW, (LONG)logCallbacks->DbgPrintW);
#else
#error Unsupported architecture!
#endif
}

Expand Down
11 changes: 10 additions & 1 deletion dokan/dokan.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,21 @@ typedef void* (WINAPI *PDokanMalloc)(size_t size, const char *fileName, int line
typedef void (WINAPI *PDokanFree)(void *userData);
typedef void* (WINAPI *PDokanRealloc)(void *userData, size_t newSize, const char *fileName, int lineNumber);

typedef void (WINAPI *PDokanDbgPrint)(LPCSTR logString);
typedef void (WINAPI *PDokanDbgPrintW)(LPCWSTR logString);

typedef struct _DOKAN_MEMORY_CALLBACKS {
PDokanMalloc Malloc;
PDokanFree Free;
PDokanRealloc Realloc;
} DOKAN_MEMORY_CALLBACKS, *PDOKAN_MEMORY_CALLBACKS;

typedef struct _DOKAN_LOG_CALLBACKS
{
PDokanDbgPrint DbgPrint;
PDokanDbgPrintW DbgPrintW;
} DOKAN_LOG_CALLBACKS, *PDOKAN_LOG_CALLBACKS;

#define DOKAN_EXCEPTION_NOT_INITIALIZED 0x0f0ff0ff
#define DOKAN_EXCEPTION_INITIALIZATION_FAILED 0x0fbadbad
#define DOKAN_EXCEPTION_SHUTDOWN_FAILED 0x0fbadf00
Expand Down Expand Up @@ -1102,7 +1111,7 @@ void DOKANAPI DokanEndDispatchSetFileSecurity(DOKAN_SET_FILE_SECURITY_EVENT *Eve
DOKAN_API PTP_POOL DOKAN_CALLBACK DokanGetThreadPool();

// Init/shutdown
void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS *memoryCallbacks);
void DOKANAPI DokanInit(DOKAN_MEMORY_CALLBACKS* memoryCallbacks, DOKAN_LOG_CALLBACKS* logCallbacks);
void DOKANAPI DokanShutdown();
/** @} */

Expand Down
34 changes: 20 additions & 14 deletions dokan/dokanc.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ extern BOOL g_DebugMode;
// DokanOptions->UseStdErr is ON?
extern BOOL g_UseStdErr;

extern volatile PDokanDbgPrint g_PDokanDbgPrint;
extern volatile PDokanDbgPrintW g_PDokanDbgPrintW;

#ifdef _MSC_VER

static VOID DokanDbgPrint(LPCSTR format, ...) {
Expand All @@ -71,14 +74,16 @@ static VOID DokanDbgPrint(LPCSTR format, ...) {
outputString = buffer;
}

OutputDebugStringA(outputString);

if(g_UseStdErr) {

fputs(outputString, stderr);
fflush(stderr);
if (g_PDokanDbgPrint) {
g_PDokanDbgPrint(outputString);
} else {
OutputDebugStringA(outputString);
if (g_UseStdErr) {
fputs(outputString, stderr);
fflush(stderr);
}
}

if(buffer) {
_freea(buffer);
}
Expand All @@ -103,16 +108,17 @@ static VOID DokanDbgPrintW(LPCWSTR format, ...) {
outputString = buffer;
}

OutputDebugStringW(outputString);

if(g_UseStdErr) {

fputws(outputString, stderr);
fflush(stderr);
if (g_PDokanDbgPrint) {
g_PDokanDbgPrintW(outputString);
} else {
OutputDebugStringW(outputString);
if (g_UseStdErr) {
fputws(outputString, stderr);
fflush(stderr);
}
}

if(buffer) {

_freea(buffer);
}

Expand Down
2 changes: 1 addition & 1 deletion dokan_control/dokanctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ int __cdecl wmain(int argc, PWCHAR argv[]) {
PVOID wow64OldValue;
BOOL isAdmin;

DokanInit(NULL);
DokanInit(NULL, NULL);
isAdmin = IsUserAnAdmin();

DokanUseStdErr(TRUE); // Set dokan library debug output
Expand Down
4 changes: 2 additions & 2 deletions dokan_fuse/src/dokanfuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ bool fuse_chan::init() {

// check version
typedef ULONG(__stdcall * DokanVersionType)();
typedef void(__stdcall *DokanInitType)(DOKAN_MEMORY_CALLBACKS *memoryCallbacks);
typedef void(__stdcall *DokanInitType)(DOKAN_MEMORY_CALLBACKS *memoryCallbacks, DOKAN_LOG_CALLBACKS *logCallbacks);

DokanVersionType ResolvedDokanVersion;
DokanInitType ResolvedInit;
Expand All @@ -647,7 +647,7 @@ bool fuse_chan::init() {
return false;
}

ResolvedInit(NULL);
ResolvedInit(NULL, NULL);

ResolvedDokanVersion =
(DokanVersionType)GetProcAddress(dokanDll, "DokanVersion");
Expand Down
4 changes: 2 additions & 2 deletions samples/dokan_mirror/mirror.c
Original file line number Diff line number Diff line change
Expand Up @@ -2820,11 +2820,11 @@ int __cdecl wmain(ULONG argc, PWCHAR argv[]) {
memoryCallbacks.Free = MirrorFree;
memoryCallbacks.Realloc = MirrorRealloc;

DokanInit(&memoryCallbacks);
DokanInit(&memoryCallbacks, NULL);

#else

DokanInit(NULL);
DokanInit(NULL, NULL);

#endif

Expand Down