mirror of https://github.com/maxcnunes/gaper.git
parent
c9da986360
commit
daee1bff02
|
@ -15,16 +15,18 @@ var (
|
|||
version = "1.0.1-dev"
|
||||
)
|
||||
|
||||
var logger = gaper.NewLogger("gaper")
|
||||
|
||||
func main() {
|
||||
logger := gaper.Logger()
|
||||
loggerVerbose := false
|
||||
|
||||
parseArgs := func(c *cli.Context) *gaper.Config {
|
||||
loggerVerbose = c.Bool("verbose")
|
||||
|
||||
return &gaper.Config{
|
||||
BinName: c.String("bin-name"),
|
||||
BuildPath: c.String("build-path"),
|
||||
BuildArgsMerged: c.String("build-args"),
|
||||
ProgramArgsMerged: c.String("program-args"),
|
||||
Verbose: c.Bool("verbose"),
|
||||
DisableDefaultIgnore: c.Bool("disable-default-ignore"),
|
||||
WatchItems: c.StringSlice("watch"),
|
||||
IgnoreItems: c.StringSlice("ignore"),
|
||||
|
@ -42,7 +44,7 @@ func main() {
|
|||
app.Action = func(c *cli.Context) {
|
||||
args := parseArgs(c)
|
||||
chOSSiginal := make(chan os.Signal, 2)
|
||||
logger.Verbose(args.Verbose)
|
||||
logger.Verbose(loggerVerbose)
|
||||
|
||||
if err := gaper.Run(args, chOSSiginal); err != nil {
|
||||
logger.Error(err)
|
||||
|
|
3
gaper.go
3
gaper.go
|
@ -29,8 +29,6 @@ var (
|
|||
NoRestartOnExit = "exit"
|
||||
)
|
||||
|
||||
var logger = NewLogger("gaper")
|
||||
|
||||
// exit statuses
|
||||
var exitStatusSuccess = 0
|
||||
var exitStatusError = 1
|
||||
|
@ -48,7 +46,6 @@ type Config struct {
|
|||
PollInterval int
|
||||
Extensions []string
|
||||
NoRestartOn string
|
||||
Verbose bool
|
||||
DisableDefaultIgnore bool
|
||||
WorkingDirectory string
|
||||
}
|
||||
|
|
30
logger.go
30
logger.go
|
@ -7,18 +7,26 @@ import (
|
|||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
// Logger used by gaper
|
||||
type Logger struct {
|
||||
// logger use by the whole package
|
||||
var logger = newLogger("gaper")
|
||||
|
||||
// Logger give access to external packages to use gaper logger
|
||||
func Logger() *LoggerEntity {
|
||||
return logger
|
||||
}
|
||||
|
||||
// LoggerEntity used by gaper
|
||||
type LoggerEntity struct {
|
||||
verbose bool
|
||||
logDebug *log.Logger
|
||||
logInfo *log.Logger
|
||||
logError *log.Logger
|
||||
}
|
||||
|
||||
// NewLogger creates a new logger
|
||||
func NewLogger(prefix string) *Logger {
|
||||
// newLogger creates a new logger
|
||||
func newLogger(prefix string) *LoggerEntity {
|
||||
prefix = "[" + prefix + "] "
|
||||
return &Logger{
|
||||
return &LoggerEntity{
|
||||
verbose: false,
|
||||
logDebug: log.New(os.Stdout, prefix, 0),
|
||||
logInfo: log.New(os.Stdout, color.CyanString(prefix), 0),
|
||||
|
@ -27,35 +35,35 @@ func NewLogger(prefix string) *Logger {
|
|||
}
|
||||
|
||||
// Verbose toggle this logger verbosity
|
||||
func (l *Logger) Verbose(verbose bool) {
|
||||
func (l *LoggerEntity) Verbose(verbose bool) {
|
||||
l.verbose = verbose
|
||||
}
|
||||
|
||||
// Debug logs a debug message
|
||||
func (l *Logger) Debug(v ...interface{}) {
|
||||
func (l *LoggerEntity) Debug(v ...interface{}) {
|
||||
if l.verbose {
|
||||
l.logDebug.Println(v...)
|
||||
}
|
||||
}
|
||||
|
||||
// Debugf logs a debug message with format
|
||||
func (l *Logger) Debugf(format string, v ...interface{}) {
|
||||
func (l *LoggerEntity) Debugf(format string, v ...interface{}) {
|
||||
if l.verbose {
|
||||
l.logDebug.Printf(format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
// Info logs a info message
|
||||
func (l *Logger) Info(v ...interface{}) {
|
||||
func (l *LoggerEntity) Info(v ...interface{}) {
|
||||
l.logInfo.Println(v...)
|
||||
}
|
||||
|
||||
// Error logs an error message
|
||||
func (l *Logger) Error(v ...interface{}) {
|
||||
func (l *LoggerEntity) Error(v ...interface{}) {
|
||||
l.logError.Println(v...)
|
||||
}
|
||||
|
||||
// Errorf logs and error message with format
|
||||
func (l *Logger) Errorf(format string, v ...interface{}) {
|
||||
func (l *LoggerEntity) Errorf(format string, v ...interface{}) {
|
||||
l.logError.Printf(format, v...)
|
||||
}
|
||||
|
|
|
@ -7,19 +7,19 @@ import (
|
|||
)
|
||||
|
||||
func TestLoggerDefault(t *testing.T) {
|
||||
l := NewLogger("gaper-test")
|
||||
l := newLogger("gaper-test")
|
||||
assert.Equal(t, l.verbose, false)
|
||||
}
|
||||
|
||||
func TestLoggerEnableVerbose(t *testing.T) {
|
||||
l := NewLogger("gaper-test")
|
||||
l := newLogger("gaper-test")
|
||||
l.Verbose(true)
|
||||
assert.Equal(t, l.verbose, true)
|
||||
}
|
||||
|
||||
func TestLoggerRunAllLogsWithoutVerbose(t *testing.T) {
|
||||
// no asserts, just checking it doesn't crash
|
||||
l := NewLogger("gaper-test")
|
||||
l := newLogger("gaper-test")
|
||||
l.Debug("debug")
|
||||
l.Debugf("%s", "debug")
|
||||
l.Info("info")
|
||||
|
@ -29,7 +29,7 @@ func TestLoggerRunAllLogsWithoutVerbose(t *testing.T) {
|
|||
|
||||
func TestLoggerRunAllLogsWithVerbose(t *testing.T) {
|
||||
// no asserts, just checking it doesn't crash
|
||||
l := NewLogger("gaper-test")
|
||||
l := newLogger("gaper-test")
|
||||
l.Verbose(true)
|
||||
l.Debug("debug")
|
||||
l.Debugf("%s", "debug")
|
||||
|
|
Loading…
Reference in New Issue