From 5127dfb4fcaafda0c0bc826af33ff61fce83f885 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Mon, 27 Apr 2015 22:00:34 -0700 Subject: [PATCH] simple console --- console.go | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 console.go diff --git a/console.go b/console.go new file mode 100644 index 000000000..9a6af599c --- /dev/null +++ b/console.go @@ -0,0 +1,163 @@ +/* + * Mini Copy (C) 2015 Minio, Inc. + * + * 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 main + +import ( + "fmt" + "os" + "sync" + + "path/filepath" +) + +type logLevel int + +const ( + levelUnknown logLevel = iota + levelDebug + levelInfo + levelError + levelFatal +) + +var ( + mutex = &sync.RWMutex{} + + // Fatal prints a error message and exits + Fatal = func(a ...interface{}) { privatePrint(levelFatal, a...) } + // Fatalln prints a error message with a new line and exits + Fatalln = func(a ...interface{}) { privatePrintln(levelFatal, a...) } + // Fatalf prints a error message with formatting and exits + Fatalf = func(f string, a ...interface{}) { privatePrintf(levelFatal, f, a...) } + + // Error prints a error message + Error = func(a ...interface{}) { privatePrint(levelError, a...) } + // Errorln prints a error message with a new line + Errorln = func(a ...interface{}) { privatePrintln(levelError, a...) } + // Errorf prints a error message with formatting + Errorf = func(f string, a ...interface{}) { privatePrintf(levelError, f, a...) } + + // Info prints a informational message + Info = func(a ...interface{}) { privatePrint(levelInfo, a...) } + // Infoln prints a informational message with a new line + Infoln = func(a ...interface{}) { privatePrintln(levelInfo, a...) } + // Infof prints a informational message with formatting + Infof = func(f string, a ...interface{}) { privatePrintf(levelInfo, f, a...) } + + // Debug prints a debug message + Debug = func(a ...interface{}) { privatePrint(levelDebug, a...) } + // Debugln prints a debug message with a new line + Debugln = func(a ...interface{}) { privatePrintln(levelDebug, a...) } + // Debugf prints a debug message with formatting + Debugf = func(f string, a ...interface{}) { privatePrintf(levelDebug, f, a...) } +) + +var ( + // print prints a message prefixed with message type and program name + privatePrint = func(l logLevel, a ...interface{}) { + switch l { + case levelDebug: + mutex.Lock() + fmt.Fprint(os.Stderr, ProgramName()+": ") + fmt.Fprint(os.Stderr, a...) + mutex.Unlock() + case levelInfo: + mutex.Lock() + fmt.Print(ProgramName() + ": ") + fmt.Print(a...) + mutex.Unlock() + case levelError: + mutex.Lock() + fmt.Fprint(os.Stderr, ProgramName()+": ") + fmt.Fprint(os.Stderr, a...) + mutex.Unlock() + case levelFatal: + mutex.Lock() + fmt.Fprint(os.Stderr, ProgramName()+": ") + fmt.Fprint(os.Stderr, a...) + mutex.Unlock() + os.Exit(1) + default: + fmt.Print(a...) + } + } + + // println - same as print with a new line + privatePrintln = func(l logLevel, a ...interface{}) { + switch l { + case levelDebug: + mutex.Lock() + fmt.Fprint(os.Stderr, ProgramName()+": ") + fmt.Fprintln(os.Stderr, a...) + mutex.Unlock() + case levelInfo: + mutex.Lock() + fmt.Print(ProgramName() + ": ") + fmt.Println(a...) + mutex.Unlock() + case levelError: + mutex.Lock() + fmt.Fprint(os.Stderr, ProgramName()+": ") + fmt.Fprintln(os.Stderr, a...) + mutex.Unlock() + case levelFatal: + mutex.Lock() + fmt.Fprint(os.Stderr, ProgramName()+": ") + fmt.Fprintln(os.Stderr, a...) + mutex.Unlock() + os.Exit(1) + default: + fmt.Print(a...) + } + } + + // printf - same as print, but takes a format specifier + privatePrintf = func(l logLevel, f string, a ...interface{}) { + switch l { + case levelDebug: + mutex.Lock() + fmt.Fprint(os.Stderr, ProgramName()+": ") + fmt.Fprintf(os.Stderr, f, a...) + mutex.Unlock() + case levelInfo: + mutex.Lock() + fmt.Print(ProgramName() + ": ") + fmt.Printf(f, a...) + mutex.Unlock() + case levelError: + mutex.Lock() + fmt.Fprint(os.Stderr, ProgramName()+": ") + fmt.Fprintf(os.Stderr, f, a...) + mutex.Unlock() + + case levelFatal: + mutex.Lock() + fmt.Fprint(os.Stderr, ProgramName()+": ") + fmt.Fprintf(os.Stderr, f, a...) + mutex.Unlock() + os.Exit(1) + default: + fmt.Printf(f, a...) + } + } +) + +// ProgramName - return the name of the executable program +func ProgramName() string { + _, progName := filepath.Split(os.Args[0]) + return progName +}