4coder » Forums » Redirection of OutputDebugString to a 4coder buffer
Zero
2 posts
#20921 Redirection of OutputDebugString to a 4coder buffer
3 months ago Edited by Zero on April 16, 2019, 12:12 p.m. Reason: Initial post

This would only be a windows thing and I don't know if this feature is as good / helpful as I am thinking right now, but with the log messages that are passed to OutputDebugString could be redirected to a 4coder buffer, so that we don't have to open / switch to a visual studio, if we only want to view log output while running.
Delix
Ryan Fleury
164 posts / 1 project

Handmade Network lead and developer of The Melodist

#20922 Redirection of OutputDebugString to a 4coder buffer
3 months ago

4coder receives stderr and stdout, so what you could probably do is just this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void DebugPrint(char *format, ...)
{
    static char buffer[4096];
    va_list args;
    va_start(args, format);
    vsnprintf(buffer, sizeof(buffer), format, args);
    va_end(args);
    printf("%s", buffer);
    OutputDebugString(buffer);
}

Handmade Network lead and developer of The Melodist
mmozeiko
Mārtiņš Možeiko
1943 posts / 1 project
#20923 Redirection of OutputDebugString to a 4coder buffer
3 months ago Edited by Mārtiņš Možeiko on April 16, 2019, 7:22 p.m.

Here's a small C code snippet how to redirect OutputDebugString to stderr of your process:
https://gist.github.com/mmozeiko/bfe0ce5762c496f92d8c775983c41694

Only thing you need to do is to call "ods_capture()" at start of process.

For more information on how OutputDebugString works internally you can read here: http://www.unixwiz.net/techtips/outputdebugstring.html
Zero
2 posts
#20927 Redirection of OutputDebugString to a 4coder buffer
3 months ago

Neither of them worked for me, they only work when I set the SUBSYSTEM to Console, and define entry point as 'int main' instead of 'WinMain'. So I ended of doing this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
typedef void (WindowsLogFn)(const char*);
static WindowsLogFn* windowsLog;

static void windowsLogConsole(const char* msg)
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	DWORD written; // ignored
	char* t = (char*)msg;
	DWORD len = 0;
	while (*t != 0) { t++; len++; }
	WriteConsoleA(hOut, msg, len, &written, 0);
}

void log(const char* cstr)
{
	windowsLog(cstr);
	windowsLog("\n");
}

// entry point
if(IsDebuggerPresent())
{
	windowsLog = OutputDebugStringA;
}
else
{
	AllocConsole();
	windowsLog = windowsLogConsole;
}
// others
mmozeiko
Mārtiņš Možeiko
1943 posts / 1 project
#20930 Redirection of OutputDebugString to a 4coder buffer
3 months ago

If you are using subsystem windows, then you need to call AllocConsole and reopen stdout/stderr FILE's. Because by default they won't go anywhere. If you are using console subsystem, then there is no need to call AllocConsole - it is already called by OS for you.