2019-08-08 05:45:54 +00:00
|
|
|
package cmds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2020-04-27 16:42:52 +00:00
|
|
|
"sync"
|
2019-08-08 05:45:54 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-12 22:00:40 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-08-29 19:46:55 +00:00
|
|
|
"github.com/urfave/cli"
|
2019-08-08 05:45:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Log struct {
|
|
|
|
VLevel int
|
|
|
|
VModule string
|
|
|
|
LogFile string
|
|
|
|
AlsoLogToStderr bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
LogConfig Log
|
|
|
|
|
2023-01-31 20:57:48 +00:00
|
|
|
VLevel = &cli.IntFlag{
|
2019-08-08 05:45:54 +00:00
|
|
|
Name: "v",
|
2019-10-27 05:53:25 +00:00
|
|
|
Usage: "(logging) Number for the log level verbosity",
|
2019-08-08 05:45:54 +00:00
|
|
|
Destination: &LogConfig.VLevel,
|
|
|
|
}
|
2023-01-31 20:57:48 +00:00
|
|
|
VModule = &cli.StringFlag{
|
2019-08-08 05:45:54 +00:00
|
|
|
Name: "vmodule",
|
2022-08-02 20:51:16 +00:00
|
|
|
Usage: "(logging) Comma-separated list of FILE_PATTERN=LOG_LEVEL settings for file-filtered logging",
|
2019-08-08 05:45:54 +00:00
|
|
|
Destination: &LogConfig.VModule,
|
|
|
|
}
|
2023-01-31 20:57:48 +00:00
|
|
|
LogFile = &cli.StringFlag{
|
2019-08-08 05:45:54 +00:00
|
|
|
Name: "log,l",
|
2019-10-27 05:53:25 +00:00
|
|
|
Usage: "(logging) Log to file",
|
2019-08-08 05:45:54 +00:00
|
|
|
Destination: &LogConfig.LogFile,
|
|
|
|
}
|
2023-01-31 20:57:48 +00:00
|
|
|
AlsoLogToStderr = &cli.BoolFlag{
|
2019-08-08 05:45:54 +00:00
|
|
|
Name: "alsologtostderr",
|
2019-10-27 05:53:25 +00:00
|
|
|
Usage: "(logging) Log to standard error as well as file (if set)",
|
2019-08-08 05:45:54 +00:00
|
|
|
Destination: &LogConfig.AlsoLogToStderr,
|
|
|
|
}
|
2020-04-27 16:42:52 +00:00
|
|
|
|
|
|
|
logSetupOnce sync.Once
|
2019-08-08 05:45:54 +00:00
|
|
|
)
|
|
|
|
|
2020-08-29 19:46:55 +00:00
|
|
|
func InitLogging() error {
|
|
|
|
var rErr error
|
|
|
|
logSetupOnce.Do(func() {
|
2021-10-08 19:47:20 +00:00
|
|
|
if err := forkIfLoggingOrReaping(); err != nil {
|
|
|
|
rErr = err
|
2020-08-29 19:46:55 +00:00
|
|
|
return
|
2020-04-27 16:42:52 +00:00
|
|
|
}
|
2020-08-29 19:46:55 +00:00
|
|
|
|
|
|
|
if err := checkUnixTimestamp(); err != nil {
|
|
|
|
rErr = err
|
|
|
|
return
|
2020-04-27 16:42:52 +00:00
|
|
|
}
|
2020-08-29 19:46:55 +00:00
|
|
|
|
|
|
|
setupLogging()
|
|
|
|
})
|
|
|
|
return rErr
|
2019-08-08 05:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2020-04-29 18:36:44 +00:00
|
|
|
flag.Set("alsologtostderr", strconv.FormatBool(Debug))
|
|
|
|
flag.Set("logtostderr", strconv.FormatBool(!Debug))
|
2022-01-12 22:00:40 +00:00
|
|
|
if Debug {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
2019-08-08 05:45:54 +00:00
|
|
|
}
|