aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2024-02-13 22:44:25 +0100
committerRobin Jarry <robin@jarry.cc>2024-02-14 23:04:38 +0100
commit8edf7b0e4d386e5b4b4f052c88a781e40b1f1d30 (patch)
tree21db1c37692f8210f0b440c4c8e73604bf58d2bf /lib
parent3aa8b6308482d2e3feea0fecb065190cf05c2468 (diff)
downloadaerc-8edf7b0e4d386e5b4b4f052c88a781e40b1f1d30.tar.gz
aerc-8edf7b0e4d386e5b4b4f052c88a781e40b1f1d30.zip
log: move package to lib
This has nothing to do at the root of the source tree. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Bence Ferdinandy <bence@ferdinandy.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/attachment.go2
-rw-r--r--lib/crypto/crypto.go2
-rw-r--r--lib/crypto/gpg/gpgbin/gpgbin.go2
-rw-r--r--lib/crypto/pgp/pgp.go2
-rw-r--r--lib/hooks/exec.go2
-rw-r--r--lib/ipc/receive.go2
-rw-r--r--lib/log/logger.go152
-rw-r--r--lib/log/panic-logger.go60
-rw-r--r--lib/messageview.go2
-rw-r--r--lib/open.go2
-rw-r--r--lib/pama/apply.go2
-rw-r--r--lib/pama/drop.go2
-rw-r--r--lib/pama/list.go2
-rw-r--r--lib/pama/models/view.go2
-rw-r--r--lib/pama/revctrl/git.go2
-rw-r--r--lib/pama/revctrl/revctrl.go2
-rw-r--r--lib/pama/store/store.go2
-rw-r--r--lib/pama/unlink.go2
-rw-r--r--lib/pama/worktree.go2
-rw-r--r--lib/parse/ansi.go2
-rw-r--r--lib/parse/daterange.go2
-rw-r--r--lib/parse/header.go2
-rw-r--r--lib/parse/match.go2
-rw-r--r--lib/rfc822/message.go2
-rw-r--r--lib/state/templates.go2
-rw-r--r--lib/threadbuilder.go2
-rw-r--r--lib/ui/textinput.go2
-rw-r--r--lib/ui/ui.go2
-rw-r--r--lib/watchers/fsevents.go2
-rw-r--r--lib/watchers/inotify.go2
-rw-r--r--lib/xdg/home.go2
31 files changed, 241 insertions, 29 deletions
diff --git a/lib/attachment.go b/lib/attachment.go
index a8a8103d..aa3cd108 100644
--- a/lib/attachment.go
+++ b/lib/attachment.go
@@ -10,7 +10,7 @@ import (
"path/filepath"
"strings"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"github.com/emersion/go-message/mail"
"github.com/pkg/errors"
)
diff --git a/lib/crypto/crypto.go b/lib/crypto/crypto.go
index e58e6075..af3d1e78 100644
--- a/lib/crypto/crypto.go
+++ b/lib/crypto/crypto.go
@@ -7,7 +7,7 @@ import (
"git.sr.ht/~rjarry/aerc/config"
"git.sr.ht/~rjarry/aerc/lib/crypto/gpg"
"git.sr.ht/~rjarry/aerc/lib/crypto/pgp"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/models"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/emersion/go-message/mail"
diff --git a/lib/crypto/gpg/gpgbin/gpgbin.go b/lib/crypto/gpg/gpgbin/gpgbin.go
index a83ebb37..69f290fd 100644
--- a/lib/crypto/gpg/gpgbin/gpgbin.go
+++ b/lib/crypto/gpg/gpgbin/gpgbin.go
@@ -10,7 +10,7 @@ import (
"strconv"
"strings"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/models"
)
diff --git a/lib/crypto/pgp/pgp.go b/lib/crypto/pgp/pgp.go
index af406446..6195233d 100644
--- a/lib/crypto/pgp/pgp.go
+++ b/lib/crypto/pgp/pgp.go
@@ -8,8 +8,8 @@ import (
"strings"
"time"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/xdg"
- "git.sr.ht/~rjarry/aerc/log"
"git.sr.ht/~rjarry/aerc/models"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/go-crypto/openpgp/armor"
diff --git a/lib/hooks/exec.go b/lib/hooks/exec.go
index bea35f32..ecc33b28 100644
--- a/lib/hooks/exec.go
+++ b/lib/hooks/exec.go
@@ -4,7 +4,7 @@ import (
"os"
"os/exec"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
)
func RunHook(h HookType) error {
diff --git a/lib/ipc/receive.go b/lib/ipc/receive.go
index dbacc660..e2fffaf0 100644
--- a/lib/ipc/receive.go
+++ b/lib/ipc/receive.go
@@ -14,8 +14,8 @@ import (
"git.sr.ht/~rjarry/go-opt"
"git.sr.ht/~rjarry/aerc/config"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/xdg"
- "git.sr.ht/~rjarry/aerc/log"
)
type AercServer struct {
diff --git a/lib/log/logger.go b/lib/log/logger.go
new file mode 100644
index 00000000..2ece5f32
--- /dev/null
+++ b/lib/log/logger.go
@@ -0,0 +1,152 @@
+package log
+
+import (
+ "fmt"
+ "io"
+ "log"
+ "os"
+ "strings"
+)
+
+type LogLevel int
+
+const (
+ TRACE LogLevel = 5
+ DEBUG LogLevel = 10
+ INFO LogLevel = 20
+ WARN LogLevel = 30
+ ERROR LogLevel = 40
+)
+
+var (
+ trace *log.Logger
+ dbg *log.Logger
+ info *log.Logger
+ warn *log.Logger
+ err *log.Logger
+ minLevel LogLevel = TRACE
+)
+
+func Init(file *os.File, level LogLevel) {
+ minLevel = level
+ flags := log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile
+ if file != nil {
+ trace = log.New(file, "TRACE ", flags)
+ dbg = log.New(file, "DEBUG ", flags)
+ info = log.New(file, "INFO ", flags)
+ warn = log.New(file, "WARN ", flags)
+ err = log.New(file, "ERROR ", flags)
+ }
+}
+
+func ParseLevel(value string) (LogLevel, error) {
+ switch strings.ToLower(value) {
+ case "trace":
+ return TRACE, nil
+ case "debug":
+ return DEBUG, nil
+ case "info":
+ return INFO, nil
+ case "warn", "warning":
+ return WARN, nil
+ case "err", "error":
+ return ERROR, nil
+ }
+ return 0, fmt.Errorf("%s: invalid log level", value)
+}
+
+func ErrorLogger() *log.Logger {
+ if err == nil {
+ return log.New(io.Discard, "", log.LstdFlags)
+ }
+ return err
+}
+
+type Logger interface {
+ Tracef(string, ...interface{})
+ Debugf(string, ...interface{})
+ Infof(string, ...interface{})
+ Warnf(string, ...interface{})
+ Errorf(string, ...interface{})
+}
+
+type logger struct {
+ name string
+ calldepth int
+}
+
+func NewLogger(name string, calldepth int) Logger {
+ return &logger{name: name, calldepth: calldepth}
+}
+
+func (l *logger) format(message string, args ...interface{}) string {
+ if len(args) > 0 {
+ message = fmt.Sprintf(message, args...)
+ }
+ if l.name != "" {
+ message = fmt.Sprintf("[%s] %s", l.name, message)
+ }
+ return message
+}
+
+func (l *logger) Tracef(message string, args ...interface{}) {
+ if trace == nil || minLevel > TRACE {
+ return
+ }
+ message = l.format(message, args...)
+ trace.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log
+}
+
+func (l *logger) Debugf(message string, args ...interface{}) {
+ if dbg == nil || minLevel > DEBUG {
+ return
+ }
+ message = l.format(message, args...)
+ dbg.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log
+}
+
+func (l *logger) Infof(message string, args ...interface{}) {
+ if info == nil || minLevel > INFO {
+ return
+ }
+ message = l.format(message, args...)
+ info.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log
+}
+
+func (l *logger) Warnf(message string, args ...interface{}) {
+ if warn == nil || minLevel > WARN {
+ return
+ }
+ message = l.format(message, args...)
+ warn.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log
+}
+
+func (l *logger) Errorf(message string, args ...interface{}) {
+ if err == nil || minLevel > ERROR {
+ return
+ }
+ message = l.format(message, args...)
+ err.Output(l.calldepth, message) //nolint:errcheck // we can't do anything with what we log
+}
+
+var root = logger{calldepth: 3}
+
+func Tracef(message string, args ...interface{}) {
+ root.Tracef(message, args...)
+}
+
+func Debugf(message string, args ...interface{}) {
+ root.Debugf(message, args...)
+}
+
+func Infof(message string, args ...interface{}) {
+ root.Infof(message, args...)
+}
+
+func Warnf(message string, args ...interface{}) {
+ root.Warnf(message, args...)
+}
+
+func Errorf(message string, args ...interface{}) {
+ root.Errorf(message, args...)
+}
diff --git a/lib/log/panic-logger.go b/lib/log/panic-logger.go
new file mode 100644
index 00000000..a1904415
--- /dev/null
+++ b/lib/log/panic-logger.go
@@ -0,0 +1,60 @@
+package log
+
+import (
+ "fmt"
+ "io"
+ "os"
+ "runtime/debug"
+ "strings"
+ "time"
+)
+
+var (
+ UICleanup = func() {}
+ BuildInfo string
+)
+
+// PanicHandler tries to restore the terminal. A stack trace is written to
+// aerc-crash.log and then passed on if a panic occurs.
+func PanicHandler() {
+ r := recover()
+
+ if r == nil {
+ return
+ }
+
+ UICleanup()
+
+ filename := time.Now().Format("/tmp/aerc-crash-20060102-150405.log")
+
+ panicLog, err := os.OpenFile(filename, os.O_SYNC|os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0o600)
+ if err != nil {
+ // we tried, not possible. bye
+ panic(r)
+ }
+ defer panicLog.Close()
+
+ outputs := io.MultiWriter(panicLog, os.Stderr)
+
+ // if any error happens here, we do not care.
+ fmt.Fprintln(panicLog, strings.Repeat("#", 80))
+ fmt.Fprint(panicLog, strings.Repeat(" ", 34))
+ fmt.Fprintln(panicLog, "PANIC CAUGHT!")
+ fmt.Fprint(panicLog, strings.Repeat(" ", 24))
+ fmt.Fprintln(panicLog, time.Now().Format("2006-01-02T15:04:05.000000-0700"))
+ fmt.Fprintln(panicLog, strings.Repeat("#", 80))
+ fmt.Fprintf(outputs, "%s\n", panicMessage)
+ fmt.Fprintf(outputs, "Version: %s\n", BuildInfo)
+ fmt.Fprintf(panicLog, "Error: %v\n\n", r)
+ panicLog.Write(debug.Stack()) //nolint:errcheck // we are already in a panic, so not much we can do here
+ fmt.Fprintf(os.Stderr, "\nThis error was also written to: %s\n", filename)
+ panic(r)
+}
+
+const panicMessage = `
+aerc has encountered a critical error and has terminated. Please help us fix
+this by sending this log and the steps to reproduce the crash to:
+~rjarry/aerc-devel@lists.sr.ht
+
+Thank you
+`
diff --git a/lib/messageview.go b/lib/messageview.go
index 6e1ce858..34549023 100644
--- a/lib/messageview.go
+++ b/lib/messageview.go
@@ -10,8 +10,8 @@ import (
_ "github.com/emersion/go-message/charset"
"git.sr.ht/~rjarry/aerc/lib/crypto"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/rfc822"
- "git.sr.ht/~rjarry/aerc/log"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types"
)
diff --git a/lib/open.go b/lib/open.go
index 21bee748..5ca819e0 100644
--- a/lib/open.go
+++ b/lib/open.go
@@ -10,7 +10,7 @@ import (
"github.com/danwakefield/fnmatch"
"git.sr.ht/~rjarry/aerc/config"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
)
func XDGOpenMime(
diff --git a/lib/pama/apply.go b/lib/pama/apply.go
index 3d701347..f97c7701 100644
--- a/lib/pama/apply.go
+++ b/lib/pama/apply.go
@@ -6,8 +6,8 @@ import (
"math/rand"
"strings"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/pama/models"
- "git.sr.ht/~rjarry/aerc/log"
)
func (m PatchManager) CurrentProject() (p models.Project, err error) {
diff --git a/lib/pama/drop.go b/lib/pama/drop.go
index 595bc87d..fd48575b 100644
--- a/lib/pama/drop.go
+++ b/lib/pama/drop.go
@@ -3,8 +3,8 @@ package pama
import (
"fmt"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/pama/models"
- "git.sr.ht/~rjarry/aerc/log"
)
func (m PatchManager) DropPatch(patch string) error {
diff --git a/lib/pama/list.go b/lib/pama/list.go
index 950f130d..7ab9e3d1 100644
--- a/lib/pama/list.go
+++ b/lib/pama/list.go
@@ -5,8 +5,8 @@ import (
"io"
"strings"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/pama/models"
- "git.sr.ht/~rjarry/aerc/log"
)
func (m PatchManager) Projects(name string) ([]models.Project, error) {
diff --git a/lib/pama/models/view.go b/lib/pama/models/view.go
index 6a5b6bd4..ff2aa710 100644
--- a/lib/pama/models/view.go
+++ b/lib/pama/models/view.go
@@ -6,7 +6,7 @@ import (
"strings"
"text/template"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
)
var templateText = `
diff --git a/lib/pama/revctrl/git.go b/lib/pama/revctrl/git.go
index 2a35754e..ce024078 100644
--- a/lib/pama/revctrl/git.go
+++ b/lib/pama/revctrl/git.go
@@ -8,8 +8,8 @@ import (
"path/filepath"
"strings"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/pama/models"
- "git.sr.ht/~rjarry/aerc/log"
)
func init() {
diff --git a/lib/pama/revctrl/revctrl.go b/lib/pama/revctrl/revctrl.go
index 42532216..cd9202f0 100644
--- a/lib/pama/revctrl/revctrl.go
+++ b/lib/pama/revctrl/revctrl.go
@@ -4,8 +4,8 @@ import (
"errors"
"fmt"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/pama/models"
- "git.sr.ht/~rjarry/aerc/log"
)
var ErrUnsupported = errors.New("unsupported")
diff --git a/lib/pama/store/store.go b/lib/pama/store/store.go
index aaee5412..385d607e 100644
--- a/lib/pama/store/store.go
+++ b/lib/pama/store/store.go
@@ -8,9 +8,9 @@ import (
"path"
"strings"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/pama/models"
"git.sr.ht/~rjarry/aerc/lib/xdg"
- "git.sr.ht/~rjarry/aerc/log"
"github.com/syndtr/goleveldb/leveldb"
)
diff --git a/lib/pama/unlink.go b/lib/pama/unlink.go
index 2def5403..6a35e1a3 100644
--- a/lib/pama/unlink.go
+++ b/lib/pama/unlink.go
@@ -3,7 +3,7 @@ package pama
import (
"fmt"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
)
// Unlink removes provided project
diff --git a/lib/pama/worktree.go b/lib/pama/worktree.go
index 9ecfadf6..1718e430 100644
--- a/lib/pama/worktree.go
+++ b/lib/pama/worktree.go
@@ -7,9 +7,9 @@ import (
"path/filepath"
"strings"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/pama/models"
"git.sr.ht/~rjarry/aerc/lib/xdg"
- "git.sr.ht/~rjarry/aerc/log"
)
func cacheDir() (string, error) {
diff --git a/lib/parse/ansi.go b/lib/parse/ansi.go
index df50042c..12ea4c07 100644
--- a/lib/parse/ansi.go
+++ b/lib/parse/ansi.go
@@ -8,7 +8,7 @@ import (
"os"
"regexp"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
)
var AnsiReg = regexp.MustCompile("\x1B\\[[0-?]*[ -/]*[@-~]")
diff --git a/lib/parse/daterange.go b/lib/parse/daterange.go
index bd984ed5..26368609 100644
--- a/lib/parse/daterange.go
+++ b/lib/parse/daterange.go
@@ -5,7 +5,7 @@ import (
"strings"
"time"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
)
const dateFmt = "2006-01-02"
diff --git a/lib/parse/header.go b/lib/parse/header.go
index 361b3af0..522d178c 100644
--- a/lib/parse/header.go
+++ b/lib/parse/header.go
@@ -3,7 +3,7 @@ package parse
import (
"strings"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"github.com/emersion/go-message/mail"
)
diff --git a/lib/parse/match.go b/lib/parse/match.go
index ac43673e..dc5a06cc 100644
--- a/lib/parse/match.go
+++ b/lib/parse/match.go
@@ -4,7 +4,7 @@ import (
"regexp"
"sync"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
)
var reCache sync.Map
diff --git a/lib/rfc822/message.go b/lib/rfc822/message.go
index 7b0d0552..f511ea19 100644
--- a/lib/rfc822/message.go
+++ b/lib/rfc822/message.go
@@ -10,8 +10,8 @@ import (
"strings"
"time"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/parse"
- "git.sr.ht/~rjarry/aerc/log"
"git.sr.ht/~rjarry/aerc/models"
"github.com/emersion/go-message"
_ "github.com/emersion/go-message/charset"
diff --git a/lib/state/templates.go b/lib/state/templates.go
index ec679211..e1570cd3 100644
--- a/lib/state/templates.go
+++ b/lib/state/templates.go
@@ -10,9 +10,9 @@ import (
"time"
"git.sr.ht/~rjarry/aerc/config"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/ui"
"git.sr.ht/~rjarry/aerc/lib/xdg"
- "git.sr.ht/~rjarry/aerc/log"
"git.sr.ht/~rjarry/aerc/models"
"github.com/danwakefield/fnmatch"
sortthread "github.com/emersion/go-imap-sortthread"
diff --git a/lib/threadbuilder.go b/lib/threadbuilder.go
index 63ae80d3..c18ab3d0 100644
--- a/lib/threadbuilder.go
+++ b/lib/threadbuilder.go
@@ -6,7 +6,7 @@ import (
"time"
"git.sr.ht/~rjarry/aerc/lib/iterator"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types"
"github.com/gatherstars-com/jwz"
diff --git a/lib/ui/textinput.go b/lib/ui/textinput.go
index a01184f2..29f71509 100644
--- a/lib/ui/textinput.go
+++ b/lib/ui/textinput.go
@@ -9,7 +9,7 @@ import (
"github.com/mattn/go-runewidth"
"git.sr.ht/~rjarry/aerc/config"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rockorager/vaxis"
)
diff --git a/lib/ui/ui.go b/lib/ui/ui.go
index ce0a7e93..d63ebf36 100644
--- a/lib/ui/ui.go
+++ b/lib/ui/ui.go
@@ -7,7 +7,7 @@ import (
"syscall"
"git.sr.ht/~rjarry/aerc/config"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rockorager/vaxis"
)
diff --git a/lib/watchers/fsevents.go b/lib/watchers/fsevents.go
index 905db2af..a273dcb0 100644
--- a/lib/watchers/fsevents.go
+++ b/lib/watchers/fsevents.go
@@ -6,7 +6,7 @@ package watchers
import (
"time"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"github.com/fsnotify/fsevents"
)
diff --git a/lib/watchers/inotify.go b/lib/watchers/inotify.go
index 22290307..dd34fc4f 100644
--- a/lib/watchers/inotify.go
+++ b/lib/watchers/inotify.go
@@ -4,7 +4,7 @@
package watchers
import (
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
"github.com/fsnotify/fsnotify"
)
diff --git a/lib/xdg/home.go b/lib/xdg/home.go
index 3471e5e2..39894cf6 100644
--- a/lib/xdg/home.go
+++ b/lib/xdg/home.go
@@ -6,7 +6,7 @@ import (
"path"
"strings"
- "git.sr.ht/~rjarry/aerc/log"
+ "git.sr.ht/~rjarry/aerc/lib/log"
)
// assign to a local var to allow mocking in unit tests