Skip to content

Commit

Permalink
Log debug entries to file
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcPer committed Apr 28, 2022
1 parent 413a37a commit 0cafe2c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
lanchat
.vscode
bin/
*.log
14 changes: 8 additions & 6 deletions lan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down
37 changes: 20 additions & 17 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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...)
}
}
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"context"
"fmt"
"log"
"os"

"github.com/MarcPer/lanchat/lan"
"github.com/MarcPer/lanchat/logger"
Expand All @@ -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())
Expand All @@ -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
}

0 comments on commit 0cafe2c

Please sign in to comment.