github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/cos/log_module.go (about) 1 // Package cos provides common low-level types and utilities for all aistore projects. 2 /* 3 * Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package cos 6 7 import ( 8 "fmt" 9 "strconv" 10 11 "github.com/NVIDIA/aistore/cmn/debug" 12 ) 13 14 // see related: duration.go, size.go 15 16 const ferl = "invalid log.level %q (%d, %08b)" 17 18 const ( 19 SmoduleTransport = 1 << iota 20 SmoduleAIS 21 SmoduleMemsys 22 SmoduleCluster 23 SmoduleFS 24 SmoduleReb 25 SmoduleEC 26 SmoduleStats 27 SmoduleIOS 28 SmoduleXs 29 SmoduleBackend 30 SmoduleSpace 31 SmoduleMirror 32 SmoduleDsort 33 SmoduleDload 34 SmoduleETL 35 SmoduleS3 36 37 // NOTE: the last 38 _smoduleLast 39 ) 40 41 const maxLevel = 5 42 43 // NOTE: keep in-sync with the above 44 var Smodules = []string{ 45 "transport", "ais", "memsys", "cluster", "fs", "reb", "ec", "stats", 46 "ios", "xs", "backend", "space", "mirror", "dsort", "downloader", "etl", 47 "s3", 48 } 49 50 type LogLevel string 51 52 func (l LogLevel) Parse() (level, modules int) { 53 value, err := strconv.Atoi(string(l)) 54 debug.AssertNoErr(err) 55 level, modules = value&0x7, value>>3 56 return 57 } 58 59 func (l *LogLevel) Set(level int, sm []string) { 60 var modules int 61 for i, a := range Smodules { 62 for _, b := range sm { 63 if a == b { 64 modules |= 1 << i 65 } 66 } 67 } 68 *l = LogLevel(strconv.Itoa(level + modules<<3)) 69 } 70 71 func (l LogLevel) Validate() (err error) { 72 level, modules := l.Parse() 73 if level == 0 || level > maxLevel || modules > _smoduleLast { 74 err = fmt.Errorf(ferl, string(l), level, modules) 75 } 76 return 77 } 78 79 func (l LogLevel) String() (s string) { 80 var ( 81 ms string 82 n int 83 level, modules = l.Parse() 84 ) 85 s = strconv.Itoa(level) 86 if modules == 0 { 87 return 88 } 89 for i, sm := range Smodules { 90 if modules&(1<<i) != 0 { 91 ms += "," + sm 92 n++ 93 } 94 } 95 debug.Assert(n > 0, fmt.Sprintf(ferl, string(l), level, modules)) 96 s += " (module" + Plural(n) + ": " + ms[1:] + ")" 97 return 98 }