mirror of
https://github.com/nonoo/kappanhang.git
synced 2026-01-11 11:19:58 +01:00
Move log into the main package
This commit is contained in:
parent
1372598279
commit
d2efa14e9c
3
args.go
3
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
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import (
|
|||
"sync"
|
||||
|
||||
"github.com/akosmarton/papipes"
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
type audioStruct struct {
|
||||
|
|
|
|||
|
|
@ -5,8 +5,6 @@ import (
|
|||
"encoding/binary"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
const audioTimeoutDuration = 3 * time.Second
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ import (
|
|||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
type controlStream struct {
|
||||
|
|
|
|||
86
log.go
Normal file
86
log.go
Normal file
|
|
@ -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
|
||||
}
|
||||
77
log/log.go
77
log/log.go
|
|
@ -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
|
||||
}
|
||||
2
main.go
2
main.go
|
|
@ -6,8 +6,6 @@ import (
|
|||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
var gotErrChan = make(chan bool)
|
||||
|
|
|
|||
2
pkt0.go
2
pkt0.go
|
|
@ -6,8 +6,6 @@ import (
|
|||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
type pkt0Type struct {
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ import (
|
|||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
type seqNum int
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/google/goterm/term"
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
type serialPortStruct struct {
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"time"
|
||||
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
const maxSerialFrameLength = 80 // Max. frame length according to Hamlib.
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
type serialTCPSrv struct {
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@ import (
|
|||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/nonoo/kappanhang/log"
|
||||
)
|
||||
|
||||
const expectTimeoutDuration = time.Second
|
||||
|
|
|
|||
Loading…
Reference in a new issue