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