Skip to content

Commit c63870b

Browse files
committed
Log to file rather than console
1 parent 5f06ec1 commit c63870b

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ quickjs/
44
*.exe
55
blocks/
66
wblocks.res
7+
*.log

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ PROJ=wblocks2
22
SRC=$(wildcard src/*.c)
33
DEP=$(wildcard src/*) quickjs
44

5-
.PHONY: clean run
5+
.PHONY: clean clean-all run
66

77
$(PROJ).exe: $(SRC) $(DEP) wblocks.res
8-
x86_64-w64-mingw32-gcc -std=c99 -O2 -Wall -Iquickjs/include -Lquickjs/lib/quickjs -o $@ $(SRC) wblocks.res -static -lgdi32 -lquickjs -pthread
8+
x86_64-w64-mingw32-gcc -std=c99 -O2 -Wall -Wl,-subsystem,windows -Iquickjs/include -Lquickjs/lib/quickjs -o $@ $(SRC) wblocks.res -static -lgdi32 -lquickjs -pthread
99

1010
wblocks.res:
1111
x86_64-w64-mingw32-windres wblocks.rc -O coff -o wblocks.res
@@ -19,7 +19,10 @@ quickjs:
1919
@echo 'OK.'
2020

2121
clean:
22-
rm -rf $(PROJ).exe wblocks.res quickjs
22+
rm -f $(PROJ).exe wblocks.res
23+
24+
clean-all: clean
25+
rm -rf quickjs
2326

2427
run: $(PROJ).exe
2528
./$(PROJ).exe

src/lib.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ console.error = (...args) => std.err.printf('%s\n', args.join(' '));;
5454
// Load all scripts within the `blocks` dir
5555
const [files, err] = os.readdir('./blocks');
5656
if (err) {
57-
console.log('Failed to open directory "blocks", does it exist?');
57+
console.error('Failed to open directory "blocks", does it exist?');
5858
std.exit(1);
5959
}
6060
files.filter(f => !f.startsWith('.')).sort().forEach(script => {

src/main.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// TODO:
2+
// - show context menu on tray icon (has 'Show Log', 'Reload' and 'Exit')
3+
// - click handlers for blocks
4+
// - bug with changing font of block also changing defaultBlock font *sometimes*...?, debug by logging fontrefs on render
5+
16
// Require Windows 10
27
#define WINVER 0x0A00
38
#define _WIN32_WINNT 0x0A00
@@ -7,6 +12,7 @@
712
#include <stdlib.h>
813
#include <stdbool.h>
914
#include <assert.h>
15+
#include <io.h>
1016

1117
#include <quickjs/quickjs.h>
1218
#include <quickjs/quickjs-libc.h>
@@ -435,7 +441,7 @@ char *runProcess(char *cmd)
435441
.hStdError = stdoutW,
436442
.dwFlags = STARTF_USESTDHANDLES,
437443
};
438-
if (!CreateProcessA(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
444+
if (!CreateProcessA(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
439445
CloseHandle(stdoutR);
440446
CloseHandle(stdoutW);
441447
return NULL;
@@ -517,6 +523,20 @@ int CALLBACK WinMain(HINSTANCE inst, HINSTANCE prevInst, LPSTR cmdLine, int cmdS
517523
{
518524
hInst = inst;
519525

526+
// Create console and redirect output
527+
assert(AllocConsole());
528+
#ifdef DEBUG
529+
assert(freopen("CONOUT$", "w", stdout));
530+
assert(freopen("CONOUT$", "w", stderr));
531+
#else
532+
ShowWindow(GetConsoleWindow(), 0);
533+
assert(freopen("wblocks.log", "w", stdout));
534+
assert(freopen("NUL", "w", stderr));
535+
assert(!_dup2(_fileno(stdout), _fileno(stderr)));
536+
#endif
537+
setvbuf(stdout, NULL, _IONBF, 0);
538+
setvbuf(stderr, NULL, _IONBF, 0);
539+
520540
// Load default font
521541
defaultBlock.font = xmalloc(sizeof(fontref_t));
522542
defaultBlock.font->handle = CreateFont(22, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New");

0 commit comments

Comments
 (0)