k3s/pkg/cli/cmds/log.go
Brad Davidson dc18ef2e51 Refactor log and reaper exec to omit MAINPID
Using MAINPID breaks systemd's exit detection, as it stops watching the
original pid, but is unable to watch the new pid as it is not a child
of systemd itself. The best we can do is just notify when execing the child
process.

We also need to consolidate forking into a sigle place so that we don't
end up with multiple levels of child processes if both redirecting log
output and reaping child processes.

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
2021-10-12 13:35:10 -07:00

80 lines
1.6 KiB
Go

package cmds
import (
"flag"
"fmt"
"strconv"
"sync"
"time"
"github.com/urfave/cli"
)
type Log struct {
VLevel int
VModule string
LogFile string
AlsoLogToStderr bool
}
var (
LogConfig Log
VLevel = cli.IntFlag{
Name: "v",
Usage: "(logging) Number for the log level verbosity",
Destination: &LogConfig.VLevel,
}
VModule = cli.StringFlag{
Name: "vmodule",
Usage: "(logging) Comma-separated list of pattern=N settings for file-filtered logging",
Destination: &LogConfig.VModule,
}
LogFile = cli.StringFlag{
Name: "log,l",
Usage: "(logging) Log to file",
Destination: &LogConfig.LogFile,
}
AlsoLogToStderr = cli.BoolFlag{
Name: "alsologtostderr",
Usage: "(logging) Log to standard error as well as file (if set)",
Destination: &LogConfig.AlsoLogToStderr,
}
logSetupOnce sync.Once
)
func InitLogging() error {
var rErr error
logSetupOnce.Do(func() {
if err := forkIfLoggingOrReaping(); err != nil {
rErr = err
return
}
if err := checkUnixTimestamp(); err != nil {
rErr = err
return
}
setupLogging()
})
return rErr
}
func checkUnixTimestamp() error {
timeNow := time.Now()
// check if time before 01/01/1980
if timeNow.Before(time.Unix(315532800, 0)) {
return fmt.Errorf("server time isn't set properly: %v", timeNow)
}
return nil
}
func setupLogging() {
flag.Set("v", strconv.Itoa(LogConfig.VLevel))
flag.Set("vmodule", LogConfig.VModule)
flag.Set("alsologtostderr", strconv.FormatBool(Debug))
flag.Set("logtostderr", strconv.FormatBool(!Debug))
}