Skip to content

Commit

Permalink
Added support for signaler under Windows platform
Browse files Browse the repository at this point in the history
**Note** Untested, code compiles and logic is present, but it was not tested (yet) under
Windows platform. It might not even be necessary at all since the console behavior could
already be correct on Windows.
  • Loading branch information
Matthieu Vachon committed Jan 7, 2020
1 parent b56a2dd commit 5e45622
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
## Go
/dist
/vendor
/zap-pretty
/zap-pretty.exe
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/maoueh/zap-pretty
require (
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e
github.com/stretchr/testify v1.3.0
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e
)

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e h1:LwyF2AFISC9nVbS6MgzsaQNSUsRXI49GS+YQ5KX/QH0=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2 changes: 2 additions & 0 deletions signaler.go → signaler_others.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//+build darwin linux

package main

import (
Expand Down
53 changes: 53 additions & 0 deletions signaler_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"golang.org/x/sys/windows"
)

type signaler struct {
}

func NewSignaler() *signaler {
return &signaler{}
}

func (s *signaler) forwardAllSignalsToProcessGroup() {
consoleCtrlEventChan := make(chan uint, 1)
if err := handleConsoleCtrlEvent(consoleCtrlEventChan); err != nil {
debug.Println("[Warning] unable to listen for console events")
return
}

for {
ctrlType := <-consoleCtrlEventChan

// The MSDN documentation for this call states the following:
//
// > If this parameter is zero, the signal is generated in all processes
// that share the console of the calling process.
//
// In our case, it's exactly what we want to do, we want to forward to all process in
// group, so hence the `0` hard-coded value.
//
// @see https://docs.microsoft.com/en-us/windows/console/generateconsolectrlevent
windows.GenerateConsoleCtrlEvent(uint32(ctrlType), 0)
}
}

// Code for windows console handler based on https://github.com/golang/go/issues/7479#issuecomment-457669779
func handleConsoleCtrlEvent(events chan<- uint) error {
kernel32 := windows.NewLazySystemDLL("kernel32.dll")
setConsoleCtrlHandler := kernel32.NewProc("SetConsoleCtrlHandler")
callback := func(ctrlType uint) uint {
events <- ctrlType
return 0
}


n, _, err := setConsoleCtrlHandler.Call(windows.NewCallback(callback), 1)
if n == 0 {
return err
}

return nil
}

0 comments on commit 5e45622

Please sign in to comment.