Browse code

Windows:Add ETW logging hook

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2019/02/21 09:39:07
Showing 3 changed files
... ...
@@ -3,7 +3,6 @@ package main
3 3
 import (
4 4
 	"fmt"
5 5
 	"os"
6
-	"runtime"
7 6
 
8 7
 	"github.com/docker/docker/cli"
9 8
 	"github.com/docker/docker/daemon/config"
... ...
@@ -70,13 +69,7 @@ func main() {
70 70
 	// Set terminal emulation based on platform as required.
71 71
 	_, stdout, stderr := term.StdStreams()
72 72
 
73
-	// @jhowardmsft - maybe there is a historic reason why on non-Windows, stderr is used
74
-	// here. However, on Windows it makes no sense and there is no need.
75
-	if runtime.GOOS == "windows" {
76
-		logrus.SetOutput(stdout)
77
-	} else {
78
-		logrus.SetOutput(stderr)
79
-	}
73
+	initLogging(stdout, stderr)
80 74
 
81 75
 	onError := func(err error) {
82 76
 		fmt.Fprintf(stderr, "%s\n", err)
... ...
@@ -2,7 +2,17 @@
2 2
 
3 3
 package main
4 4
 
5
+import (
6
+	"io"
7
+
8
+	"github.com/sirupsen/logrus"
9
+)
10
+
5 11
 func runDaemon(opts *daemonOptions) error {
6 12
 	daemonCli := NewDaemonCli()
7 13
 	return daemonCli.start(opts)
8 14
 }
15
+
16
+func initLogging(_, stderr io.Writer) {
17
+	logrus.SetOutput(stderr)
18
+}
... ...
@@ -1,8 +1,10 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"io"
4 5
 	"path/filepath"
5 6
 
7
+	"github.com/Microsoft/go-winio/pkg/etwlogrus"
6 8
 	_ "github.com/docker/docker/autogen/winresources/dockerd"
7 9
 	"github.com/sirupsen/logrus"
8 10
 )
... ...
@@ -36,3 +38,17 @@ func runDaemon(opts *daemonOptions) error {
36 36
 	notifyShutdown(err)
37 37
 	return err
38 38
 }
39
+
40
+func initLogging(stdout, _ io.Writer) {
41
+	// Maybe there is a historic reason why on non-Windows, stderr is used
42
+	// for output. However, on Windows it makes no sense and there is no need.
43
+	logrus.SetOutput(stdout)
44
+
45
+	// Provider ID: {6996f090-c5de-5082-a81e-5841acc3a635}
46
+	// Hook isn't closed explicitly, as it will exist until process exit.
47
+	// GUID is generated based on name - see Microsoft/go-winio/tools/etw-provider-gen.
48
+	if hook, err := etwlogrus.NewHook("Moby"); err == nil {
49
+		logrus.AddHook(hook)
50
+	}
51
+	return
52
+}