~eliasnaur/gio-patches

gio: app/internal/log: add logger for Windows DebugView v1 PROPOSED

~typeless
Windows GUI programs close all stdin/stdout/stderr by default.
This
patch adds the native logging support provided by Windows.

mural (1):
  app/internal/log: add logger for Windows DebugView

 app/internal/log/log_windows.go | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 app/internal/log/log_windows.go

-- 
2.24.1
> From: mural <mural@ctli.io>
> 
> 
> Signed-off-by: Mura Li <mural@ctli.io>
> ---
> app/internal/log/log_windows.go | 32 ++++++++++++++++++++++++++++++++
> 1 file changed, 32 insertions(+)
> create mode 100644 app/internal/log/log_windows.go
> 
> 
> diff --git a/app/internal/log/log_windows.go
> b/app/internal/log/log_windows.go
> new file mode 100644
> index 0000000..c7691d2
> --- /dev/null
> +++ b/app/internal/log/log_windows.go
> @@ -0,0 +1,32 @@
> +// SPDX-License-Identifier: Unlicense OR MIT
> +
> +package log
> +
> +import (
> + "log"
> + "syscall"
> + "unsafe"
> +)
> +
> +type logger struct{}
> +
> +var (
> + kernel32 = syscall.NewLazyDLL("kernel32")
> + outputDebugStringW = kernel32.NewProc("OutputDebugStringW")
> + out logger
> +)
> +
> +func init() {
> + // Windows DbgView already includes timestamps.
> + log.SetFlags(log.Flags() &^ log.LstdFlags)
> + log.SetOutput(&out)
Thanks. The logging seems to appear in the DbgView application, as
expected. However, I often run Gio programs from the Windows shell,
cmd.exe or similar, where the log output will no longer appear.

Can you make it so the logging appear both in the terminal and in
DbgView? Alternatively, detect whether standard error is gone and
only redirect to dgbview if so?
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/9482/mbox | git am -3
Learn more about email & git

[PATCH gio 1/1] app/internal/log: add logger for Windows DebugView Export this patch

~typeless
From: mural <mural@ctli.io>

Signed-off-by: Mura Li <mural@ctli.io>
---
 app/internal/log/log_windows.go | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 app/internal/log/log_windows.go

diff --git a/app/internal/log/log_windows.go b/app/internal/log/log_windows.go
new file mode 100644
index 0000000..c7691d2
--- /dev/null
+++ b/app/internal/log/log_windows.go
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: Unlicense OR MIT

package log

import (
	"log"
	"syscall"
	"unsafe"
)

type logger struct{}

var (
	kernel32           = syscall.NewLazyDLL("kernel32")
	outputDebugStringW = kernel32.NewProc("OutputDebugStringW")
	out                logger
)

func init() {
	// Windows DbgView already includes timestamps.
	log.SetFlags(log.Flags() &^ log.LstdFlags)
	log.SetOutput(&out)
}

func (l *logger) Write(buf []byte) (int, error) {
	p, err := syscall.UTF16PtrFromString(string(buf))
	if err != nil {
		return 0, err
	}
	outputDebugStringW.Call(uintptr(unsafe.Pointer(p)))
	return len(buf), nil
}
-- 
2.24.1
> From: mural <mural@ctli.io>
> 
> 
> Signed-off-by: Mura Li <mural@ctli.io>
> ---
> app/internal/log/log_windows.go | 32 ++++++++++++++++++++++++++++++++
> 1 file changed, 32 insertions(+)
> create mode 100644 app/internal/log/log_windows.go
> 
> 
> diff --git a/app/internal/log/log_windows.go
> b/app/internal/log/log_windows.go
> new file mode 100644
> index 0000000..c7691d2
> --- /dev/null
> +++ b/app/internal/log/log_windows.go
> @@ -0,0 +1,32 @@
> +// SPDX-License-Identifier: Unlicense OR MIT
> +
> +package log
> +
> +import (
> + "log"
> + "syscall"
> + "unsafe"
> +)
> +
> +type logger struct{}
> +
> +var (
> + kernel32 = syscall.NewLazyDLL("kernel32")
> + outputDebugStringW = kernel32.NewProc("OutputDebugStringW")
> + out logger
> +)
> +
> +func init() {
> + // Windows DbgView already includes timestamps.
> + log.SetFlags(log.Flags() &^ log.LstdFlags)
> + log.SetOutput(&out)
Thanks. The logging seems to appear in the DbgView application, as
expected. However, I often run Gio programs from the Windows shell,
cmd.exe or similar, where the log output will no longer appear.

Can you make it so the logging appear both in the terminal and in
DbgView? Alternatively, detect whether standard error is gone and
only redirect to dgbview if so?
View this thread in the archives