repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
d0b47cc
parent
aa5b8d3
author
Antonio Mika
date
2024-10-03 22:02:17 +0000 UTC
Remove log dep and add data race safe impl
3 files changed,  +78, -14
M go.mod
M go.sum
M go.mod
+0, -2
 1@@ -40,7 +40,6 @@ require (
 2 	github.com/picosh/send v0.0.0-20240820031602-5d3b1a4494cc
 3 	github.com/picosh/tunkit v0.0.0-20240709033345-8315d4f3cd0e
 4 	github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
 5-	github.com/samber/slog-multi v1.2.3
 6 	github.com/sendgrid/sendgrid-go v3.14.0+incompatible
 7 	github.com/simplesurance/go-ip-anonymizer v0.0.0-20200429124537-35a880f8e87d
 8 	github.com/x-way/crawlerdetect v0.2.21
 9@@ -134,7 +133,6 @@ require (
10 	github.com/rogpeppe/go-internal v1.11.0 // indirect
11 	github.com/rs/xid v1.5.0 // indirect
12 	github.com/safchain/ethtool v0.3.0 // indirect
13-	github.com/samber/lo v1.47.0 // indirect
14 	github.com/secure-io/sio-go v0.3.1 // indirect
15 	github.com/sendgrid/rest v2.6.9+incompatible // indirect
16 	github.com/shirou/gopsutil/v3 v3.24.5 // indirect
M go.sum
+0, -4
 1@@ -261,10 +261,6 @@ github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDj
 2 github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
 3 github.com/safchain/ethtool v0.3.0 h1:gimQJpsI6sc1yIqP/y8GYgiXn/NjgvpM0RNoWLVVmP0=
 4 github.com/safchain/ethtool v0.3.0/go.mod h1:SA9BwrgyAqNo7M+uaL6IYbxpm5wk3L7Mm6ocLW+CJUs=
 5-github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
 6-github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
 7-github.com/samber/slog-multi v1.2.3 h1:np8YoAZbGP699xA92SYZxs7zzKpL1/yBYk6q8/caXpc=
 8-github.com/samber/slog-multi v1.2.3/go.mod h1:ACuZ5B6heK57TfMVkVknN2UZHoFfjCwRxR0Q2OXKHlo=
 9 github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
10 github.com/secure-io/sio-go v0.3.1 h1:dNvY9awjabXTYGsTF1PiCySl9Ltofk9GA3VdWlo7rRc=
11 github.com/secure-io/sio-go v0.3.1/go.mod h1:+xbkjDzPjwh4Axd07pRKSNriS9SCiYksWnZqdnfpQxs=
M shared/sendlog.go
+78, -8
  1@@ -1,6 +1,7 @@
  2 package shared
  3 
  4 import (
  5+	"context"
  6 	"errors"
  7 	"fmt"
  8 	"io"
  9@@ -8,14 +9,80 @@ import (
 10 	"net"
 11 	"os"
 12 	"path/filepath"
 13+	"slices"
 14 	"strings"
 15 	"sync"
 16 	"time"
 17 
 18-	slogmulti "github.com/samber/slog-multi"
 19 	"golang.org/x/crypto/ssh"
 20 )
 21 
 22+type MultiHandler struct {
 23+	Handlers []slog.Handler
 24+	mu       sync.Mutex
 25+}
 26+
 27+func (m *MultiHandler) Enabled(ctx context.Context, l slog.Level) bool {
 28+	m.mu.Lock()
 29+	defer m.mu.Unlock()
 30+
 31+	for _, h := range m.Handlers {
 32+		if h.Enabled(ctx, l) {
 33+			return true
 34+		}
 35+	}
 36+
 37+	return false
 38+}
 39+
 40+func (m *MultiHandler) Handle(ctx context.Context, r slog.Record) error {
 41+	m.mu.Lock()
 42+	defer m.mu.Unlock()
 43+
 44+	var errs []error
 45+	for _, h := range m.Handlers {
 46+		if h.Enabled(ctx, r.Level) {
 47+			errs = append(errs, h.Handle(ctx, r.Clone()))
 48+		}
 49+	}
 50+
 51+	return errors.Join(errs...)
 52+}
 53+
 54+func (m *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
 55+	m.mu.Lock()
 56+	defer m.mu.Unlock()
 57+
 58+	var handlers []slog.Handler
 59+
 60+	for _, h := range m.Handlers {
 61+		handlers = append(handlers, h.WithAttrs(slices.Clone(attrs)))
 62+	}
 63+
 64+	return &MultiHandler{
 65+		Handlers: handlers,
 66+	}
 67+}
 68+
 69+func (m *MultiHandler) WithGroup(name string) slog.Handler {
 70+	if name == "" {
 71+		return m
 72+	}
 73+
 74+	m.mu.Lock()
 75+	defer m.mu.Unlock()
 76+
 77+	var handlers []slog.Handler
 78+
 79+	for _, h := range m.Handlers {
 80+		handlers = append(handlers, h.WithGroup(name))
 81+	}
 82+
 83+	return &MultiHandler{
 84+		Handlers: handlers,
 85+	}
 86+}
 87+
 88 type SendLogWriter struct {
 89 	SSHClient        *ssh.Client
 90 	Session          *ssh.Session
 91@@ -244,14 +311,17 @@ func SendLogRegister(logger *slog.Logger, buffer int) (*slog.Logger, error) {
 92 	logWriter.Reconnect()
 93 
 94 	return slog.New(
 95-		slogmulti.Fanout(
 96-			currentHandler,
 97-			slog.NewJSONHandler(logWriter, &slog.HandlerOptions{
 98-				AddSource: true,
 99-				Level:     slog.LevelDebug,
100-			}),
101-		),
102+		&MultiHandler{
103+			Handlers: []slog.Handler{
104+				currentHandler,
105+				slog.NewJSONHandler(logWriter, &slog.HandlerOptions{
106+					AddSource: true,
107+					Level:     slog.LevelDebug,
108+				}),
109+			},
110+		},
111 	), nil
112 }
113 
114 var _ io.Writer = (*SendLogWriter)(nil)
115+var _ slog.Handler = (*MultiHandler)(nil)