Add `--log-file` flag for windows service.
@@ -42,6 +42,7 @@ var (
registerServiceFlag bool
unregisterServiceFlag bool
runServiceFlag bool
+ logFileFlag string
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
setStdHandle = kernel32.NewProc("SetStdHandle")
@@ -52,6 +53,8 @@ var (
service *handler
)
+const defaultServiceName = "containerd"
+
// serviceFlags returns an array of flags for configuring containerd to run
// as a Windows service under control of SCM.
func serviceFlags() []cli.Flag {
@@ -59,7 +62,7 @@ func serviceFlags() []cli.Flag {
cli.StringFlag{
Name: "service-name",
Usage: "Set the Windows service name",
- Value: "containerd",
+ Value: defaultServiceName,
},
cli.BoolFlag{
Name: "register-service",
@@ -74,14 +77,18 @@ func serviceFlags() []cli.Flag {
Usage: "",
Hidden: true,
},
+ cli.StringFlag{
+ Name: "log-file",
+ Usage: "Path to the containerd log file",
+ },
}
}
// applyPlatformFlags applies platform-specific flags.
func applyPlatformFlags(context *cli.Context) {
-
- if s := context.GlobalString("service-name"); s != "" {
- serviceNameFlag = s
+ serviceNameFlag = context.GlobalString("service-name")
+ if serviceNameFlag == "" {
+ serviceNameFlag = defaultServiceName
}
for _, v := range []struct {
name string
@@ -102,6 +109,7 @@ func applyPlatformFlags(context *cli.Context) {
} {
*v.d = context.GlobalBool(v.name)
}
+ logFileFlag = context.GlobalString("log-file")
}
type handler struct {
@@ -243,7 +251,15 @@ func registerUnregisterService(root string) (bool, error) {
return true, err
}
- logrus.SetOutput(ioutil.Discard)
+ logOutput := ioutil.Discard
+ if logFileFlag != "" {
+ f, err := os.OpenFile(logFileFlag, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+ if err != nil {
+ return true, errors.Wrapf(err, "open log file %q", logFileFlag)
+ }
+ logOutput = f
+ }
+ logrus.SetOutput(logOutput)
}
return false, nil
}
