diff --git a/cmd/root.go b/cmd/root.go index a868975..752aba0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -58,6 +58,7 @@ var defaultRDirs = []string{ var defaultDirs = []string{} var triggerInterval int var colored bool +var debug bool func init() { rootCmd.PersistentFlags().BoolVarP(&logPS, "procevents", "p", true, "print new processes to stdout") @@ -66,12 +67,13 @@ func init() { rootCmd.PersistentFlags().StringArrayVarP(&dirs, "dirs", "d", defaultDirs, "watch these dirs") rootCmd.PersistentFlags().IntVarP(&triggerInterval, "interval", "i", 100, "scan every 'interval' milliseconds for new processes") rootCmd.PersistentFlags().BoolVarP(&colored, "color", "c", true, "color the printed events") + rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "", false, "print detailed error messages") log.SetOutput(os.Stdout) } func root(cmd *cobra.Command, args []string) { - logger := logging.NewLogger() + logger := logging.NewLogger(debug) cfg := &config.Config{ RDirs: rDirs, diff --git a/internal/logging/logging.go b/internal/logging/logging.go index 23ab6f0..c8d0094 100644 --- a/internal/logging/logging.go +++ b/internal/logging/logging.go @@ -16,13 +16,15 @@ type Logger struct { infoLogger *log.Logger errorLogger *log.Logger eventLogger *log.Logger + debug bool } -func NewLogger() *Logger { +func NewLogger(debug bool) *Logger { return &Logger{ infoLogger: log.New(os.Stdout, "", 0), errorLogger: log.New(os.Stderr, "", 0), eventLogger: log.New(os.Stdout, "", log.Ldate|log.Ltime), + debug: debug, } } @@ -32,8 +34,10 @@ func (l *Logger) Infof(format string, v ...interface{}) { } // Errorf writes an error message to stderr -func (l *Logger) Errorf(format string, v ...interface{}) { - l.errorLogger.Printf(format, v...) +func (l *Logger) Errorf(debug bool, format string, v ...interface{}) { + if l.debug == debug { + l.errorLogger.Printf(format, v...) + } } // Eventf writes an event with timestamp to stdout diff --git a/internal/logging/logging_test.go b/internal/logging/logging_test.go index f70230c..690a2c6 100644 --- a/internal/logging/logging_test.go +++ b/internal/logging/logging_test.go @@ -13,7 +13,7 @@ const ansiPattern = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\ var ansiMatcher = regexp.MustCompile(ansiPattern) -var l = NewLogger() +var l = NewLogger(true) var logTests = []struct { logger *log.Logger @@ -23,8 +23,8 @@ var logTests = []struct { }{ {l.infoLogger, func() { l.Infof("Info message no. %d", 1) }, "Info message no. 1\n", nil}, {l.infoLogger, func() { l.Infof("Info message no. %d with a string %s\n", 2, "appended to it") }, "Info message no. 2 with a string appended to it\n", nil}, - {l.errorLogger, func() { l.Errorf("Error message") }, "Error message\n", nil}, - {l.errorLogger, func() { l.Errorf("Error message\n") }, "Error message\n", nil}, + {l.errorLogger, func() { l.Errorf(true, "Error message") }, "Error message\n", nil}, + {l.errorLogger, func() { l.Errorf(true, "Error message\n") }, "Error message\n", nil}, {l.eventLogger, func() { l.Eventf(ColorNone, "Event message") }, dateFormatPattern + " Event message\n", nil}, {l.eventLogger, func() { l.Eventf(ColorRed, "Event message") }, dateFormatPattern + " Event message\n", [][]byte{[]byte("\x1b[31;1m"), []byte("\x1b[0m")}}, {l.eventLogger, func() { l.Eventf(ColorGreen, "Event message") }, dateFormatPattern + " Event message\n", [][]byte{[]byte("\x1b[32;1m"), []byte("\x1b[0m")}}, diff --git a/internal/pspy/pspy.go b/internal/pspy/pspy.go index e90dd38..cd854d9 100644 --- a/internal/pspy/pspy.go +++ b/internal/pspy/pspy.go @@ -16,7 +16,7 @@ type Bindings struct { type Logger interface { Infof(format string, v ...interface{}) - Errorf(format string, v ...interface{}) + Errorf(debug bool, format string, v ...interface{}) Eventf(color int, format string, v ...interface{}) } @@ -96,7 +96,7 @@ func initFSW(fsw FSWatcher, rdirs, dirs []string, logger Logger) { case <-doneCh: return case err := <-errCh: - logger.Errorf("initializing fs watcher: %v", err) + logger.Errorf(true, "initializing fs watcher: %v", err) } } } @@ -130,7 +130,7 @@ func triggerEvery(d time.Duration, triggerCh chan struct{}) { func logErrors(errCh chan error, logger Logger) { for { err := <-errCh - logger.Errorf("ERROR: %v", err) + logger.Errorf(true, "ERROR: %v", err) } } diff --git a/internal/pspy/pspy_test.go b/internal/pspy/pspy_test.go index a8a7b89..5d3c9a1 100644 --- a/internal/pspy/pspy_test.go +++ b/internal/pspy/pspy_test.go @@ -190,6 +190,7 @@ type mockLogger struct { Info chan string Error chan string Event chan string + Debug bool } func newMockLogger() *mockLogger { @@ -197,6 +198,7 @@ func newMockLogger() *mockLogger { Info: make(chan string, 10), Error: make(chan string, 10), Event: make(chan string, 10), + Debug: true, } } @@ -204,8 +206,10 @@ func (l *mockLogger) Infof(format string, v ...interface{}) { l.Info <- fmt.Sprintf(format, v...) } -func (l *mockLogger) Errorf(format string, v ...interface{}) { - l.Error <- fmt.Sprintf(format, v...) +func (l *mockLogger) Errorf(debug bool, format string, v ...interface{}) { + if l.Debug == debug { + l.Error <- fmt.Sprintf(format, v...) + } } func (l *mockLogger) Eventf(color int, format string, v ...interface{}) {