update logging in multiple packages
Jeromy committed
Sep 28, 2014 at 00:13 UTC
54142b2173e07c493659bd280420360cbe87284c
6 files changed
+85
-45
blockservice/blockservice.go
+9
-6
@@ -7,12 +7,15 @@ import (
7
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
9
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10
+ "github.com/op/go-logging"
11
12
blocks "github.com/jbenet/go-ipfs/blocks"
13
exchange "github.com/jbenet/go-ipfs/exchange"
14
u "github.com/jbenet/go-ipfs/util"
15
)
16
17
+var log = logging.MustGetLogger("blockservice")
18
+
19
// BlockService is a block datastore.
20
// It uses an internal `datastore.Datastore` instance to store values.
21
type BlockService struct {
@@ -26,7 +29,7 @@ func NewBlockService(d ds.Datastore, rem exchange.Interface) (*BlockService, err
29
return nil, fmt.Errorf("BlockService requires valid datastore")
30
}
31
if rem == nil {
29
- u.DErr("Caution: blockservice running in local (offline) mode.\n")
32
+ log.Warning("blockservice running in local (offline) mode.")
33
}
34
return &BlockService{Datastore: d, Remote: rem}, nil
35
}
@@ -35,7 +38,7 @@ func NewBlockService(d ds.Datastore, rem exchange.Interface) (*BlockService, err
38
func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
39
k := b.Key()
40
dsk := ds.NewKey(string(k))
38
- u.DOut("storing [%s] in datastore\n", k.Pretty())
41
+ log.Debug("storing [%s] in datastore", k.Pretty())
42
// TODO(brian): define a block datastore with a Put method which accepts a
43
// block parameter
44
err := s.Datastore.Put(dsk, b.Data)
@@ -52,11 +55,11 @@ func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
55
// GetBlock retrieves a particular block from the service,
56
// Getting it from the datastore using the key (hash).
57
func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
55
- u.DOut("BlockService GetBlock: '%s'\n", k.Pretty())
58
+ log.Debug("BlockService GetBlock: '%s'", k.Pretty())
59
dsk := ds.NewKey(string(k))
60
datai, err := s.Datastore.Get(dsk)
61
if err == nil {
59
- u.DOut("Blockservice: Got data in datastore.\n")
62
+ log.Debug("Blockservice: Got data in datastore.")
63
bdata, ok := datai.([]byte)
64
if !ok {
65
return nil, fmt.Errorf("data associated with %s is not a []byte", k)
@@ -66,7 +69,7 @@ func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
69
Data: bdata,
70
}, nil
71
} else if err == ds.ErrNotFound && s.Remote != nil {
69
- u.DOut("Blockservice: Searching bitswap.\n")
72
+ log.Debug("Blockservice: Searching bitswap.")
73
ctx, _ := context.WithTimeout(context.TODO(), 5*time.Second)
74
blk, err := s.Remote.Block(ctx, k)
75
if err != nil {
@@ -74,7 +77,7 @@ func (s *BlockService) GetBlock(k u.Key) (*blocks.Block, error) {
77
}
78
return blk, nil
79
} else {
77
- u.DOut("Blockservice GetBlock: Not found.\n")
80
+ log.Debug("Blockservice GetBlock: Not found.")
81
return nil, u.ErrNotFound
82
}
83
}
fuse/ipns/ipns_unix.go
+46
-10
@@ -79,7 +79,9 @@ func CreateRoot(n *core.IpfsNode, keys []ci.PrivKey, ipfsroot string) (*Root, er
79
80
pointsTo, err := n.Namesys.Resolve(name)
81
if err != nil {
82
- log.Warning("Could not resolve value for local ipns entry")
82
+ log.Warning("Could not resolve value for local ipns entry, providing empty dir")
83
+ nd.Nd = &mdag.Node{Data: mdag.FolderPBData()}
84
+ root.LocalDirs[name] = nd
85
continue
86
}
87
@@ -147,16 +149,16 @@ func (s *Root) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
149
log.Debug("ipns: Falling back to resolution for [%s].", name)
150
resolved, err := s.Ipfs.Namesys.Resolve(name)
151
if err != nil {
150
- log.Error("ipns: namesys resolve error: %s", err)
152
+ log.Warning("ipns: namesys resolve error: %s", err)
153
return nil, fuse.ENOENT
154
}
155
154
- return &Link{s.IpfsRoot + "/" + u.Key(resolved).Pretty()}, nil
156
+ return &Link{s.IpfsRoot + "/" + resolved}, nil
157
}
158
159
// ReadDir reads a particular directory. Disallowed for root.
160
func (r *Root) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
159
- u.DOut("Read Root.\n")
161
+ log.Debug("Read Root.")
162
listing := []fuse.Dirent{
163
fuse.Dirent{
164
Name: "local",
@@ -255,7 +257,7 @@ func (n *Node) makeChild(name string, node *mdag.Node) *Node {
257
258
// ReadDir reads the link structure as directory entries
259
func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
258
- u.DOut("Node ReadDir\n")
260
+ log.Debug("Node ReadDir")
261
entries := make([]fuse.Dirent, len(s.Nd.Links))
262
for i, link := range s.Nd.Links {
263
n := link.Name
@@ -370,10 +372,8 @@ func (n *Node) Fsync(req *fuse.FsyncRequest, intr fs.Intr) fuse.Error {
372
373
func (n *Node) Mkdir(req *fuse.MkdirRequest, intr fs.Intr) (fs.Node, fuse.Error) {
374
log.Debug("Got mkdir request!")
373
- dagnd := new(mdag.Node)
374
- dagnd.Data = mdag.FolderPBData()
375
+ dagnd := &mdag.Node{Data: mdag.FolderPBData()}
376
n.Nd.AddNodeLink(req.Name, dagnd)
376
- n.changed = true
377
378
child := &Node{
379
Ipfs: n.Ipfs,
@@ -386,6 +386,7 @@ func (n *Node) Mkdir(req *fuse.MkdirRequest, intr fs.Intr) (fs.Node, fuse.Error)
386
child.nsRoot = n.nsRoot
387
}
388
389
+ n.changed = true
390
n.updateTree()
391
392
return child, nil
@@ -404,8 +405,9 @@ func (n *Node) Open(req *fuse.OpenRequest, resp *fuse.OpenResponse, intr fs.Intr
405
406
func (n *Node) Create(req *fuse.CreateRequest, resp *fuse.CreateResponse, intr fs.Intr) (fs.Node, fs.Handle, fuse.Error) {
407
log.Debug("Got create request: %s", req.Name)
407
- nd := new(mdag.Node)
408
- nd.Data = mdag.FilePBData(nil)
408
+
409
+ // New 'empty' file
410
+ nd := &mdag.Node{Data: mdag.FilePBData(nil)}
411
child := n.makeChild(req.Name, nd)
412
413
err := n.Nd.AddNodeLink(req.Name, nd)
@@ -545,3 +547,37 @@ func (l *Link) Readlink(req *fuse.ReadlinkRequest, intr fs.Intr) (string, fuse.E
547
log.Debug("ReadLink: %s", l.Target)
548
return l.Target, nil
549
}
550
+
551
+type Republisher struct {
552
+ Timeout time.Duration
553
+ Publish chan struct{}
554
+ node *Node
555
+}
556
+
557
+func NewRepublisher(n *Node, tout time.Duration) *Republisher {
558
+ return &Republisher{
559
+ Timeout: tout,
560
+ Publish: make(chan struct{}),
561
+ node: n,
562
+ }
563
+}
564
+
565
+func (np *Republisher) Run() {
566
+ for _ := range np.Publish {
567
+ timer := time.After(np.Timeout)
568
+ for {
569
+ select {
570
+ case <-timer:
571
+ //Do the publish!
572
+ err := np.node.updateTree()
573
+ if err != nil {
574
+ log.Critical("updateTree error: %s", err)
575
+ }
576
+ goto done
577
+ case <-np.Publish:
578
+ timer = time.After(np.Timeout)
579
+ }
580
+ }
581
+ done:
582
+ }
583
+}
fuse/readonly/readonly_unix.go
+9
-8
@@ -21,8 +21,11 @@ import (
21
core "github.com/jbenet/go-ipfs/core"
22
mdag "github.com/jbenet/go-ipfs/merkledag"
23
u "github.com/jbenet/go-ipfs/util"
24
+ "github.com/op/go-logging"
25
)
26
27
+var log = logging.MustGetLogger("ipfs")
28
+
29
// FileSystem is the readonly Ipfs Fuse Filesystem.
30
type FileSystem struct {
31
Ipfs *core.IpfsNode
@@ -50,7 +53,7 @@ func (*Root) Attr() fuse.Attr {
53
54
// Lookup performs a lookup under this node.
55
func (s *Root) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
53
- u.DOut("Root Lookup: '%s'\n", name)
56
+ log.Debug("Root Lookup: '%s'", name)
57
switch name {
58
case "mach_kernel", ".hidden", "._.":
59
// Just quiet some log noise on OS X.
@@ -68,7 +71,7 @@ func (s *Root) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
71
72
// ReadDir reads a particular directory. Disallowed for root.
73
func (*Root) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
71
- u.DOut("Read Root.\n")
74
+ log.Debug("Read Root.")
75
return nil, fuse.EPERM
76
}
77
@@ -87,16 +90,14 @@ func (s *Node) loadData() error {
90
91
// Attr returns the attributes of a given node.
92
func (s *Node) Attr() fuse.Attr {
90
- u.DOut("Node attr.\n")
93
+ log.Debug("Node attr.")
94
if s.cached == nil {
95
s.loadData()
96
}
97
switch s.cached.GetType() {
98
case mdag.PBData_Directory:
96
- u.DOut("this is a directory.\n")
99
return fuse.Attr{Mode: os.ModeDir | 0555}
100
case mdag.PBData_File, mdag.PBData_Raw:
99
- u.DOut("this is a file.\n")
101
size, _ := s.Nd.Size()
102
return fuse.Attr{
103
Mode: 0444,
@@ -111,7 +112,7 @@ func (s *Node) Attr() fuse.Attr {
112
113
// Lookup performs a lookup under this node.
114
func (s *Node) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
114
- u.DOut("Lookup '%s'\n", name)
115
+ log.Debug("Lookup '%s'", name)
116
nd, err := s.Ipfs.Resolver.ResolveLinks(s.Nd, []string{name})
117
if err != nil {
118
// todo: make this error more versatile.
@@ -123,7 +124,7 @@ func (s *Node) Lookup(name string, intr fs.Intr) (fs.Node, fuse.Error) {
124
125
// ReadDir reads the link structure as directory entries
126
func (s *Node) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
126
- u.DOut("Node ReadDir\n")
127
+ log.Debug("Node ReadDir")
128
entries := make([]fuse.Dirent, len(s.Nd.Links))
129
for i, link := range s.Nd.Links {
130
n := link.Name
@@ -193,7 +194,7 @@ func Mount(ipfs *core.IpfsNode, fpath string) error {
194
// Unmount attempts to unmount the provided FUSE mount point, forcibly
195
// if necessary.
196
func Unmount(point string) error {
196
- fmt.Printf("Unmounting %s...\n", point)
197
+ log.Info("Unmounting %s...", point)
198
199
var cmd *exec.Cmd
200
switch runtime.GOOS {
routing/dht/dht.go
+15
-15
@@ -77,7 +77,7 @@ func NewDHT(p *peer.Peer, ps peer.Peerstore, net inet.Network, sender inet.Sende
77
78
// Connect to a new peer at the given address, ping and add to the routing table
79
func (dht *IpfsDHT) Connect(ctx context.Context, npeer *peer.Peer) (*peer.Peer, error) {
80
- u.DOut("Connect to new peer: %s\n", npeer.ID.Pretty())
80
+ log.Debug("Connect to new peer: %s\n", npeer.ID.Pretty())
81
82
// TODO(jbenet,whyrusleeping)
83
//
@@ -132,7 +132,7 @@ func (dht *IpfsDHT) HandleMessage(ctx context.Context, mes msg.NetMessage) msg.N
132
dht.Update(mPeer)
133
134
// Print out diagnostic
135
- u.DOut("[peer: %s] Got message type: '%s' [from = %s]\n",
135
+ log.Debug("[peer: %s] Got message type: '%s' [from = %s]\n",
136
dht.self.ID.Pretty(),
137
Message_MessageType_name[int32(pmes.GetType())], mPeer.ID.Pretty())
138
@@ -177,7 +177,7 @@ func (dht *IpfsDHT) sendRequest(ctx context.Context, p *peer.Peer, pmes *Message
177
start := time.Now()
178
179
// Print out diagnostic
180
- u.DOut("[peer: %s] Sent message type: '%s' [to = %s]\n",
180
+ log.Debug("[peer: %s] Sent message type: '%s' [to = %s]\n",
181
dht.self.ID.Pretty(),
182
Message_MessageType_name[int32(pmes.GetType())], p.ID.Pretty())
183
@@ -224,7 +224,7 @@ func (dht *IpfsDHT) putProvider(ctx context.Context, p *peer.Peer, key string) e
224
return err
225
}
226
227
- u.DOut("[%s] putProvider: %s for %s\n", dht.self.ID.Pretty(), p.ID.Pretty(), key)
227
+ log.Debug("[%s] putProvider: %s for %s", dht.self.ID.Pretty(), p.ID.Pretty(), key)
228
if *rpmes.Key != *pmes.Key {
229
return errors.New("provider not added correctly")
230
}
@@ -240,10 +240,10 @@ func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p *peer.Peer,
240
return nil, nil, err
241
}
242
243
- u.DOut("pmes.GetValue() %v\n", pmes.GetValue())
243
+ log.Debug("pmes.GetValue() %v", pmes.GetValue())
244
if value := pmes.GetValue(); value != nil {
245
// Success! We were given the value
246
- u.DOut("getValueOrPeers: got value\n")
246
+ log.Debug("getValueOrPeers: got value")
247
return value, nil, nil
248
}
249
@@ -253,7 +253,7 @@ func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p *peer.Peer,
253
if err != nil {
254
return nil, nil, err
255
}
256
- u.DOut("getValueOrPeers: get from providers\n")
256
+ log.Debug("getValueOrPeers: get from providers")
257
return val, nil, nil
258
}
259
@@ -266,7 +266,7 @@ func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p *peer.Peer,
266
267
addr, err := ma.NewMultiaddr(pb.GetAddr())
268
if err != nil {
269
- u.PErr("%v\n", err.Error())
269
+ log.Error("%v", err.Error())
270
continue
271
}
272
@@ -281,11 +281,11 @@ func (dht *IpfsDHT) getValueOrPeers(ctx context.Context, p *peer.Peer,
281
}
282
283
if len(peers) > 0 {
284
- u.DOut("getValueOrPeers: peers\n")
284
+ u.DOut("getValueOrPeers: peers")
285
return nil, peers, nil
286
}
287
288
- u.DOut("getValueOrPeers: u.ErrNotFound\n")
288
+ log.Warning("getValueOrPeers: u.ErrNotFound")
289
return nil, nil, u.ErrNotFound
290
}
291
@@ -307,13 +307,13 @@ func (dht *IpfsDHT) getFromPeerList(ctx context.Context, key u.Key,
307
for _, pinfo := range peerlist {
308
p, err := dht.ensureConnectedToPeer(pinfo)
309
if err != nil {
310
- u.DErr("getFromPeers error: %s\n", err)
310
+ log.Error("getFromPeers error: %s", err)
311
continue
312
}
313
314
pmes, err := dht.getValueSingle(ctx, p, key, level)
315
if err != nil {
316
- u.DErr("getFromPeers error: %s\n", err)
316
+ log.Error("getFromPeers error: %s\n", err)
317
continue
318
}
319
@@ -348,7 +348,7 @@ func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
348
// Update signals to all routingTables to Update their last-seen status
349
// on the given peer.
350
func (dht *IpfsDHT) Update(p *peer.Peer) {
351
- u.DOut("updating peer: [%s] latency = %f\n", p.ID.Pretty(), p.GetLatency().Seconds())
351
+ log.Debug("updating peer: [%s] latency = %f\n", p.ID.Pretty(), p.GetLatency().Seconds())
352
removedCount := 0
353
for _, route := range dht.routingTables {
354
removed := route.Update(p)
@@ -404,7 +404,7 @@ func (dht *IpfsDHT) addProviders(key u.Key, peers []*Message_Peer) []*peer.Peer
404
continue
405
}
406
407
- u.DOut("[%s] adding provider: %s for %s", dht.self.ID.Pretty(), p, key)
407
+ log.Debug("[%s] adding provider: %s for %s", dht.self.ID.Pretty(), p, key)
408
409
// Dont add outselves to the list
410
if p.ID.Equal(dht.self.ID) {
@@ -439,7 +439,7 @@ func (dht *IpfsDHT) betterPeerToQuery(pmes *Message) *peer.Peer {
439
440
// == to self? nil
441
if closer.ID.Equal(dht.self.ID) {
442
- u.DOut("Attempted to return self! this shouldnt happen...\n")
442
+ log.Error("Attempted to return self! this shouldnt happen...")
443
return nil
444
}
445
routing/dht/routing.go
+5
-5
@@ -86,7 +86,7 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
86
return nil, err
87
}
88
89
- u.DOut("[%s] GetValue %v %v\n", dht.self.ID.Pretty(), key, result.value)
89
+ log.Debug("GetValue %v %v", key, result.value)
90
if result.value == nil {
91
return nil, u.ErrNotFound
92
}
@@ -189,7 +189,7 @@ func (dht *IpfsDHT) addPeerListAsync(k u.Key, peers []*Message_Peer, ps *peerSet
189
// FindProviders searches for peers who can provide the value for given key.
190
func (dht *IpfsDHT) FindProviders(ctx context.Context, key u.Key) ([]*peer.Peer, error) {
191
// get closest peer
192
- u.DOut("Find providers for: '%s'\n", key.Pretty())
192
+ log.Debug("Find providers for: '%s'", key.Pretty())
193
p := dht.routingTables[0].NearestPeer(kb.ConvertKey(key))
194
if p == nil {
195
return nil, nil
@@ -333,17 +333,17 @@ func (dht *IpfsDHT) findPeerMultiple(ctx context.Context, id peer.ID) (*peer.Pee
333
// Ping a peer, log the time it took
334
func (dht *IpfsDHT) Ping(ctx context.Context, p *peer.Peer) error {
335
// Thoughts: maybe this should accept an ID and do a peer lookup?
336
- u.DOut("[%s] ping %s start\n", dht.self.ID.Pretty(), p.ID.Pretty())
336
+ log.Info("ping %s start", p.ID.Pretty())
337
338
pmes := newMessage(Message_PING, "", 0)
339
_, err := dht.sendRequest(ctx, p, pmes)
340
- u.DOut("[%s] ping %s end (err = %s)\n", dht.self.ID.Pretty(), p.ID.Pretty(), err)
340
+ log.Info("ping %s end (err = %s)", p.ID.Pretty(), err)
341
return err
342
}
343
344
func (dht *IpfsDHT) getDiagnostic(ctx context.Context) ([]*diagInfo, error) {
345
346
- u.DOut("Begin Diagnostic")
346
+ log.Info("Begin Diagnostic")
347
peers := dht.routingTables[0].NearestPeers(kb.ConvertPeerID(dht.self.ID), 10)
348
var out []*diagInfo
349
util/util.go
+1
-1
@@ -14,7 +14,7 @@ import (
14
"github.com/op/go-logging"
15
)
16
17
-var format = "%{color}%{time} %{shortfile} %{level}: %{color:reset}%{message}"
17
+var format = "%{color}%{time:01-02 15:04:05.9999} %{shortfile} %{level}: %{color:reset}%{message}"
18
19
func init() {
20
backend := logging.NewLogBackend(os.Stderr, "", 0)