// ------------------------------------------------------------------------ // Project active2 // Active Thing (activething.com) git.activething.com/go // // File name level.go // Created by DEV // Modified 23/04/2024 // // Copyright 2024 activething.com // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ------------------------------------------------------------------------ package alogs import ( "math/bits" "strings" ) const( levelsCount int = 6 ) const( LevelData Level = 1 << iota LevelDebug LevelInfo LevelWarn LevelError LevelFatal LevelNone Level = 0 LevelAll Level = LevelData|LevelDebug|LevelInfo|LevelWarn|LevelError|LevelFatal ) type ( // Level // Enum type // Level uint8 ) func (lv Level) Count() int{ return bits.OnesCount8(uint8(lv)) } func (lv Level) String() string { return LevelNames(lv) } func (lv Level) List () []Level { return LevelList(lv) } func (lv Level) Add(lvl Level) Level { return lv | lvl } func (lv Level) Del(lvl Level) Level { return lv &^ lvl } func (lv Level) Has (lvl Level) bool { return lv&lvl != 0 } func (lv Level) HasData() bool { return lv&LevelData!=0 } func (lv Level) HasDebug() bool { return lv&LevelDebug!=0 } func (lv Level) HasInfo() bool { return lv&LevelInfo!=0 } func (lv Level) HasWarn() bool { return lv&LevelWarn!=0 } func (lv Level) HasError() bool { return lv&LevelError!=0 } func (lv Level) HasFatal() bool { return lv&LevelFatal!=0 } func (lv Level) isValid () bool { return lv <= LevelAll } func LevelList(level Level) []Level { var result [levelsCount]Level x := 0 for i := uint8(0); i <= uint8(levelsCount); i++ { c := Level(1 << i) if level.Has(c) { result[x] = c x++ } } return result[:x] } func LevelNames (level Level) string { if level == LevelNone { return "" } ix := 0 ls := make([]string, levelsCount) if level&LevelData != 0 { ls[ix]=LevelNameData ; ix++ } if level&LevelDebug!= 0 { ls[ix]=LevelNameDebug; ix++ } if level&LevelInfo != 0 { ls[ix]=LevelNameInfo ; ix++ } if level&LevelWarn != 0 { ls[ix]=LevelNameWarn ; ix++ } if level&LevelError!= 0 { ls[ix]=LevelNameError; ix++ } if level&LevelFatal!= 0 { ls[ix]=LevelNameFatal; ix++ } if ix==0 { return "" } return strings.Join(ls[:ix],levelNameSep) } func LevelWithNames(name string) Level { if name == "" { return LevelNone } lv := LevelNone for _,sl := range strings.Split(strings.ToUpper(name),levelNameSep) { switch sl { case LevelNameData : lv|=LevelData case LevelNameDebug : lv|=LevelDebug case LevelNameInfo : lv|=LevelInfo case LevelNameWarn : lv|=LevelWarn case LevelNameError : lv|=LevelError case LevelNameFatal : lv|=LevelFatal } } return lv }