mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 03:34:50 +00:00
introduce --debug flag to hide excessive error messages by default but allow displaying them
This commit is contained in:
@@ -58,6 +58,7 @@ var defaultRDirs = []string{
|
|||||||
var defaultDirs = []string{}
|
var defaultDirs = []string{}
|
||||||
var triggerInterval int
|
var triggerInterval int
|
||||||
var colored bool
|
var colored bool
|
||||||
|
var debug bool
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.PersistentFlags().BoolVarP(&logPS, "procevents", "p", true, "print new processes to stdout")
|
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().StringArrayVarP(&dirs, "dirs", "d", defaultDirs, "watch these dirs")
|
||||||
rootCmd.PersistentFlags().IntVarP(&triggerInterval, "interval", "i", 100, "scan every 'interval' milliseconds for new processes")
|
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(&colored, "color", "c", true, "color the printed events")
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "", false, "print detailed error messages")
|
||||||
|
|
||||||
log.SetOutput(os.Stdout)
|
log.SetOutput(os.Stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func root(cmd *cobra.Command, args []string) {
|
func root(cmd *cobra.Command, args []string) {
|
||||||
logger := logging.NewLogger()
|
logger := logging.NewLogger(debug)
|
||||||
|
|
||||||
cfg := &config.Config{
|
cfg := &config.Config{
|
||||||
RDirs: rDirs,
|
RDirs: rDirs,
|
||||||
|
|||||||
@@ -16,13 +16,15 @@ type Logger struct {
|
|||||||
infoLogger *log.Logger
|
infoLogger *log.Logger
|
||||||
errorLogger *log.Logger
|
errorLogger *log.Logger
|
||||||
eventLogger *log.Logger
|
eventLogger *log.Logger
|
||||||
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogger() *Logger {
|
func NewLogger(debug bool) *Logger {
|
||||||
return &Logger{
|
return &Logger{
|
||||||
infoLogger: log.New(os.Stdout, "", 0),
|
infoLogger: log.New(os.Stdout, "", 0),
|
||||||
errorLogger: log.New(os.Stderr, "", 0),
|
errorLogger: log.New(os.Stderr, "", 0),
|
||||||
eventLogger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
|
eventLogger: log.New(os.Stdout, "", log.Ldate|log.Ltime),
|
||||||
|
debug: debug,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,9 +34,11 @@ func (l *Logger) Infof(format string, v ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Errorf writes an error message to stderr
|
// Errorf writes an error message to stderr
|
||||||
func (l *Logger) Errorf(format string, v ...interface{}) {
|
func (l *Logger) Errorf(debug bool, format string, v ...interface{}) {
|
||||||
|
if l.debug == debug {
|
||||||
l.errorLogger.Printf(format, v...)
|
l.errorLogger.Printf(format, v...)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Eventf writes an event with timestamp to stdout
|
// Eventf writes an event with timestamp to stdout
|
||||||
func (l *Logger) Eventf(color int, format string, v ...interface{}) {
|
func (l *Logger) Eventf(color int, format string, v ...interface{}) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ const ansiPattern = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\
|
|||||||
|
|
||||||
var ansiMatcher = regexp.MustCompile(ansiPattern)
|
var ansiMatcher = regexp.MustCompile(ansiPattern)
|
||||||
|
|
||||||
var l = NewLogger()
|
var l = NewLogger(true)
|
||||||
|
|
||||||
var logTests = []struct {
|
var logTests = []struct {
|
||||||
logger *log.Logger
|
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", 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.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(true, "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\n") }, "Error message\n", nil},
|
||||||
{l.eventLogger, func() { l.Eventf(ColorNone, "Event message") }, dateFormatPattern + " Event 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(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")}},
|
{l.eventLogger, func() { l.Eventf(ColorGreen, "Event message") }, dateFormatPattern + " Event message\n", [][]byte{[]byte("\x1b[32;1m"), []byte("\x1b[0m")}},
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ type Bindings struct {
|
|||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Infof(format string, v ...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{})
|
Eventf(color int, format string, v ...interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ func initFSW(fsw FSWatcher, rdirs, dirs []string, logger Logger) {
|
|||||||
case <-doneCh:
|
case <-doneCh:
|
||||||
return
|
return
|
||||||
case err := <-errCh:
|
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) {
|
func logErrors(errCh chan error, logger Logger) {
|
||||||
for {
|
for {
|
||||||
err := <-errCh
|
err := <-errCh
|
||||||
logger.Errorf("ERROR: %v", err)
|
logger.Errorf(true, "ERROR: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -190,6 +190,7 @@ type mockLogger struct {
|
|||||||
Info chan string
|
Info chan string
|
||||||
Error chan string
|
Error chan string
|
||||||
Event chan string
|
Event chan string
|
||||||
|
Debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMockLogger() *mockLogger {
|
func newMockLogger() *mockLogger {
|
||||||
@@ -197,6 +198,7 @@ func newMockLogger() *mockLogger {
|
|||||||
Info: make(chan string, 10),
|
Info: make(chan string, 10),
|
||||||
Error: make(chan string, 10),
|
Error: make(chan string, 10),
|
||||||
Event: make(chan string, 10),
|
Event: make(chan string, 10),
|
||||||
|
Debug: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,9 +206,11 @@ func (l *mockLogger) Infof(format string, v ...interface{}) {
|
|||||||
l.Info <- fmt.Sprintf(format, v...)
|
l.Info <- fmt.Sprintf(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *mockLogger) Errorf(format string, v ...interface{}) {
|
func (l *mockLogger) Errorf(debug bool, format string, v ...interface{}) {
|
||||||
|
if l.Debug == debug {
|
||||||
l.Error <- fmt.Sprintf(format, v...)
|
l.Error <- fmt.Sprintf(format, v...)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (l *mockLogger) Eventf(color int, format string, v ...interface{}) {
|
func (l *mockLogger) Eventf(color int, format string, v ...interface{}) {
|
||||||
m := fmt.Sprintf(format, v...)
|
m := fmt.Sprintf(format, v...)
|
||||||
|
|||||||
Reference in New Issue
Block a user