Merged in consistent-config-msgs (pull request #357)

revid/config: make config error messages more consistent

Approved-by: Saxon Milton <saxon.milton@gmail.com>
This commit is contained in:
Scott Barnard 2020-01-28 22:48:55 +00:00
commit f9d33f008e
1 changed files with 31 additions and 26 deletions

View File

@ -373,19 +373,19 @@ func (c *Config) Validate() error {
switch c.LogLevel {
case logger.Debug, logger.Info, logger.Warning, logger.Error, logger.Fatal:
default:
c.logInvalidField("LogLevel", defaultVerbosity)
c.LogLevel = defaultVerbosity
c.Logger.Log(logger.Info, pkg+"bad LogLevel mode defined, defaulting", "LogLevel", defaultVerbosity)
}
if c.CameraIP == "" {
c.Logger.Log(logger.Info, pkg+"no CameraIP defined, defaulting", "CameraIP", defaultCameraIP)
c.logInvalidField("CameraIP", defaultCameraIP)
c.CameraIP = defaultCameraIP
}
switch c.Input {
case InputRaspivid, InputV4L, InputFile, InputAudio, InputRTSP:
case NothingDefined:
c.Logger.Log(logger.Info, pkg+"no input type defined, defaulting", "input", defaultInput)
c.logInvalidField("Input", defaultInput)
c.Input = defaultInput
default:
return errors.New("bad input type defined in config")
@ -396,16 +396,16 @@ func (c *Config) Validate() error {
default:
switch c.Input {
case OutputAudio:
c.Logger.Log(logger.Info, pkg+"input is audio but no codec defined, defaulting", "inputCodec", defaultAudioInputCodec)
c.logInvalidField("InputCodec", defaultAudioInputCodec)
c.InputCodec = defaultAudioInputCodec
default:
c.Logger.Log(logger.Info, pkg+"no input codec defined, defaulting", "inputCodec", defaultInputCodec)
c.logInvalidField("InputCodec", defaultInputCodec)
c.InputCodec = defaultInputCodec
}
}
if c.Outputs == nil {
c.Logger.Log(logger.Info, pkg+"no output defined, defaulting", "output", defaultOutput)
c.logInvalidField("Outputs", defaultOutput)
c.Outputs = append(c.Outputs, defaultOutput)
}
@ -423,14 +423,14 @@ func (c *Config) Validate() error {
}
if c.BurstPeriod == 0 {
c.Logger.Log(logger.Info, pkg+"no burst period defined, defaulting", "burstPeriod", defaultBurstPeriod)
c.logInvalidField("BurstPeriod", defaultBurstPeriod)
c.BurstPeriod = defaultBurstPeriod
}
const maxMinFrames = 1000
switch {
case c.MinFrames == 0:
c.Logger.Log(logger.Info, pkg+"no min period defined, defaulting", "MinFrames", defaultMinFrames)
c.logInvalidField("MinFrames", defaultMinFrames)
c.MinFrames = defaultMinFrames
case c.MinFrames < 0:
return errors.New("refresh period is less than 0")
@ -439,87 +439,88 @@ func (c *Config) Validate() error {
}
if c.FrameRate <= 0 {
c.Logger.Log(logger.Info, pkg+"frame rate bad or unset, defaulting", "FrameRate", defaultFrameRate)
c.logInvalidField("FrameRate", defaultFrameRate)
c.FrameRate = defaultFrameRate
}
if c.WriteRate <= 0 {
c.Logger.Log(logger.Info, pkg+"write rate bad or unset, defaulting", "writeRate", defaultWriteRate)
c.logInvalidField("writeRate", defaultWriteRate)
c.WriteRate = defaultWriteRate
}
if c.ClipDuration == 0 {
c.Logger.Log(logger.Info, pkg+"no clip duration defined, defaulting", "ClipDuration", defaultClipDuration)
c.logInvalidField("ClipDuration", defaultClipDuration)
c.ClipDuration = defaultClipDuration
} else if c.ClipDuration < 0 {
return errors.New("clip duration is less than 0")
}
if c.RTPAddress == "" {
c.logInvalidField("RTPAddress", defaultRtpAddr)
c.RTPAddress = defaultRtpAddr
}
if c.RBMaxElements <= 0 {
c.Logger.Log(logger.Info, pkg+"RBMaxElements bad or unset, defaulting", "RBMaxElements", defaultRBMaxElements)
c.logInvalidField("RBMaxElements", defaultRBMaxElements)
c.RBMaxElements = defaultRBMaxElements
}
if c.RBCapacity <= 0 {
c.Logger.Log(logger.Info, pkg+"RBCapacity bad or unset, defaulting", "RBCapacity", defaultRBCapacity)
c.logInvalidField("RBCapacity", defaultRBCapacity)
c.RBCapacity = defaultRBCapacity
}
if c.RBWriteTimeout <= 0 {
c.Logger.Log(logger.Info, pkg+"RBWriteTimeout bad or unset, defaulting", "RBWriteTimeout", defaultRBWriteTimeout)
c.logInvalidField("RBWriteTimeout", defaultRBWriteTimeout)
c.RBWriteTimeout = defaultRBWriteTimeout
}
if c.PSITime <= 0 {
c.Logger.Log(logger.Info, pkg+"PSITime bad or unset, defaulting", "PSITime", defaultPSITime)
c.logInvalidField("PSITime", defaultPSITime)
c.PSITime = defaultPSITime
}
if c.MotionInterval <= 0 {
c.Logger.Log(logger.Info, pkg+"MotionInterval bad or unset, defaulting", "MotionInterval", defaultMotionInterval)
c.logInvalidField("MotionInterval", defaultMotionInterval)
c.MotionInterval = defaultMotionInterval
}
if c.MinFPS <= 0 {
c.Logger.Log(logger.Info, pkg+"MinFPS bad or unset, defaulting", "MinFPS", defaultMinFPS)
c.logInvalidField("MinFPS", defaultMinFPS)
c.MinFPS = defaultMinFPS
}
if c.KNNMinArea <= 0 {
c.Logger.Log(logger.Info, pkg+"KNNMinArea bad or unset, defaulting", "KNNMinArea", defaultKNNMinArea)
c.logInvalidField("KNNMinArea", defaultKNNMinArea)
c.KNNMinArea = defaultKNNMinArea
}
if c.KNNThreshold <= 0 {
c.Logger.Log(logger.Info, pkg+"KNNThreshold bad or unset, defaulting", "KNNThreshold", defaultKNNThreshold)
c.logInvalidField("KNNThreshold", defaultKNNThreshold)
c.KNNThreshold = defaultKNNThreshold
}
if c.KNNHistory == 0 {
c.Logger.Log(logger.Info, pkg+"KNNHistory bad or unset, defaulting", "KNNHistory", defaultKNNHistory)
c.logInvalidField("KNNHistory", defaultKNNHistory)
c.KNNHistory = defaultKNNHistory
}
if c.KNNKernel <= 0 {
c.Logger.Log(logger.Info, pkg+"KNNKernel bad or unset, defaulting", "KNNKernel", defaultKNNKernel)
c.logInvalidField("KNNKernel", defaultKNNKernel)
c.KNNKernel = defaultKNNKernel
}
if c.MOGMinArea <= 0 {
c.Logger.Log(logger.Info, pkg+"MOGMinArea bad or unset, defaulting", "MOGMinArea", defaultMOGMinArea)
c.logInvalidField("MOGMinArea", defaultMOGMinArea)
c.MOGMinArea = defaultMOGMinArea
}
if c.MOGThreshold <= 0 {
c.Logger.Log(logger.Info, pkg+"MOGThreshold bad or unset, defaulting", "MOGThreshold", defaultMOGThreshold)
c.logInvalidField("MOGThreshold", defaultMOGThreshold)
c.MOGThreshold = defaultMOGThreshold
}
if c.MOGHistory == 0 {
c.Logger.Log(logger.Info, pkg+"MOGHistory bad or unset, defaulting", "MOGHistory", defaultMOGHistory)
c.logInvalidField("MOGHistory", defaultMOGHistory)
c.MOGHistory = defaultMOGHistory
}
@ -529,13 +530,13 @@ func (c *Config) Validate() error {
return err
}
if os == raspian {
c.Logger.Log(logger.Info, "ShowWindows disabled on Raspbian GNU/Linux")
c.Logger.Log(logger.Info, pkg+"ShowWindows disabled on Raspbian GNU/Linux", "ShowWindows", false)
c.ShowWindows = false
}
}
if c.FileFPS <= 0 || (c.FileFPS > 0 && c.Input != InputFile) {
c.Logger.Log(logger.Info, pkg+"FileFPS bad or unset, defaulting", "FileFPS", defaultFileFPS)
c.logInvalidField("FileFPS", defaultFileFPS)
c.FileFPS = defaultFileFPS
}
@ -551,6 +552,10 @@ func osName() (string, error) {
return string(out)[6 : len(out)-2], nil
}
func (c *Config) logInvalidField(name string, def interface{}) {
c.Logger.Log(logger.Info, pkg+name+" bad or unset, defaulting", name, def)
}
// stringInSlice returns true if want is in slice.
func stringInSlice(want string, slice []string) bool {
for _, s := range slice {