mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-05-06 16:50:14 +02:00
improved logging thing with the "debug" build tag.
This commit is contained in:
parent
7b0af23f1a
commit
6f74d284c4
14
lib.go
14
lib.go
@ -1,14 +0,0 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
var (
|
||||
// call SetOutput on InfoLogger to enable info logging
|
||||
InfoLogger = log.New(ioutil.Discard, "[go-nostr][info] ", log.LstdFlags)
|
||||
|
||||
// call SetOutput on DebugLogger to enable debug logging
|
||||
DebugLogger = log.New(ioutil.Discard, "[go-nostr][debug] ", log.LstdFlags)
|
||||
)
|
14
log.go
Normal file
14
log.go
Normal file
@ -0,0 +1,14 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
// call SetOutput on InfoLogger to enable info logging
|
||||
InfoLogger = log.New(os.Stderr, "[go-nostr][info] ", log.LstdFlags)
|
||||
|
||||
// call SetOutput on DebugLogger to enable debug logging
|
||||
DebugLogger = log.New(os.Stderr, "[go-nostr][debug] ", log.LstdFlags)
|
||||
)
|
24
log_debug.go
Normal file
24
log_debug.go
Normal file
@ -0,0 +1,24 @@
|
||||
//go:build debug
|
||||
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func debugLog(str string, args ...any) {
|
||||
for i, v := range args {
|
||||
switch v.(type) {
|
||||
case []json.RawMessage:
|
||||
j, _ := json.Marshal(v)
|
||||
args[i] = string(j)
|
||||
case []byte:
|
||||
args[i] = string(v.([]byte))
|
||||
case fmt.Stringer:
|
||||
args[i] = v.(fmt.Stringer).String()
|
||||
}
|
||||
}
|
||||
|
||||
DebugLogger.Printf(str, args...)
|
||||
}
|
7
log_normal.go
Normal file
7
log_normal.go
Normal file
@ -0,0 +1,7 @@
|
||||
//go:build !debug
|
||||
|
||||
package nostr
|
||||
|
||||
func debugLog(str string, args ...any) {
|
||||
return
|
||||
}
|
10
relay.go
10
relay.go
@ -160,7 +160,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
|
||||
switch command {
|
||||
case "NOTICE":
|
||||
DebugLogger.Printf("{%s} %v\n", r.URL, jsonMessage)
|
||||
debugLog("{%s} %v\n", r.URL, jsonMessage)
|
||||
var content string
|
||||
json.Unmarshal(jsonMessage[1], &content)
|
||||
go func() {
|
||||
@ -171,7 +171,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
r.mutex.RUnlock()
|
||||
}()
|
||||
case "AUTH":
|
||||
DebugLogger.Printf("{%s} %v\n", r.URL, jsonMessage)
|
||||
debugLog("{%s} %v\n", r.URL, jsonMessage)
|
||||
var challenge string
|
||||
json.Unmarshal(jsonMessage[1], &challenge)
|
||||
go func() {
|
||||
@ -228,7 +228,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
if len(jsonMessage) < 2 {
|
||||
continue
|
||||
}
|
||||
DebugLogger.Printf("{%s} %v\n", r.URL, jsonMessage)
|
||||
debugLog("{%s} %v\n", r.URL, jsonMessage)
|
||||
var subId string
|
||||
json.Unmarshal(jsonMessage[1], &subId)
|
||||
if subscription, ok := r.subscriptions.Load(subId); ok {
|
||||
@ -240,7 +240,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
if len(jsonMessage) < 3 {
|
||||
continue
|
||||
}
|
||||
DebugLogger.Printf("{%s} %v\n", r.URL, jsonMessage)
|
||||
debugLog("{%s} %v\n", r.URL, jsonMessage)
|
||||
var (
|
||||
eventId string
|
||||
ok bool
|
||||
@ -376,7 +376,7 @@ func (r *Relay) Auth(ctx context.Context, event Event) (Status, error) {
|
||||
|
||||
// send AUTH
|
||||
authResponse := []interface{}{"AUTH", event}
|
||||
DebugLogger.Printf("{%s} sending %v\n", r.URL, authResponse)
|
||||
debugLog("{%s} sending %v\n", r.URL, authResponse)
|
||||
if err := r.Connection.WriteJSON(authResponse); err != nil {
|
||||
// status will be "failed"
|
||||
return status, err
|
||||
|
@ -68,7 +68,7 @@ func (sub *Subscription) Fire() error {
|
||||
message = append(message, filter)
|
||||
}
|
||||
|
||||
DebugLogger.Printf("{%s} sending %v", sub.Relay.URL, message)
|
||||
debugLog("{%s} sending %v", sub.Relay.URL, message)
|
||||
|
||||
err := sub.conn.WriteJSON(message)
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user