-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for signaler under Windows platform
**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
Showing
5 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
## Go | ||
/dist | ||
/vendor | ||
/zap-pretty | ||
/zap-pretty.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//+build darwin linux | ||
|
||
package main | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |