github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/logger/utils.go (about) 1 // Copyright (c) 2015-2021 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package logger 19 20 import ( 21 "context" 22 "errors" 23 "fmt" 24 "net/http" 25 "regexp" 26 "runtime" 27 28 "github.com/minio/minio/internal/color" 29 ) 30 31 var ansiRE = regexp.MustCompile("(\x1b[^m]*m)") 32 33 // Print ANSI Control escape 34 func ansiEscape(format string, args ...interface{}) { 35 Esc := "\x1b" 36 fmt.Printf("%s%s", Esc, fmt.Sprintf(format, args...)) 37 } 38 39 func ansiMoveRight(n int) { 40 if runtime.GOOS == "windows" { 41 return 42 } 43 if color.IsTerminal() { 44 ansiEscape("[%dC", n) 45 } 46 } 47 48 func ansiSaveAttributes() { 49 if runtime.GOOS == "windows" { 50 return 51 } 52 if color.IsTerminal() { 53 ansiEscape("7") 54 } 55 } 56 57 func ansiRestoreAttributes() { 58 if runtime.GOOS == "windows" { 59 return 60 } 61 if color.IsTerminal() { 62 ansiEscape("8") 63 } 64 } 65 66 // logIgnoreError if true,the error will ignore. 67 func logIgnoreError(err error) bool { 68 return err == nil || errors.Is(err, context.Canceled) || errors.Is(err, http.ErrServerClosed) || err.Error() == "drive not found" 69 }