From 7695f42e20470b804e3554ca0050c312ef6289fb Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Wed, 27 Mar 2019 13:52:22 +0100 Subject: [PATCH] started new logger output --- cmd/mark2web/main.go | 33 ++++++++++++++++++--------------- pkg/jobm/jobmanager.go | 3 +-- pkg/logger/logger.go | 24 +++++++++++++++--------- pkg/progress/bar.go | 26 ++++++++++++++++++++++++-- 4 files changed, 58 insertions(+), 28 deletions(-) diff --git a/cmd/mark2web/main.go b/cmd/mark2web/main.go index cf2fba1..ba5b1f4 100644 --- a/cmd/mark2web/main.go +++ b/cmd/mark2web/main.go @@ -46,57 +46,60 @@ func main() { logger.SetLogLevel(level) if progressBars != nil && *progressBars { - progress.Init() + progress.Start() } if inDir == nil || *inDir == "" { - logger.Log.Panic("input directory not specified") + logger.E("input directory not specified") + os.Exit(1) } iDir := path.Clean(*inDir) inDir = &iDir - logger.Log.Infof("input directory: %s", *inDir) + logger.I("input directory: %s", *inDir) if outDir == nil || *outDir == "" { - logger.Log.Panic("output directory not specified") + logger.E("output directory not specified") + os.Exit(1) } oDir := path.Clean(*outDir) outDir = &oDir - logger.Log.Infof("output directory: %s", *outDir) + logger.I("output directory: %s", *outDir) if createOutDir != nil && *createOutDir { if _, err := os.Stat(*outDir); os.IsNotExist(err) { - logger.Log.Debugf("output directory '%s' does not exist", *outDir) - logger.Log.Debugf("trying to create output directory: %s", *outDir) + logger.D("output directory '%s' does not exist", *outDir) + logger.D("trying to create output directory: %s", *outDir) err := os.MkdirAll(*outDir, 0755) if err != nil { logger.Log.Panic(err) } - logger.Log.Noticef("created output directory: %s", *outDir) + logger.I("created output directory: %s", *outDir) } else { - logger.Log.Noticef("output directory '%s' already exists", *outDir) + logger.I("output directory '%s' already exists", *outDir) } } if fD, err := os.Stat(*outDir); os.IsNotExist(err) { - logger.Log.Panicf("output directory '%s' does not exist, try -create parameter or create manually", *outDir) + logger.Eexit(err, "output directory '%s' does not exist, try -create parameter or create manually", *outDir) } else { if fD == nil { - logger.Log.Panicf("something went wrong, could not get file handle for output dir %s", *outDir) + logger.P("something went wrong, could not get file handle for output dir %s", *outDir) } else if !fD.IsDir() { - logger.Log.Panicf("output directory '%s' is not a directory", *outDir) + logger.E("output directory '%s' is not a directory", *outDir) + os.Exit(1) } } - logger.Log.Debug("reading global config...") + logger.D("reading global config...") configFilename := *inDir + "/config.yml" err := mark2web.Config.ReadFromFile(configFilename) if err != nil { - logger.Log.Panicf("could not read file '%s': %s", configFilename, err) + logger.Eexit(err, "could not read file '%s'", configFilename) } mark2web.Config.Directories.Input = *inDir mark2web.Config.Directories.Output = *outDir - logger.Log.Debugf("reading input directory %s", *inDir) + logger.D("reading input directory %s", *inDir) defaultTemplate := "base.html" defaultInputFile := "README.md" diff --git a/pkg/jobm/jobmanager.go b/pkg/jobm/jobmanager.go index d02dcb9..19999ae 100644 --- a/pkg/jobm/jobmanager.go +++ b/pkg/jobm/jobmanager.go @@ -3,7 +3,6 @@ package jobm import ( "runtime" "sync" - "time" "gitbase.de/apairon/mark2web/pkg/progress" ) @@ -52,7 +51,7 @@ func Enqueue(jobs ...Job) { // Wait will wait for all jobs to finish func Wait() { close(jobChan) - time.Sleep(time.Millisecond * 500) + progress.Stop() wg.Wait() } diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 78b12f4..974d56f 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -11,6 +11,8 @@ import ( var Log = logging.MustGetLogger("myLogger") var logBackendLeveled logging.LeveledBackend +var Prefix = "" + // SetLogLevel sets log level for global logger (debug, info, notice, warning, error) func SetLogLevel(level string) { logBackendLevel := logging.INFO @@ -44,7 +46,7 @@ func SetLogLevel(level string) { func configureLogger() { logBackend := logging.NewLogBackend(os.Stderr, "", 0) logBackendFormatter := logging.NewBackendFormatter(logBackend, logging.MustStringFormatter( - `%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`, + `%{color}%{time:2006-01-02 15:04:05.000} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`, )) logBackendLeveled = logging.AddModuleLevel(logBackendFormatter) logBackendLeveled.SetLevel(logging.NOTICE, "") @@ -59,34 +61,38 @@ func init() { configureLogger() } +func prefix() string { + return Prefix +} + // D is shorthand for Debugf func D(format string, args ...interface{}) { - Log.Debugf(format, args...) + Log.Debugf(prefix()+format, args...) } // I is shorthand for Infof func I(format string, args ...interface{}) { - Log.Infof(format, args...) + Log.Infof(prefix()+format, args...) } // N is shorthand for Noticef func N(format string, args ...interface{}) { - Log.Noticef(format, args...) + Log.Noticef(prefix()+format, args...) } // W is shorthand for Warningf func W(format string, args ...interface{}) { - Log.Warningf(format, args...) + Log.Warningf(prefix()+format, args...) } // E is shorthand for Errorf func E(format string, args ...interface{}) { - Log.Errorf(format, args...) + Log.Errorf(prefix()+format, args...) } // P is shorthand for Panicf func P(format string, args ...interface{}) { - Log.Panicf(format, args...) + Log.Panicf(prefix()+format, args...) } // Eerr is shorthand for @@ -96,7 +102,7 @@ func P(format string, args ...interface{}) { func Eerr(err error, format string, args ...interface{}) { if err != nil { args = append(args, err) - Log.Errorf(format+"\nError: %s", args...) + Log.Errorf(prefix()+format+" (Error: %s)", args...) } } @@ -119,6 +125,6 @@ func Eexit(err error, format string, args ...interface{}) { func Perr(err error, format string, args ...interface{}) { if err != nil { args = append(args, err) - Log.Panicf(format+"\nError: %s", args...) + Log.Panicf(prefix()+format+" (Error: %s)", args...) } } diff --git a/pkg/progress/bar.go b/pkg/progress/bar.go index 72e6130..10f3531 100644 --- a/pkg/progress/bar.go +++ b/pkg/progress/bar.go @@ -2,8 +2,10 @@ package progress import ( "fmt" + "time" "gitbase.de/apairon/mark2web/pkg/helper" + "gitbase.de/apairon/mark2web/pkg/logger" "github.com/gosuri/uiprogress" "github.com/mattn/go-tty" ) @@ -23,8 +25,12 @@ var OverallTotal = 0 // OverallDone is number of done jobs var OverallDone = 0 -// Init initializes the bar drawing -func Init() { +func init() { + updateLoggerPrefix() +} + +// Start initializes the bar drawing +func Start() { if t, err := tty.Open(); err == nil && t != nil { terminalWidth, _, _ = t.Size() t.Close() @@ -33,9 +39,15 @@ func Init() { initialized = true } +func updateLoggerPrefix() { + logger.Prefix = fmt.Sprintf("%3d/%3d: ", OverallDone, OverallTotal) +} + // IncrTotal increases the total jobs for the bar func IncrTotal(barname string) { OverallTotal++ + updateLoggerPrefix() + if initialized { _bar := bars[barname] if _bar == nil { @@ -60,6 +72,8 @@ func IncrTotal(barname string) { // IncrDone increases to done jobs counter func IncrDone(barname string) { OverallDone++ + updateLoggerPrefix() + if initialized { bars[barname].Bar.Incr() bars[barname].Description = "" @@ -72,3 +86,11 @@ func DescribeCurrent(barname, description string) { bars[barname].Description = description } } + +// Stop stops the bar drawing +func Stop() { + if initialized { + time.Sleep(time.Millisecond * 200) + uiprogress.Stop() + } +}