go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/logging/sdlogger/tracker.go (about) 1 // Copyright 2018 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sdlogger 16 17 import ( 18 "sync/atomic" 19 ) 20 21 // SeverityTracker wraps LogEntryWriter and observes severity of messages there. 22 type SeverityTracker struct { 23 Out LogEntryWriter 24 25 debug int32 26 info int32 27 warn int32 28 err int32 29 } 30 31 // Write is part of LogEntryWriter interface. 32 func (s *SeverityTracker) Write(l *LogEntry) { 33 s.Out.Write(l) 34 35 var ptr *int32 36 switch l.Severity { 37 case DebugSeverity: 38 ptr = &s.debug 39 case InfoSeverity: 40 ptr = &s.info 41 case WarningSeverity: 42 ptr = &s.warn 43 case ErrorSeverity: 44 ptr = &s.err 45 default: 46 return 47 } 48 49 if *ptr == 0 { 50 atomic.StoreInt32(ptr, 1) 51 } 52 } 53 54 // MaxSeverity returns maximum severity observed thus far or "". 55 func (s *SeverityTracker) MaxSeverity() Severity { 56 switch { 57 case atomic.LoadInt32(&s.err) == 1: 58 return ErrorSeverity 59 case atomic.LoadInt32(&s.warn) == 1: 60 return WarningSeverity 61 case atomic.LoadInt32(&s.info) == 1: 62 return InfoSeverity 63 case atomic.LoadInt32(&s.debug) == 1: 64 return DebugSeverity 65 default: 66 return UnknownSeverity 67 } 68 }