github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/nlog/init.go (about) 1 // Package nlog - aistore logger, provides buffering, timestamping, writing, and 2 // flushing/syncing/rotating 3 /* 4 * Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. 5 */ 6 package nlog 7 8 import ( 9 "fmt" 10 "os" 11 "path/filepath" 12 "strings" 13 "sync" 14 "sync/atomic" 15 "time" 16 ) 17 18 var ( 19 host = "unknown" 20 // assorted filenames that we don't want to show up 21 redactFnames = map[string]int{ 22 "target_stats": 0, 23 "proxy_stats": 0, 24 "common_stats": 0, 25 "err": 0, 26 } 27 sevText = []string{sevInfo: "INFO", sevErr: "ERROR"} 28 ) 29 30 var ( 31 pool sync.Pool // bytes.Buffer mem pool (errors and warnings only) 32 33 nlogs [3]*nlog 34 35 logDir string 36 arg0 string 37 aisrole string 38 title string 39 40 pid int 41 42 onceInitFiles sync.Once 43 44 unitTests atomic.Bool 45 46 stopping atomic.Bool // true when exiting 47 ) 48 49 func init() { 50 pid = os.Getpid() 51 arg0 = filepath.Base(os.Args[0]) 52 if h, err := os.Hostname(); err == nil { 53 host = _shortHost(h) 54 } 55 } 56 57 func initFiles() { 58 if logDir == "" { 59 unitTests.Store(true) 60 logDir = filepath.Join(os.TempDir(), "aislogs") 61 } 62 if err := fcreateAll(sevErr); err != nil { 63 panic(fmt.Sprintf("FATAL: unable to create logs in %q: %v", logDir, err)) 64 } 65 } 66 67 func fcreateAll(sev severity) error { 68 now := time.Now() 69 for s := sev; s >= sevInfo && nlogs[s] == nil; s-- { 70 if s == sevWarn { 71 continue 72 } 73 nlog := newNlog(s) 74 if err := nlog.rotate(now); err != nil { 75 return err 76 } 77 nlogs[s] = nlog 78 } 79 return nil 80 } 81 82 func sname() (name string) { 83 name = arg0 84 if name == "aisnode" && aisrole != "" { 85 name = "ais" + aisrole 86 } 87 return 88 } 89 90 func _shortHost(hostname string) string { 91 if i := strings.Index(hostname, "."); i >= 0 { 92 return hostname[:i] 93 } 94 if len(hostname) < 16 || strings.IndexByte(hostname, '-') < 0 { 95 return hostname 96 } 97 // (e.g. "runner-r9rhlq8--project-4149-concurrent-0") 98 parts := strings.Split(hostname, "-") 99 if len(parts) < 2 { 100 return hostname 101 } 102 if parts[1] != "" || len(parts) == 2 { 103 return parts[0] + "-" + parts[1] 104 } 105 return parts[0] + "-" + parts[2] 106 } 107 108 func fcreate(tag string, t time.Time) (f *os.File, fname string, err error) { 109 err = os.MkdirAll(logDir, os.ModePerm) 110 if err != nil { 111 return 112 } 113 name, link := logfname(tag, t) 114 fname = filepath.Join(logDir, name) 115 f, err = os.OpenFile(fname, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o640) 116 if err != nil { 117 return 118 } 119 // re-symlink 120 symlink := filepath.Join(logDir, link) 121 os.Remove(symlink) 122 os.Symlink(name, symlink) 123 return 124 }