~eliasnaur/gio-patches

6 2

[gio] app/internal/log: add logger for Windows DebugView

Details
Message ID
<20200110144548.34087-1-mural@ctli.io>
DKIM signature
missing
Download raw message
Patch: +41 -0
Signed-off-by: mural <mural@ctli.io>
---
 app/internal/log/log_windows.go | 41 +++++++++++++++++++++++++++++++++
 1 file changed, 41 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..2019bcd
--- /dev/null
+++ b/app/internal/log/log_windows.go
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: Unlicense OR MIT

package log

import (
	"io"
	"log"
	"os"
	"syscall"
	"unsafe"
)

type logger struct{}

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

func init() {
	// Windows DebugView already includes timestamps.
	log.SetFlags(log.Flags() &^ log.LstdFlags)

	var out io.Writer
	if syscall.Stderr != syscall.InvalidHandle {
		out = os.Stderr
	} else {
		out = debugView
	}
	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.19.1
Details
Message ID
<BZSA0XKTYNO0.1A2TNG8GE6WTU@toolbox>
In-Reply-To
<20200110144548.34087-1-mural@ctli.io> (view parent)
DKIM signature
pass
Download raw message
On Fri Jan 10, 2020 at 22:45, mural wrote:
> Signed-off-by: mural <mural@ctli.io>
> ---
>  app/internal/log/log_windows.go | 41 +++++++++++++++++++++++++++++++++
>  1 file changed, 41 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..2019bcd
> --- /dev/null
> +++ b/app/internal/log/log_windows.go
> @@ -0,0 +1,41 @@
> +// SPDX-License-Identifier: Unlicense OR MIT
> +
> +package log
> +
> +import (
> +	"io"
> +	"log"
> +	"os"
> +	"syscall"
> +	"unsafe"
> +)
> +
> +type logger struct{}
> +
> +var (
> +	kernel32           = syscall.NewLazyDLL("kernel32")
> +	outputDebugStringW = kernel32.NewProc("OutputDebugStringW")
> +	debugView          *logger
> +)
> +
> +func init() {
> +	// Windows DebugView already includes timestamps.
> +	log.SetFlags(log.Flags() &^ log.LstdFlags)
> +
> +	var out io.Writer
> +	if syscall.Stderr != syscall.InvalidHandle {

Did you test the change? From my manual testing, syscall.Stderr
is never equal to syscall.InvalidHandle when building with
`-ldflags="-H windowsgui"`.

Regardless, you shouldn't set the logger to os.Stderr here. Just
leave it alone and return early.

> +		out = os.Stderr
> +	} else {
> +		out = debugView
> +	}
> +	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.19.1
Details
Message ID
<d0111916-d9b2-4dbd-95b8-2fab392c9af6@www.fastmail.com>
In-Reply-To
<BZSA0XKTYNO0.1A2TNG8GE6WTU@toolbox> (view parent)
DKIM signature
pass
Download raw message
It's strange that I was struggling with git-send-email and
still couldn't figure out how to make it work on macOS before going to bed. 
I thought no emails were sent successfully. (I managed to use the web interface of sr.ht before this one)

On Sat, Jan 11, 2020, at 1:10 AM, Elias Naur wrote:
> On Fri Jan 10, 2020 at 22:45, mural wrote:
> > Signed-off-by: mural <mural@ctli.io>
> > ---
> >  app/internal/log/log_windows.go | 41 +++++++++++++++++++++++++++++++++
> >  1 file changed, 41 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..2019bcd
> > --- /dev/null
> > +++ b/app/internal/log/log_windows.go
> > @@ -0,0 +1,41 @@
> > +// SPDX-License-Identifier: Unlicense OR MIT
> > +
> > +package log
> > +
> > +import (
> > +	"io"
> > +	"log"
> > +	"os"
> > +	"syscall"
> > +	"unsafe"
> > +)
> > +
> > +type logger struct{}
> > +
> > +var (
> > +	kernel32           = syscall.NewLazyDLL("kernel32")
> > +	outputDebugStringW = kernel32.NewProc("OutputDebugStringW")
> > +	debugView          *logger
> > +)
> > +
> > +func init() {
> > +	// Windows DebugView already includes timestamps.
> > +	log.SetFlags(log.Flags() &^ log.LstdFlags)
> > +
> > +	var out io.Writer
> > +	if syscall.Stderr != syscall.InvalidHandle {
> 
> Did you test the change? From my manual testing, syscall.Stderr
No. I don't have a Windows dev environment at the moment.
I will find one at my home or wait until next weekday at the workspace.

It looks that this approach does not work.

> is never equal to syscall.InvalidHandle when building with
> `-ldflags="-H windowsgui"`.
> 
> Regardless, you shouldn't set the logger to os.Stderr here. Just
> leave it alone and return early.
Ah, understood.
I will revise the patch tomorrow.

> 
> > +		out = os.Stderr
> > +	} else {
> > +		out = debugView
> > +	}
> > +	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.19.1
> 
>

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

Details
Message ID
<20200113114355.17505-1-mural@ctli.io>
In-Reply-To
<BZSA0XKTYNO0.1A2TNG8GE6WTU@toolbox> (view parent)
DKIM signature
pass
Download raw message
Patch: +34 -0
Signed-off-by: mural <mural@ctli.io>
---
According to
https://docs.microsoft.com/en-us/windows/console/getstdhandle,
GetStdHandle would return NULL if there is no associated standard handles.
So we chack 0 as well.

 app/internal/log/log_windows.go | 34 +++++++++++++++++++++++++++++++++
 1 file changed, 34 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..0642919
--- /dev/null
+++ b/app/internal/log/log_windows.go
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Unlicense OR MIT

package log

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

type logger struct{}

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

func init() {
	// Windows DebugView already includes timestamps.
	if syscall.Stderr == 0 || syscall.Stderr == syscall.InvalidHandle {
		log.SetFlags(log.Flags() &^ log.LstdFlags)
		log.SetOutput(debugView)
	}
}

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

Re: [gio v1] app/internal/log: add logger for Windows DebugView

Details
Message ID
<BZUOYL6MJOBS.76S8LRPRL6OQ@testmac>
In-Reply-To
<20200113114355.17505-1-mural@ctli.io> (view parent)
DKIM signature
pass
Download raw message
On Mon Jan 13, 2020 at 7:43 PM, mural wrote:
> Signed-off-by: mural <mural@ctli.io>
> ---
> According to
> https://docs.microsoft.com/en-us/windows/console/getstdhandle,
> GetStdHandle would return NULL if there is no associated standard
> handles.
> So we chack 0 as well.
>
> 
> app/internal/log/log_windows.go | 34 +++++++++++++++++++++++++++++++++
> 1 file changed, 34 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..0642919
> --- /dev/null
> +++ b/app/internal/log/log_windows.go
> @@ -0,0 +1,34 @@
> + // Windows DebugView already includes timestamps.
> + if syscall.Stderr == 0 || syscall.Stderr == syscall.InvalidHandle {

Your commit message implies that 0 is returned for unattached I/O. If
so, please remove the InvalidHandle check which will only confuse future
readers.

[gio v2] app/internal/log: add logger for Windows DebugView

Details
Message ID
<20200113152649.81566-1-mural@ctli.io>
In-Reply-To
<BZUOYL6MJOBS.76S8LRPRL6OQ@testmac> (view parent)
DKIM signature
pass
Download raw message
Patch: +34 -0
Signed-off-by: mural <mural@ctli.io>
---
Please take a look.

 app/internal/log/log_windows.go | 34 +++++++++++++++++++++++++++++++++
 1 file changed, 34 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..13c5fe4
--- /dev/null
+++ b/app/internal/log/log_windows.go
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Unlicense OR MIT

package log

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

type logger struct{}

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

func init() {
	// Windows DebugView already includes timestamps.
	if syscall.Stderr == 0 {
		log.SetFlags(log.Flags() &^ log.LstdFlags)
		log.SetOutput(debugView)
	}
}

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

Re: [gio v2] app/internal/log: add logger for Windows DebugView

Details
Message ID
<BZUYRQ7KGEQR.362YI1TKRFNIC@toolbox>
In-Reply-To
<20200113152649.81566-1-mural@ctli.io> (view parent)
DKIM signature
pass
Download raw message
Thanks, applied.