Skip to content

Commit

Permalink
Refactor logging statements in various files
Browse files Browse the repository at this point in the history
Several log printing statements were revised for consistency. The standard logger, "log" is now used in all the different files, ensuring identical log output format and handling. Additionally, unused or unnecessary code related to logging was removed, and some minor adjustments were performed.
  • Loading branch information
mleku committed Jan 11, 2024
1 parent d55bc54 commit d2cea51
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 99 deletions.
2 changes: 1 addition & 1 deletion cmd/algia/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (cfg *C) GetFollows(profile string) (profiles Follows, e error) {
mu.Unlock()
m := make(Checklist)
cfg.Do(rp, cfg.GetRelaysAndTags(pub, m, &mu))
log.D.F("found %d followers\n", len(m))
log.D.F("found %d followers", len(m))
if len(m) > 0 {
var follows []string
for k := range m {
Expand Down
14 changes: 0 additions & 14 deletions cmd/algia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,6 @@ func main() {
HelpName: "profile",
Action: doProfile,
},
{
Name: "powa",
Usage: "post ぽわ〜",
UsageText: appName + " powa",
HelpName: "powa",
Action: doPowa,
},
{
Name: "puru",
Usage: "post ぷる",
UsageText: appName + " puru",
HelpName: "puru",
Action: doPuru,
},
{
Name: "zap",
Flags: []cli.Flag{
Expand Down
34 changes: 19 additions & 15 deletions cmd/algia/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"os"

"github.com/Hubmakerlabs/replicatr/pkg/context"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/event"
Expand Down Expand Up @@ -35,39 +34,44 @@ func doProfile(cCtx *cli.Context) (e error) {
return fmt.Errorf("failed to parse pubkey from '%s'", user)
}
}

// get set-metadata
f := filter.T{
Kinds: []int{event.KindProfileMetadata},
Authors: []string{pub},
Limit: 1,
}

evs := cfg.Events(f)
if len(evs) == 0 {
return errors.New("cannot find user")
}

if j {
fmt.Fprintln(os.Stdout, evs[0].Content)
fmt.Println(evs[0].Content)
return nil
}
var profile Profile
e = json.Unmarshal([]byte(evs[0].Content), &profile)
var p Profile
e = json.Unmarshal([]byte(evs[0].Content), &p)
if log.Fail(e) {
return e
}
npub, e := nip19.EncodePublicKey(pub)
if log.Fail(e) {
return e
}
fmt.Printf("Pubkey: %v\n", npub)
fmt.Printf("Name: %v\n", profile.Name)
fmt.Printf("DisplayName: %v\n", profile.DisplayName)
fmt.Printf("WebSite: %v\n", profile.Website)
fmt.Printf("Picture: %v\n", profile.Picture)
fmt.Printf("NIP-05: %v\n", profile.Nip05)
fmt.Printf("LUD-16: %v\n", profile.Lud16)
fmt.Printf("About: %v\n", profile.About)
fmt.Printf("Pubkey: %v\n"+
"Name: %v\n"+
"DisplayName: %v\n"+
"WebSite: %v\n"+
"Picture: %v\n"+
"NIP-05: %v\n"+
"LUD-16: %v\n"+
"About:\n%v\n",
npub,
p.Name,
p.DisplayName,
p.Website,
p.Picture,
p.Nip05,
p.Lud16,
p.About)
return nil
}
14 changes: 3 additions & 11 deletions cmd/algia/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,9 @@ func doTimeline(cCtx *cli.Context) (e error) {
extra := cCtx.Bool("extra")
cfg := cCtx.App.Metadata["config"].(*C)
// get followers
followsMap, e := cfg.GetFollows(cCtx.String("a"))
if log.Fail(e) {
return e
var followsMap Follows
if followsMap, e = cfg.GetFollows(cCtx.String("a")); log.Fail(e) {
return
}
var follows []string
for k := range followsMap {
Expand Down Expand Up @@ -789,11 +789,3 @@ func postMsg(cCtx *cli.Context, msg string) (e error) {
}
return
}

func doPowa(cCtx *cli.Context) (e error) {
return postMsg(cCtx, "ぽわ〜")
}

func doPuru(cCtx *cli.Context) (e error) {
return postMsg(cCtx, "(((( ˙꒳​˙ ))))プルプルプルプルプルプルプル")
}
6 changes: 3 additions & 3 deletions cmd/replicatrd/replicatr/websockethandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
var conn *websocket.Conn
conn, e = rl.upgrader.Upgrade(w, r, nil)
if rl.E.Chk(e) {
rl.E.F("failed to upgrade websocket: %v\n", e)
rl.E.F("failed to upgrade websocket: %v", e)
return
}
rl.clients.Store(conn, struct{}{})
Expand Down Expand Up @@ -226,7 +226,7 @@ func (rl *Relay) websocketReadMessages(c context.T, kill func(),
websocket.CloseNoStatusReceived, // 1005
websocket.CloseAbnormalClosure, // 1006
) {
rl.E.F("unexpected close error from %s: %v\n",
rl.E.F("unexpected close error from %s: %v",
r.Header.Get("X-Forwarded-For"), e)
}
return
Expand All @@ -249,7 +249,7 @@ func (rl *Relay) websocketWatcher(c context.T, kill func(), t *time.Ticker, ws *
case <-t.C:
if e = ws.WriteMessage(websocket.PingMessage, nil); rl.E.Chk(e) {
if !strings.HasSuffix(e.Error(), "use of closed network connection") {
rl.E.F("error writing ping: %v; closing websocket\n", e)
rl.E.F("error writing ping: %v; closing websocket", e)
}
return
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/eventstore/badger/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (b *BadgerBackend) CountEvents(c context.T, f *filter.T) (int64, error) {
return
}
log.D.F("badger: count (%v) failed to get %d from raw " +
"event store: %s\n", q, idx)
"event store: %s", q, idx)
return
}

Expand All @@ -77,7 +77,7 @@ func (b *BadgerBackend) CountEvents(c context.T, f *filter.T) (int64, error) {
return nil
})
if log.Fail(e) {
log.D.F("badger: count value read error: %s\n", e)
log.D.F("badger: count value read error: %s", e)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/eventstore/badger/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ func (b BadgerBackend) QueryEvents(c context.T, f *filter.T) (chan *event.T, err
if e == badger.ErrDiscardedTxn {
return
}
log.D.F("badger: failed to get %x based on prefix %x, index key %x from raw event store: %s\n",
log.D.F("badger: failed to get %x based on prefix %x, index key %x from raw event store: %s",
idx, q.prefix, key, e)
return
}
item.Value(func(val []byte) (e error) {
evt := &event.T{}
if e := nostr_binary.Unmarshal(val, evt); e != nil {
log.D.F("badger: value read error (id %x): %s\n", val[0:32], e)
log.D.F("badger: value read error (id %x): %s", val[0:32], e)
return e
}

Expand Down Expand Up @@ -160,7 +160,7 @@ func (b BadgerBackend) QueryEvents(c context.T, f *filter.T) (chan *event.T, err
return nil
})
if e != nil {
log.D.F("badger: query txn error: %s\n", e)
log.D.F("badger: query txn error: %s", e)
}
}()

Expand Down
2 changes: 1 addition & 1 deletion pkg/go-nostr/envelope/envelopes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestParseMessage(t *testing.T) {
return
}
if testCase.ExpectedEnvelope == nil && env != nil {
t.Fatalf("expected nil but got %v\n", env)
t.Fatalf("expected nil but got %v", env)
}
if testCase.ExpectedEnvelope.String() != env.String() {
t.Fatalf("unexpected output:\n %s\n != %s", testCase.ExpectedEnvelope, env)
Expand Down
8 changes: 4 additions & 4 deletions pkg/go-nostr/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
)

type T struct {
ID string `json:"id"`
ID string `json:"id"`
PubKey string `json:"pubkey"`
CreatedAt timestamp.T `json:"created_at"`
Kind int `json:"kind"`
Tags tags.Tags `json:"tags"`
Content string `json:"content"`
Sig string `json:"sig"`
Tags tags.Tags `json:"tags"`
Content string `json:"content"`
Sig string `json:"sig"`

// anything here will be mashed together with the main event object when serializing
extra map[string]any
Expand Down
11 changes: 7 additions & 4 deletions pkg/go-nostr/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/Hubmakerlabs/replicatr/pkg/context"
log2 "github.com/Hubmakerlabs/replicatr/pkg/log"

"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/event"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/filter"
Expand All @@ -19,6 +20,8 @@ import (
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/timestamp"
)

var log = log2.GetStd()

func main() {
ctx, cancel := context.Timeout(context.Bg(), 3*time.Second)

Expand All @@ -32,7 +35,7 @@ func main() {
reader := os.Stdin
var npub string
var b [64]byte
fmt.Fprintf(os.Stderr, "using %s\n----\nexample subscription for three most recent notes mentioning user\npaste npub key: ", url)
log.D.F("using %s\n----\nexample subscription for three most recent notes mentioning user\npaste npub key: ", url)
if n, e := reader.Read(b[:]); e == nil {
npub = strings.TrimSpace(fmt.Sprintf("%s", b[:n]))
} else {
Expand Down Expand Up @@ -73,7 +76,7 @@ func main() {

filename := "example_output.json"
if f, e := os.Create(filename); e == nil {
fmt.Fprintf(os.Stderr, "returned events saved to %s\n", filename)
log.D.F("returned events saved to %s\n", filename)
// encode the returned events in a file
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
Expand All @@ -83,7 +86,7 @@ func main() {
panic(e)
}

fmt.Fprintf(os.Stderr, "----\nexample publication of note.\npaste nsec key (leave empty to autogenerate): ")
log.D.F("----\nexample publication of note.\npaste nsec key (leave empty to autogenerate): ")
var nsec string
if n, e := reader.Read(b[:]); e == nil {
nsec = strings.TrimSpace(fmt.Sprintf("%s", b[:n]))
Expand Down Expand Up @@ -130,6 +133,6 @@ func main() {
continue
}
fmt.Println("posting to: ", url)
rl.Publish(ctx, ev)
log.Fail(rl.Publish(ctx, &ev))
}
}
7 changes: 4 additions & 3 deletions pkg/go-nostr/pools/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package pools

import (
"fmt"
"log"
"strings"
"sync"
"time"

"github.com/Hubmakerlabs/replicatr/pkg/context"
log2 "github.com/Hubmakerlabs/replicatr/pkg/log"

"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/event"
"github.com/Hubmakerlabs/replicatr/pkg/go-nostr/filter"
Expand All @@ -17,6 +17,7 @@ import (
"github.com/Hubmakerlabs/replicatr/pkg/nostr/normalize"
"github.com/puzpuzpuz/xsync/v2"
)
var log = log2.GetStd()

const (
seenAlreadyDropTick = time.Minute
Expand Down Expand Up @@ -195,7 +196,7 @@ func (pool *SimplePool) subMany(c context.T, urls []string, filters filters.T, u
goto subscribe
}
} else {
log.Printf("CLOSED from %s: '%s'\n", nm, reason)
log.D.F("CLOSED from %s: '%s'", nm, reason)
}
return
case <-c.Done():
Expand Down Expand Up @@ -273,7 +274,7 @@ func (pool *SimplePool) subManyEose(c context.T, urls []string, filters filters.
goto subscribe
}
}
log.Printf("CLOSED from %s: '%s'\n", nm, reason)
log.D.F("CLOSED from %s: '%s'", nm, reason)
return
case evt, more := <-sub.Events:
if !more {
Expand Down
Loading

0 comments on commit d2cea51

Please sign in to comment.