github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/cos/exit.go (about) 1 // Package cos provides common low-level types and utilities for all aistore projects. 2 /* 3 * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package cos 6 7 import ( 8 "flag" 9 "fmt" 10 "os" 11 12 "github.com/NVIDIA/aistore/cmn/nlog" 13 ) 14 15 const fatalPrefix = "FATAL ERROR: " 16 17 func _exit(msg string) { 18 fmt.Fprintln(os.Stderr, msg) 19 os.Exit(1) 20 } 21 22 func Exitf(f string, a ...any) { 23 msg := fmt.Sprintf(fatalPrefix+f, a...) 24 _exit(msg) 25 } 26 27 // +log 28 func ExitLogf(f string, a ...any) { 29 msg := fmt.Sprintf(fatalPrefix+f, a...) 30 if flag.Parsed() { 31 nlog.ErrorDepth(1, msg+"\n") 32 nlog.Flush(nlog.ActExit) 33 os.Exit(1) 34 } 35 _exit(msg) 36 } 37 38 func ExitLog(a ...any) { 39 msg := fatalPrefix + fmt.Sprint(a...) 40 if flag.Parsed() { 41 nlog.ErrorDepth(1, msg+"\n") 42 nlog.Flush(nlog.ActExit) 43 os.Exit(1) 44 } 45 _exit(msg) 46 } 47 48 // +condition 49 func ExitAssertLog(cond bool, a ...any) { 50 if !cond { 51 ExitLog(a...) 52 } 53 }