2020-04-01 09:55:42 -05:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
|
|
|
https://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.
|
|
|
|
*/
|
|
|
|
|
2019-05-03 13:20:38 -05:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2020-08-24 09:48:29 -05:00
|
|
|
"fmt"
|
2019-05-03 13:20:38 -05:00
|
|
|
"io"
|
|
|
|
"log"
|
2019-10-04 10:31:41 -05:00
|
|
|
"os"
|
2019-05-03 13:20:38 -05:00
|
|
|
)
|
|
|
|
|
2019-10-04 10:31:41 -05:00
|
|
|
var (
|
|
|
|
debug = false
|
2020-05-01 12:08:29 -05:00
|
|
|
airshipLog = log.New(os.Stderr, "[airshipctl] ", log.LstdFlags)
|
2019-10-04 10:31:41 -05:00
|
|
|
)
|
2019-05-03 13:20:38 -05:00
|
|
|
|
|
|
|
// Init initializes settings related to logging
|
2019-06-06 10:57:33 -05:00
|
|
|
func Init(debugFlag bool, out io.Writer) {
|
|
|
|
debug = debugFlag
|
2020-08-24 09:48:29 -05:00
|
|
|
if debug {
|
|
|
|
airshipLog.SetFlags(log.LstdFlags | log.Llongfile)
|
|
|
|
}
|
2019-10-04 10:31:41 -05:00
|
|
|
airshipLog.SetOutput(out)
|
2019-05-03 13:20:38 -05:00
|
|
|
}
|
|
|
|
|
2020-08-25 16:29:19 -05:00
|
|
|
// DebugEnabled returns whether the debug level is set
|
|
|
|
func DebugEnabled() bool {
|
|
|
|
return debug
|
|
|
|
}
|
|
|
|
|
2019-05-03 13:20:38 -05:00
|
|
|
// Debug is a wrapper for log.Debug
|
|
|
|
func Debug(v ...interface{}) {
|
|
|
|
if debug {
|
2020-08-24 09:48:29 -05:00
|
|
|
writeLog(v...)
|
2019-05-03 13:20:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf is a wrapper for log.Debugf
|
|
|
|
func Debugf(format string, v ...interface{}) {
|
|
|
|
if debug {
|
2020-08-24 09:48:29 -05:00
|
|
|
writeLog(fmt.Sprintf(format, v...))
|
2019-05-03 13:20:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print is a wrapper for log.Print
|
|
|
|
func Print(v ...interface{}) {
|
2020-08-24 09:48:29 -05:00
|
|
|
writeLog(v...)
|
2019-05-03 13:20:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Printf is a wrapper for log.Printf
|
|
|
|
func Printf(format string, v ...interface{}) {
|
2020-08-24 09:48:29 -05:00
|
|
|
writeLog(fmt.Sprintf(format, v...))
|
2019-05-03 13:20:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatal is a wrapper for log.Fatal
|
|
|
|
func Fatal(v ...interface{}) {
|
2019-10-04 10:31:41 -05:00
|
|
|
airshipLog.Fatal(v...)
|
2019-05-03 13:20:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalf is a wrapper for log.Fatalf
|
|
|
|
func Fatalf(format string, v ...interface{}) {
|
2019-10-04 10:31:41 -05:00
|
|
|
airshipLog.Fatalf(format, v...)
|
2019-05-03 13:20:38 -05:00
|
|
|
}
|
2020-02-07 20:57:41 +04:00
|
|
|
|
|
|
|
// Writer returns log output writer object
|
|
|
|
func Writer() io.Writer {
|
|
|
|
return airshipLog.Writer()
|
|
|
|
}
|
2020-08-24 09:48:29 -05:00
|
|
|
|
|
|
|
func writeLog(v ...interface{}) {
|
|
|
|
if debug {
|
|
|
|
err := airshipLog.Output(3, fmt.Sprint(v...))
|
|
|
|
if err != nil {
|
|
|
|
log.Print(v...)
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
airshipLog.Print(v...)
|
|
|
|
}
|
|
|
|
}
|