From 0cafe2c06d4e6a07c3ee7bcc4a641ff2c6183ddd Mon Sep 17 00:00:00 2001 From: Marcelo Pereira Date: Thu, 28 Apr 2022 11:00:12 +0200 Subject: [PATCH] Log debug entries to file --- .gitignore | 1 + lan/scan.go | 14 ++++++++------ logger/logger.go | 37 ++++++++++++++++++++----------------- main.go | 20 ++++++++++++++++++++ 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 28ab550..52bd659 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ lanchat .vscode bin/ +*.log diff --git a/lan/scan.go b/lan/scan.go index 9354e67..a393ebd 100644 --- a/lan/scan.go +++ b/lan/scan.go @@ -112,12 +112,12 @@ func (s *DefaultScanner) scanHost(ips []string, chatPort int) (string, bool) { hostInCh := make(chan string) logger.Debugf("Scanning %d hosts\n", len(ips)) - go func(ch chan string) { + go func() { for _, host := range ips { hostInCh <- host } - close(ch) - }(hostInCh) + close(hostInCh) + }() ctx, cancel := context.WithCancel(context.Background()) resCh := make(chan string) @@ -145,7 +145,8 @@ func (s *DefaultScanner) scanHost(ips []string, chatPort int) (string, bool) { var wg sync.WaitGroup -const numWorkers = 15 +const numWorkers = 10 +const timeout = 100 * time.Millisecond func popScan(ctx context.Context, chatPort int, in chan string, out chan string) { for { @@ -159,11 +160,12 @@ func popScan(ctx context.Context, chatPort int, in chan string, out chan string) return } url := fmt.Sprintf("%s:%d", host, chatPort) - logger.Debugf("scanning %s\n", url) - conn, err := net.DialTimeout("tcp", url, 100*time.Millisecond) + logger.Debugf("scanning %s", url) + conn, err := net.DialTimeout("tcp", url, timeout) if err == nil { conn.Close() out <- url + wg.Done() return } } diff --git a/logger/logger.go b/logger/logger.go index c0ac07a..ced2175 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -14,13 +14,13 @@ var ( ) const ( - logLevelErr = iota - logLevelWarn - logLevelInfo - logLevelDebug + LogLevelErr = iota + LogLevelWarn + LogLevelInfo + LogLevelDebug ) -const LogLevel = logLevelInfo +const LogLevel = LogLevelInfo func init() { out := os.Stdout @@ -31,56 +31,59 @@ func init() { } func Init(w io.Writer) { - debugLogger = log.New(w, "[D]", log.Ldate|log.Ltime|log.Lshortfile) - infoLogger = log.New(w, "[I]", log.Ldate|log.Ltime) - warnLogger = log.New(w, "[W]", log.Ldate|log.Ltime) - errLogger = log.New(w, "[E]", log.Ldate|log.Ltime) + infoLogger.SetOutput(w) + warnLogger.SetOutput(w) + errLogger.SetOutput(w) +} + +func InitDebug(w io.Writer) { + debugLogger.SetOutput(w) } func Debug(msg string) { - if LogLevel >= logLevelDebug { + if LogLevel >= LogLevelDebug { debugLogger.Print(msg) } } func Debugf(format string, v ...interface{}) { - if LogLevel >= logLevelDebug { + if LogLevel >= LogLevelDebug { debugLogger.Printf(format, v...) } } func Info(msg string) { - if LogLevel >= logLevelInfo { + if LogLevel >= LogLevelInfo { infoLogger.Print(msg) } } func Infof(format string, v ...interface{}) { - if LogLevel >= logLevelInfo { + if LogLevel >= LogLevelInfo { infoLogger.Printf(format, v...) } } func Warn(msg string) { - if LogLevel >= logLevelWarn { + if LogLevel >= LogLevelWarn { warnLogger.Print(msg) } } func Warnf(format string, v ...interface{}) { - if LogLevel >= logLevelWarn { + if LogLevel >= LogLevelWarn { warnLogger.Printf(format, v...) } } func Error(msg string) { - if LogLevel >= logLevelErr { + if LogLevel >= LogLevelErr { errLogger.Print(msg) } } func Errorf(format string, v ...interface{}) { - if LogLevel >= logLevelErr { + if LogLevel >= LogLevelErr { errLogger.Printf(format, v...) } } diff --git a/main.go b/main.go index b2e2904..5668906 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "context" "fmt" + "log" + "os" "github.com/MarcPer/lanchat/lan" "github.com/MarcPer/lanchat/logger" @@ -25,6 +27,9 @@ func main() { logger.Infof("Starting UI\n") renderer := ui.New(cfg.username, toUI, fromUI) logger.Init(&renderer) + f := debugFile() + defer f.Close() + logger.InitDebug(f) client := &lan.Client{Name: cfg.username, HostPort: cfg.port, FromUI: fromUI, ToUI: toUI, Scanner: scanner} ctx, cancel := context.WithCancel(context.Background()) @@ -34,3 +39,18 @@ func main() { cancel() fmt.Println("bye") } + +func debugFile() *os.File { + var debugPath string + if logger.LogLevel >= logger.LogLevelDebug { + debugPath = "debug.log" + } else { + debugPath = os.DevNull + } + + f, err := os.OpenFile(debugPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + if err != nil { + log.Fatal(err) + } + return f +}