github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/util/log/logpb/severity.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package logpb 12 13 import ( 14 "strconv" 15 "strings" 16 ) 17 18 // Set is part of the pflag.Value interface. 19 func (s *Severity) Set(value string) error { 20 var threshold Severity 21 // Is it a known name? 22 if v, ok := SeverityByName(value); ok { 23 threshold = v 24 } else { 25 v, err := strconv.Atoi(value) 26 if err != nil { 27 return err 28 } 29 threshold = Severity(v) 30 } 31 *s = threshold 32 return nil 33 } 34 35 // IsSet returns true iff the severity was set to a non-unknown value. 36 func (s Severity) IsSet() bool { return s != Severity_UNKNOWN } 37 38 // Type implements the pflag.Value interface. 39 func (s Severity) Type() string { return "<severity>" } 40 41 // Name returns the string representation of the severity (i.e. ERROR, INFO). 42 func (s *Severity) Name() string { 43 return s.String() 44 } 45 46 // SeverityByName attempts to parse the passed in string into a severity. (i.e. 47 // ERROR, INFO). If it succeeds, the returned bool is set to true. 48 func SeverityByName(s string) (Severity, bool) { 49 s = strings.ToUpper(s) 50 if i, ok := Severity_value[s]; ok { 51 return Severity(i), true 52 } 53 switch s { 54 case "TRUE": 55 return Severity_INFO, true 56 case "FALSE": 57 return Severity_NONE, true 58 } 59 return 0, false 60 } 61 62 // UnmarshalYAML implements the yaml.Unmarshaler interface. 63 func (s *Severity) UnmarshalYAML(fn func(interface{}) error) error { 64 var sv string 65 if err := fn(&sv); err != nil { 66 return err 67 } 68 return s.Set(sv) 69 } 70 71 // MarshalYAML implements the yaml.Marshaler interface. 72 func (s Severity) MarshalYAML() (interface{}, error) { 73 return s.String(), nil 74 }