diff --git a/args.go b/args.go index a842f65..4535901 100644 --- a/args.go +++ b/args.go @@ -7,12 +7,14 @@ import ( "github.com/pborman/getopt" ) +var verboseLog bool var connectAddress string var serialTCPPort uint16 var enableSerialDevice bool func parseArgs() { h := getopt.BoolLong("help", 'h', "display help") + v := getopt.BoolLong("verbose", 'v', "Enable verbose (debug) logging") a := getopt.StringLong("address", 'a', "IC-705", "Connect to address") t := getopt.Uint16Long("serial-tcp-port", 'p', 4533, "Expose radio's serial port on this TCP port") s := getopt.BoolLong("enable-serial-device", 's', "Expose radio's serial port as a virtual serial port") @@ -25,6 +27,7 @@ func parseArgs() { os.Exit(1) } + verboseLog = *v connectAddress = *a serialTCPPort = *t enableSerialDevice = *s diff --git a/audio-linux.go b/audio-linux.go index ca46de1..f6256ee 100644 --- a/audio-linux.go +++ b/audio-linux.go @@ -9,7 +9,6 @@ import ( "sync" "github.com/akosmarton/papipes" - "github.com/nonoo/kappanhang/log" ) type audioStruct struct { diff --git a/audiostream.go b/audiostream.go index cb5943c..71db60e 100644 --- a/audiostream.go +++ b/audiostream.go @@ -5,8 +5,6 @@ import ( "encoding/binary" "errors" "time" - - "github.com/nonoo/kappanhang/log" ) const audioTimeoutDuration = 3 * time.Second diff --git a/controlstream.go b/controlstream.go index aef6087..23c715c 100644 --- a/controlstream.go +++ b/controlstream.go @@ -6,8 +6,6 @@ import ( "errors" "strings" "time" - - "github.com/nonoo/kappanhang/log" ) type controlStream struct { diff --git a/log.go b/log.go new file mode 100644 index 0000000..8dd19b9 --- /dev/null +++ b/log.go @@ -0,0 +1,86 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "runtime" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +type logger struct { + logger *zap.SugaredLogger + filenameTrimChars int +} + +var log logger + +func (l *logger) GetCallerFileName(withLine bool) string { + _, filename, line, _ := runtime.Caller(2) + extension := filepath.Ext(filename) + if withLine { + return fmt.Sprint(filename[l.filenameTrimChars:len(filename)-len(extension)], "@", line) + } else { + return filename[l.filenameTrimChars : len(filename)-len(extension)] + } +} + +func (l *logger) Printf(a string, b ...interface{}) { + l.logger.Infof(l.GetCallerFileName(false)+": "+a, b...) +} + +func (l *logger) Print(a ...interface{}) { + l.logger.Info(append([]interface{}{l.GetCallerFileName(false) + ": "}, a...)...) +} + +func (l *logger) Debugf(a string, b ...interface{}) { + l.logger.Debugf(l.GetCallerFileName(true)+": "+a, b...) +} + +func (l *logger) Debug(a ...interface{}) { + l.logger.Debug(append([]interface{}{l.GetCallerFileName(true) + ": "}, a...)...) +} + +func (l *logger) Errorf(a string, b ...interface{}) { + l.logger.Errorf(l.GetCallerFileName(true)+": "+a, b...) +} + +func (l *logger) Error(a ...interface{}) { + l.logger.Error(append([]interface{}{l.GetCallerFileName(true) + ": "}, a...)...) +} + +func (l *logger) ErrorC(a ...interface{}) { + l.logger.Error(a...) +} + +func (l *logger) Fatalf(a string, b ...interface{}) { + l.logger.Fatalf(l.GetCallerFileName(true)+": "+a, b...) +} + +func (l *logger) Fatal(a ...interface{}) { + l.logger.Fatal(append([]interface{}{l.GetCallerFileName(true) + ": "}, a...)...) +} + +func (l *logger) Init() { + // Example: https://stackoverflow.com/questions/50933936/zap-logger-does-not-print-on-console-rather-print-in-the-log-file/50936341 + pe := zap.NewProductionEncoderConfig() + pe.EncodeTime = zapcore.ISO8601TimeEncoder + // pe.LevelKey = "" + consoleEncoder := zapcore.NewConsoleEncoder(pe) + + var level zapcore.Level + if verboseLog { + level = zap.DebugLevel + } else { + level = zap.InfoLevel + } + + core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), level) + l.logger = zap.New(core).Sugar() + + var callerFilename string + _, callerFilename, _, _ = runtime.Caller(1) + l.filenameTrimChars = len(filepath.Dir(callerFilename)) + 1 +} diff --git a/log/log.go b/log/log.go deleted file mode 100644 index bc9a8f7..0000000 --- a/log/log.go +++ /dev/null @@ -1,77 +0,0 @@ -package log - -import ( - "fmt" - "os" - "path/filepath" - "runtime" - - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -var logger *zap.SugaredLogger -var filenameTrimChars int - -func GetCallerFileName(withLine bool) string { - _, filename, line, _ := runtime.Caller(2) - extension := filepath.Ext(filename) - if withLine { - return fmt.Sprint(filename[filenameTrimChars:len(filename)-len(extension)], "@", line) - } else { - return filename[filenameTrimChars : len(filename)-len(extension)] - } -} - -func Printf(a string, b ...interface{}) { - logger.Infof(GetCallerFileName(false)+": "+a, b...) -} - -func Print(a ...interface{}) { - logger.Info(append([]interface{}{GetCallerFileName(false) + ": "}, a...)...) -} - -func Debugf(a string, b ...interface{}) { - logger.Debugf(GetCallerFileName(true)+": "+a, b...) -} - -func Debug(a ...interface{}) { - logger.Debug(append([]interface{}{GetCallerFileName(true) + ": "}, a...)...) -} - -func Errorf(a string, b ...interface{}) { - logger.Errorf(GetCallerFileName(true)+": "+a, b...) -} - -func Error(a ...interface{}) { - logger.Error(append([]interface{}{GetCallerFileName(true) + ": "}, a...)...) -} - -func ErrorC(a ...interface{}) { - logger.Error(a...) -} - -func Fatalf(a string, b ...interface{}) { - logger.Fatalf(GetCallerFileName(true)+": "+a, b...) -} - -func Fatal(a ...interface{}) { - logger.Fatal(append([]interface{}{GetCallerFileName(true) + ": "}, a...)...) -} - -func Init() { - // Example: https://stackoverflow.com/questions/50933936/zap-logger-does-not-print-on-console-rather-print-in-the-log-file/50936341 - pe := zap.NewProductionEncoderConfig() - pe.EncodeTime = zapcore.ISO8601TimeEncoder - // pe.LevelKey = "" - consoleEncoder := zapcore.NewConsoleEncoder(pe) - - level := zap.DebugLevel - - core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), level) - logger = zap.New(core).Sugar() - - var callerFilename string - _, callerFilename, _, _ = runtime.Caller(1) - filenameTrimChars = len(filepath.Dir(callerFilename)) + 1 -} diff --git a/main.go b/main.go index f5a6d24..f8cad5f 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,6 @@ import ( "strings" "syscall" "time" - - "github.com/nonoo/kappanhang/log" ) var gotErrChan = make(chan bool) diff --git a/pkt0.go b/pkt0.go index 5e18bbc..2441d45 100644 --- a/pkt0.go +++ b/pkt0.go @@ -6,8 +6,6 @@ import ( "math" "sync" "time" - - "github.com/nonoo/kappanhang/log" ) type pkt0Type struct { diff --git a/seqbuf.go b/seqbuf.go index 37b485c..fe98e87 100644 --- a/seqbuf.go +++ b/seqbuf.go @@ -4,8 +4,6 @@ import ( "errors" "sync" "time" - - "github.com/nonoo/kappanhang/log" ) type seqNum int diff --git a/serial-linux.go b/serial-linux.go index bbf5fe3..63d16d9 100644 --- a/serial-linux.go +++ b/serial-linux.go @@ -6,7 +6,6 @@ import ( "os" "github.com/google/goterm/term" - "github.com/nonoo/kappanhang/log" ) type serialPortStruct struct { diff --git a/serialstream.go b/serialstream.go index 10734e3..f352dce 100644 --- a/serialstream.go +++ b/serialstream.go @@ -4,8 +4,6 @@ import ( "bytes" "encoding/binary" "time" - - "github.com/nonoo/kappanhang/log" ) const maxSerialFrameLength = 80 // Max. frame length according to Hamlib. diff --git a/serialtcpsrv.go b/serialtcpsrv.go index cde5d66..2d2930d 100644 --- a/serialtcpsrv.go +++ b/serialtcpsrv.go @@ -4,8 +4,6 @@ import ( "fmt" "io" "net" - - "github.com/nonoo/kappanhang/log" ) type serialTCPSrv struct { diff --git a/streamcommon.go b/streamcommon.go index 6dd60cb..9be2c40 100644 --- a/streamcommon.go +++ b/streamcommon.go @@ -7,8 +7,6 @@ import ( "fmt" "net" "time" - - "github.com/nonoo/kappanhang/log" ) const expectTimeoutDuration = time.Second