shithub: mycel

Download patch

ref: d5977e3393e6486566ac307f40d8c3492ed8338f
parent: e5a8744557758bf4592551225a60ceaf592a9c8c
author: Philip Silva <philip.silva@protonmail.com>
date: Sat Apr 30 14:47:36 EDT 2022

Simplify logging

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -1514,7 +1514,7 @@
 func NewBrowser(_dui *duit.DUI, initUrl string) (b *Browser) {
 	jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
 	if err != nil {
-		log.Fatal(err)
+		log.Fatalf("%v", err)
 	}
 	tr := http.DefaultTransport.(*http.Transport).Clone()
 	tr.MaxIdleConns = 10
--- a/browser/duitx/scroll.go
+++ b/browser/duitx/scroll.go
@@ -292,7 +292,6 @@
 		ui.Kid.Draw = duit.Clean
 	} else if ui.Kid.Draw != duit.Clean || force {
 		log.Printf("drawChild: refresh: ui.Kid.Draw=%v  force=%v", ui.Kid.Draw, force)
-		log.Flush()
 		ui.freeCur()
 		tmp := img.Clipr
 		img.ReplClipr(false, ui.childR.Add(orig))
--- a/browser/website.go
+++ b/browser/website.go
@@ -159,8 +159,6 @@
 
 	fs.Update(htm, csss, scripts)
 	fs.SetDOM(nt)
-
-	log.Flush()
 }
 
 func cssSrcs(f opossum.Fetcher, doc *html.Node) (srcs []string) {
--- a/cmd/opossum/main.go
+++ b/cmd/opossum/main.go
@@ -290,7 +290,7 @@
 	if cpuprofile != "" {
 		f, err := os.Create(cpuprofile)
 		if err != nil {
-			log.Fatal(err)
+			log.Fatalf("%v", err)
 		}
 		pprof.StartCPUProfile(f)
 		go func() {
@@ -304,7 +304,7 @@
 	if memprofile != "" {
 		f, err := os.Create(memprofile)
 		if err != nil {
-			log.Fatal(err)
+			log.Fatalf("%v", err)
 		}
 		go func() {
 			<-time.After(time.Minute)
--- a/logger/logger.go
+++ b/logger/logger.go
@@ -1,113 +1,41 @@
 package log
 
 import (
-	"fmt"
-	"io"
-	goLog "log"
-	"os"
+	"log"
 	"sync"
 )
 
-// Sink for Go's log pkg
 var (
-	Sink  io.Writer
+	mu    sync.Mutex
 	quiet bool
-	gl    *goLog.Logger
-
 	Debug bool
-
-	mu       sync.Mutex
-	last     string
-	lastSev  int
-	repeated int
 )
 
-func init() {
-	gl = goLog.New(os.Stderr, "", goLog.LstdFlags)
-}
-
 func SetQuiet() {
 	mu.Lock()
 	defer mu.Unlock()
 
 	quiet = true
-	Sink = &NullWriter{}
-	goLog.SetOutput(Sink)
 }
 
-type NullWriter struct{}
-
-func (w *NullWriter) Write(p []byte) (n int, err error) {
-	n = len(p)
-	return
-}
-
 func Printf(format string, v ...interface{}) {
-	emit(debug, format, v...)
+	if Debug && !quiet {
+		log.Printf(format, v...)
+	}
 }
 
 func Infof(format string, v ...interface{}) {
-	emit(info, format, v...)
+	if !quiet {
+		log.Printf(format, v...)
+	}
 }
 
 func Errorf(format string, v ...interface{}) {
-	emit(er, format, v...)
+	if !quiet {
+		log.Printf(format, v...)
+	}
 }
 
-func Fatal(v ...interface{}) {
-	gl.Fatal(v...)
-}
-
 func Fatalf(format string, v ...interface{}) {
-	gl.Fatalf(format, v...)
-}
-
-const (
-	debug = iota
-	print
-	info
-	er
-	fatal
-
-	flush
-)
-
-func Flush() {
-	emit(flush, "")
-}
-
-func emit(severity int, format string, v ...interface{}) {
-	mu.Lock()
-	defer mu.Unlock()
-
-	if severity == debug && !Debug {
-		return
-	}
-	if (severity != fatal && severity != flush) && quiet {
-		return
-	}
-
-	msg := fmt.Sprintf(format, v...)
-	switch {
-	case last == msg && lastSev == severity:
-		repeated++
-	case repeated > 0:
-		goLog.Printf("...and %v more", repeated)
-		repeated = 0
-		fallthrough
-	default:
-		switch severity {
-		case debug:
-			gl.Printf(format, v...)
-		case info:
-			gl.Printf(format, v...)
-		case er:
-			gl.Printf(format, v...)
-		case fatal:
-			gl.Fatalf(format, v...)
-		case flush:
-		}
-	}
-	last = msg
-	lastSev = severity
+	log.Fatalf(format, v...)
 }