forked from ruiaylin/pgparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
severity.go
67 lines (59 loc) · 1.56 KB
/
severity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package log
import (
"strconv"
"strings"
"sync/atomic"
)
const severityChar = "IWEF"
// get returns the value of the Severity.
func (s *Severity) get() Severity {
return Severity(atomic.LoadInt32((*int32)(s)))
}
// set sets the value of the Severity.
func (s *Severity) set(val Severity) {
atomic.StoreInt32((*int32)(s), int32(val))
}
// Set is part of the flag.Value interface.
func (s *Severity) Set(value string) error {
var threshold Severity
// Is it a known name?
if v, ok := SeverityByName(value); ok {
threshold = v
} else {
v, err := strconv.Atoi(value)
if err != nil {
return err
}
threshold = Severity(v)
}
s.set(threshold)
return nil
}
// Name returns the string representation of the severity (i.e. ERROR, INFO).
func (s *Severity) Name() string {
return s.String()
}
// SeverityByName attempts to parse the passed in string into a severity. (i.e.
// ERROR, INFO). If it succeeds, the returned bool is set to true.
func SeverityByName(s string) (Severity, bool) {
s = strings.ToUpper(s)
if i, ok := Severity_value[s]; ok {
return Severity(i), true
}
switch s {
case "TRUE":
return Severity_INFO, true
case "FALSE":
return Severity_NONE, true
}
return 0, false
}