kappanhang/log.go
Birk da Yooper 7656d4335b Added flag to set alternate controller address
Added flag to show CI-V packet contents to support debugging

At start-up show virtual soundcard info by both name & filesystem location
Updated to always present status lines at bottom of the terminal
Removed milliseconds from time shown on status lines
Reformatted status line showing packet activity to be easier to follow
Adjusted default status update frequency to 150ms to be easier to observe

Restored commented out code for working with single VFO

Added multiple functions to handle BCD <-> human readable values to improve easy of maintenance
Added function to prepare CI-V packets, abstracting this step so that each function no longer
  needs to create entire packet. This makes the code much easier to follow and maintaint.
  This will also greatly ease effort needed for extending radio features supported

Added/adjusted various comments

Updated some variable names to be less terse to facilitate maintenance
  eg "incTS" to "incTuningStep"

Some adjustments to improve readability/ease maintenance in favor of unnecessary optimizations
  EG - using formatted prints instead of prints using concatenations
       using strings.Repeat instead of loop to build space buffers
       abstracted control char strings to variables

Hooks for future functionality

Removed some commented out code blocks
   "Trust the repo, Luke!"
2024-02-24 20:53:49 -05:00

104 lines
2.4 KiB
Go

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)
}
return filename[l.filenameTrimChars : len(filename)-len(extension)]
}
func (l *logger) Print(a ...interface{}) {
if statusLog.isRealtime() {
statusLog.mutex.Lock()
statusLog.clearStatusLine()
defer func() {
statusLog.mutex.Unlock()
statusLog.print()
}()
}
l.logger.Info(append([]interface{}{l.GetCallerFileName(false) + ": "}, a...)...)
}
func (l *logger) PrintStatusLog(a ...interface{}) {
l.logger.Info(append([]interface{}{l.GetCallerFileName(false) + ": "}, a...)...)
}
func (l *logger) Debug(a ...interface{}) {
if statusLog.isRealtime() {
statusLog.mutex.Lock()
statusLog.clearStatusLine()
defer func() {
statusLog.mutex.Unlock()
statusLog.print()
}()
}
l.logger.Debug(append([]interface{}{l.GetCallerFileName(true) + ": "}, a...)...)
}
func (l *logger) Error(a ...interface{}) {
if statusLog.isRealtime() {
statusLog.mutex.Lock()
statusLog.clearStatusLine()
defer func() {
statusLog.mutex.Unlock()
statusLog.print()
}()
}
l.logger.Error(append([]interface{}{l.GetCallerFileName(true) + ": "}, a...)...)
}
func (l *logger) ErrorC(a ...interface{}) {
if statusLog.isRealtime() {
statusLog.mutex.Lock()
statusLog.clearStatusLine()
defer func() {
statusLog.mutex.Unlock()
statusLog.print()
}()
}
l.logger.Error(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 if quietLog {
level = zap.FatalLevel
} 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
}