github.com/utopiagio/gio@v0.0.8/app/log_windows.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package app
     4  
     5  import (
     6  	"log"
     7  	"syscall"
     8  	"unsafe"
     9  )
    10  
    11  type logger struct{}
    12  
    13  var (
    14  	kernel32           = syscall.NewLazyDLL("kernel32")
    15  	outputDebugStringW = kernel32.NewProc("OutputDebugStringW")
    16  	debugView          *logger
    17  )
    18  
    19  func init() {
    20  	// Windows DebugView already includes timestamps.
    21  	if syscall.Stderr == 0 {
    22  		log.SetFlags(log.Flags() &^ log.LstdFlags)
    23  		log.SetOutput(debugView)
    24  	}
    25  }
    26  
    27  func (l *logger) Write(buf []byte) (int, error) {
    28  	p, err := syscall.UTF16PtrFromString(string(buf))
    29  	if err != nil {
    30  		return 0, err
    31  	}
    32  	outputDebugStringW.Call(uintptr(unsafe.Pointer(p)))
    33  	return len(buf), nil
    34  }