Skip to content

Commit

Permalink
Merge pull request #66 from vincepri/defaults-once
Browse files Browse the repository at this point in the history
Set default values only once
  • Loading branch information
k8s-ci-robot committed May 21, 2019
2 parents e88f730 + 402fa4e commit 89e63fd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
31 changes: 23 additions & 8 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,21 +404,36 @@ func init() {
go logging.flushDaemon()
}

// InitFlags is for explicitly initializing the flags
var initDefaultsOnce sync.Once

// InitFlags is for explicitly initializing the flags.
func InitFlags(flagset *flag.FlagSet) {

// Initialize defaults.
initDefaultsOnce.Do(func() {
logging.logDir = ""
logging.logFile = ""
logging.logFileMaxSizeMB = 1800
logging.toStderr = true
logging.alsoToStderr = false
logging.skipHeaders = false
logging.skipLogHeaders = false
})

if flagset == nil {
flagset = flag.CommandLine
}
flagset.StringVar(&logging.logDir, "log_dir", "", "If non-empty, write log files in this directory")
flagset.StringVar(&logging.logFile, "log_file", "", "If non-empty, use this log file")
flagset.Uint64Var(&logging.logFileMaxSizeMB, "log_file_max_size", 1800,

flagset.StringVar(&logging.logDir, "log_dir", logging.logDir, "If non-empty, write log files in this directory")
flagset.StringVar(&logging.logFile, "log_file", logging.logFile, "If non-empty, use this log file")
flagset.Uint64Var(&logging.logFileMaxSizeMB, "log_file_max_size", logging.logFileMaxSizeMB,
"Defines the maximum size a log file can grow to. Unit is megabytes. "+
"If the value is 0, the maximum file size is unlimited.")
flagset.BoolVar(&logging.toStderr, "logtostderr", true, "log to standard error instead of files")
flagset.BoolVar(&logging.alsoToStderr, "alsologtostderr", false, "log to standard error as well as files")
flagset.BoolVar(&logging.toStderr, "logtostderr", logging.toStderr, "log to standard error instead of files")
flagset.BoolVar(&logging.alsoToStderr, "alsologtostderr", logging.alsoToStderr, "log to standard error as well as files")
flagset.Var(&logging.verbosity, "v", "number for the log level verbosity")
flagset.BoolVar(&logging.skipHeaders, "skip_headers", false, "If true, avoid header prefixes in the log messages")
flagset.BoolVar(&logging.skipLogHeaders, "skip_log_headers", false, "If true, avoid headers when opening log files")
flagset.BoolVar(&logging.skipHeaders, "skip_headers", logging.skipHeaders, "If true, avoid header prefixes in the log messages")
flagset.BoolVar(&logging.skipLogHeaders, "skip_log_headers", logging.skipLogHeaders, "If true, avoid headers when opening log files")
flagset.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr")
flagset.Var(&logging.vmodule, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging")
flagset.Var(&logging.traceLocation, "log_backtrace_at", "when logging hits line file:N, emit a stack trace")
Expand Down
17 changes: 17 additions & 0 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package klog

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
stdLog "log"
Expand Down Expand Up @@ -533,3 +534,19 @@ func TestFileSizeCheck(t *testing.T) {
}
}
}

func TestInitFlags(t *testing.T) {
fs1 := flag.NewFlagSet("test1", flag.PanicOnError)
InitFlags(fs1)
fs1.Set("log_dir", "/test1")
fs1.Set("log_file_max_size", "1")
fs2 := flag.NewFlagSet("test2", flag.PanicOnError)
InitFlags(fs2)
if logging.logDir != "/test1" {
t.Fatalf("Expected log_dir to be %q, got %q", "/test1", logging.logDir)
}
fs2.Set("log_file_max_size", "2048")
if logging.logFileMaxSizeMB != 2048 {
t.Fatal("Expected log_file_max_size to be 2048")
}
}

0 comments on commit 89e63fd

Please sign in to comment.