Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit e572ea3

Browse files
committed
Add debug log flag
1 parent 2668c40 commit e572ea3

14 files changed

Lines changed: 164 additions & 40 deletions

File tree

cmd/pcp/pcp.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ func main() {
4242
receive.Command,
4343
send.Command,
4444
},
45+
Before: func(c *cli.Context) error {
46+
if c.Bool("debug") {
47+
log.SetLevel(log.DebugLevel)
48+
}
49+
return nil
50+
},
51+
Flags: []cli.Flag{
52+
&cli.BoolFlag{
53+
Name: "debug",
54+
Usage: "enables debug log output",
55+
},
56+
},
4557
}
4658

4759
sigs := make(chan os.Signal, 1)

internal/log/log.go

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,35 @@ import (
55
"io"
66
"os"
77
"strings"
8+
"time"
89

910
"golang.org/x/crypto/ssh/terminal"
1011
)
1112

13+
type Level uint8
14+
15+
const (
16+
DebugLevel Level = iota
17+
InfoLevel
18+
WarningLevel
19+
ErrorLevel
20+
)
21+
22+
var level Level
23+
24+
func SetLevel(l Level) {
25+
level = l
26+
}
27+
1228
// Out represents the writer to print the log messages to.
1329
// This is used for tests.
1430
var Out io.Writer = os.Stderr
1531

1632
var tWidth int
1733

1834
func init() {
35+
level = InfoLevel
36+
1937
var err error
2038
tWidth, _, err = terminal.GetSize(int(os.Stdout.Fd()))
2139
if err != nil {
@@ -26,55 +44,111 @@ func init() {
2644
}
2745
}
2846

47+
func printTimestamp() {
48+
if level > DebugLevel {
49+
return
50+
}
51+
fmt.Printf("[%s] ", time.Now().Format(time.RFC3339))
52+
}
53+
2954
func Info(a ...interface{}) {
55+
if level > InfoLevel {
56+
return
57+
}
58+
printTimestamp()
3059
fmt.Fprint(Out, a...)
3160
}
3261

3362
func Infoln(a ...interface{}) {
63+
if level > InfoLevel {
64+
return
65+
}
66+
printTimestamp()
3467
fmt.Fprintln(Out, a...)
3568
}
3669

3770
func Infor(format string, a ...interface{}) {
38-
blank := fmt.Sprintf("\r%s\r", strings.Repeat(" ", tWidth))
39-
fmt.Fprint(Out, fmt.Sprintf("%s%s", blank, fmt.Sprintf(format, a...)))
71+
if level > InfoLevel {
72+
return
73+
}
74+
75+
if level > DebugLevel {
76+
blank := fmt.Sprintf("\r%s\r", strings.Repeat(" ", tWidth))
77+
printTimestamp()
78+
fmt.Fprint(Out, fmt.Sprintf("%s%s", blank, fmt.Sprintf(format, a...)))
79+
} else {
80+
printTimestamp()
81+
fmt.Fprintln(Out, fmt.Sprintf(format, a...))
82+
}
4083
}
4184

4285
func Infof(format string, a ...interface{}) {
86+
if level > InfoLevel {
87+
return
88+
}
89+
printTimestamp()
4390
fmt.Fprintf(Out, format, a...)
4491
}
4592

4693
func Debug(a ...interface{}) {
94+
if level > DebugLevel {
95+
return
96+
}
97+
printTimestamp()
4798
fmt.Fprint(Out, a...)
4899
}
49100

50101
func Debugln(a ...interface{}) {
102+
if level > DebugLevel {
103+
return
104+
}
105+
printTimestamp()
51106
fmt.Fprintln(Out, a...)
52107
}
53108

54109
func Debugf(format string, a ...interface{}) {
110+
if level > DebugLevel {
111+
return
112+
}
113+
printTimestamp()
55114
fmt.Fprintf(Out, format, a...)
56115
}
57116

58117
func Warning(a ...interface{}) {
118+
if level > WarningLevel {
119+
return
120+
}
121+
printTimestamp()
59122
fmt.Fprint(Out, a...)
60123
}
61124

62125
func Warningln(a ...interface{}) {
126+
if level > WarningLevel {
127+
return
128+
}
129+
printTimestamp()
63130
fmt.Fprintln(Out, a...)
64131
}
65132

66133
func Warningf(format string, a ...interface{}) {
134+
if level > WarningLevel {
135+
return
136+
}
137+
printTimestamp()
67138
fmt.Fprintf(Out, format, a...)
68139
}
69140

70141
func Error(a ...interface{}) {
142+
printTimestamp()
71143
fmt.Fprint(Out, a...)
72144
}
73145

74146
func Errorln(a ...interface{}) {
147+
printTimestamp()
75148
fmt.Fprintln(Out, a...)
76149
}
77150

78151
func Errorf(format string, a ...interface{}) {
152+
printTimestamp()
79153
fmt.Fprintf(Out, format, a...)
80154
}

pkg/dht/advertiser.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (a *Advertiser) Advertise(chanID int) error {
5353
}
5454
defer a.ServiceStopped()
5555

56+
log.Debugln("DHT - Waiting for public IP...")
5657
for {
5758
// Only advertise in the DHT if we have a public addr.
5859
if !a.HasPublicAddr() {
@@ -63,7 +64,11 @@ func (a *Advertiser) Advertise(chanID int) error {
6364
continue
6465
}
6566
}
67+
log.Debugln("DHT - Identified a public IP in", a.Addrs())
68+
break
69+
}
6670

71+
for {
6772
err := a.provide(a.ServiceContext(), a.DiscoveryID(chanID))
6873
if err == context.Canceled {
6974
break
@@ -97,6 +102,8 @@ func (a *Advertiser) Shutdown() {
97102
// closest peers to the key/CID before it goes on to provide the record to them.
98103
// Not setting a timeout here will make the DHT wander forever.
99104
func (a *Advertiser) provide(ctx context.Context, id string) error {
105+
log.Debugln("DHT - Advertising", id)
106+
defer log.Debugln("DHT - Advertising", id, "done")
100107
cID, err := strToCid(id)
101108
if err != nil {
102109
return err

pkg/dht/discoverer.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"time"
66

7+
"github.com/dennis-tra/pcp/internal/log"
8+
79
"github.com/libp2p/go-libp2p-core/host"
810
"github.com/libp2p/go-libp2p-core/peer"
911
ma "github.com/multiformats/go-multiaddr"
@@ -36,19 +38,23 @@ func (d *Discoverer) Discover(chanID int, handler func(info peer.AddrInfo)) erro
3638
}
3739

3840
for {
39-
cID, err := strToCid(d.DiscoveryID(chanID))
41+
did := d.DiscoveryID(chanID)
42+
log.Debugln("DHT - Discovering ", did)
43+
cID, err := strToCid(did)
4044
if err != nil {
4145
return err
4246
}
4347

4448
// Find new provider with a timeout, so the discovery ID is renewed if necessary.
4549
ctx, cancel := context.WithTimeout(d.ServiceContext(), provideTimeout)
4650
for pi := range d.dht.FindProvidersAsync(ctx, cID, 100) {
51+
log.Debugln("DHT - Found peer ", pi.ID)
4752
pi.Addrs = onlyPublic(pi.Addrs)
4853
if isRoutable(pi) {
4954
go handler(pi)
5055
}
5156
}
57+
log.Debugln("DHT - Discovering", did, " done.")
5258

5359
// cannot defer cancel in this for loop
5460
cancel()
@@ -76,6 +82,9 @@ func onlyPublic(addrs []ma.Multiaddr) []ma.Multiaddr {
7682
for _, addr := range addrs {
7783
if manet.IsPublicAddr(addr) {
7884
routable = append(routable, addr)
85+
log.Debugf("\tpublic - %s\n", addr.String())
86+
} else {
87+
log.Debugf("\tprivate - %s\n", addr.String())
7988
}
8089
}
8190
return routable

pkg/dht/protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type protocol struct {
4848

4949
func newProtocol(h host.Host, dht wrap.IpfsDHT) *protocol {
5050
bootstrap[h.ID()] = &sync.Once{}
51-
return &protocol{Host: h, dht: dht, Service: service.New()}
51+
return &protocol{Host: h, dht: dht, Service: service.New("DHT")}
5252
}
5353

5454
// Bootstrap connects to a set of bootstrap nodes to connect

pkg/mdns/advertiser.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"time"
66

7+
"github.com/dennis-tra/pcp/internal/log"
8+
79
"github.com/libp2p/go-libp2p-core/host"
810
"github.com/libp2p/go-libp2p/p2p/discovery"
911
)
@@ -26,17 +28,22 @@ func (a *Advertiser) Advertise(chanID int) error {
2628
defer a.ServiceStopped()
2729

2830
for {
31+
did := a.DiscoveryID(chanID)
32+
log.Debugln("mDNS - Advertising ", did)
2933
ctx, cancel := context.WithTimeout(a.ServiceContext(), time.Minute)
30-
mdns, err := discovery.NewMdnsService(ctx, a, a.interval, a.DiscoveryID(chanID))
34+
mdns, err := discovery.NewMdnsService(ctx, a, a.interval, did)
3135
if err != nil {
36+
cancel()
3237
return err
3338
}
3439

3540
select {
3641
case <-a.SigShutdown():
42+
log.Debugln("mDNS - Advertising", did, " done - shutdown signal")
3743
cancel()
3844
return mdns.Close()
3945
case <-ctx.Done():
46+
log.Debugln("mDNS - Advertising", did, "done -", ctx.Err())
4047
cancel()
4148
if ctx.Err() == context.DeadlineExceeded {
4249
_ = mdns.Close()

pkg/mdns/discoverer.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ func (d *Discoverer) Discover(chanID int, handler func(info peer.AddrInfo)) erro
3232
entriesCh := make(chan *mdns.ServiceEntry, 16)
3333
go d.drainEntriesChan(entriesCh, handler)
3434

35+
did := d.DiscoveryID(chanID)
36+
log.Debugln("mDNS - Discovering", did)
3537
qp := &mdns.QueryParam{
3638
Domain: "local",
3739
Entries: entriesCh,
38-
Service: d.DiscoveryID(chanID),
40+
Service: did,
3941
Timeout: time.Second * 5,
4042
}
4143

4244
err := mdns.Query(qp)
45+
log.Debugln("mDNS - Discovering", did, " done.")
4346
if err != nil {
44-
log.Warningln("mdns lookup error", err)
47+
log.Warningln("mDNS - query error", err)
4548
}
4649
close(entriesCh)
4750

@@ -65,6 +68,8 @@ func (d *Discoverer) drainEntriesChan(entries chan *mdns.ServiceEntry, handler f
6568
continue
6669
}
6770

71+
log.Debugln("mDNS - Found peer", pi.ID)
72+
6873
if pi.ID == d.ID() {
6974
continue
7075
}
@@ -114,6 +119,9 @@ func onlyPrivate(addrs []ma.Multiaddr) []ma.Multiaddr {
114119
for _, addr := range addrs {
115120
if manet.IsPrivateAddr(addr) {
116121
routable = append(routable, addr)
122+
log.Debugf("\tprivate - %s\n", addr.String())
123+
} else {
124+
log.Debugf("\tpublic - %s\n", addr.String())
117125
}
118126
}
119127
return routable

pkg/mdns/protocol.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func newProtocol(h host.Host) *protocol {
3232
return &protocol{
3333
Host: h,
3434
interval: time.Second,
35-
Service: service.New(),
35+
Service: service.New("mDNS"),
3636
}
3737
}
3838

0 commit comments

Comments
 (0)