@@ -69,6 +69,8 @@ type Node struct {
6969
7070 stop chan struct {} // Channel to wait for termination notifications
7171 lock sync.RWMutex
72+
73+ log log.Logger
7274}
7375
7476// New creates a new P2P node, ready for protocol registration.
@@ -101,6 +103,9 @@ func New(conf *Config) (*Node, error) {
101103 if err != nil {
102104 return nil , err
103105 }
106+ if conf .Logger == nil {
107+ conf .Logger = log .New ()
108+ }
104109 // Note: any interaction with Config that would create/touch files
105110 // in the data directory or instance directory is delayed until Start.
106111 return & Node {
@@ -112,6 +117,7 @@ func New(conf *Config) (*Node, error) {
112117 httpEndpoint : conf .HTTPEndpoint (),
113118 wsEndpoint : conf .WSEndpoint (),
114119 eventmux : new (event.TypeMux ),
120+ log : conf .Logger ,
115121 }, nil
116122}
117123
@@ -146,6 +152,7 @@ func (n *Node) Start() error {
146152 n .serverConfig = n .config .P2P
147153 n .serverConfig .PrivateKey = n .config .NodeKey ()
148154 n .serverConfig .Name = n .config .NodeName ()
155+ n .serverConfig .Logger = n .log
149156 if n .serverConfig .StaticNodes == nil {
150157 n .serverConfig .StaticNodes = n .config .StaticNodes ()
151158 }
@@ -156,7 +163,7 @@ func (n *Node) Start() error {
156163 n .serverConfig .NodeDatabase = n .config .NodeDB ()
157164 }
158165 running := & p2p.Server {Config : n .serverConfig }
159- log .Info ("Starting peer-to-peer node" , "instance" , n .serverConfig .Name )
166+ n . log .Info ("Starting peer-to-peer node" , "instance" , n .serverConfig .Name )
160167
161168 // Otherwise copy and specialize the P2P configuration
162169 services := make (map [reflect.Type ]Service )
@@ -280,7 +287,7 @@ func (n *Node) startInProc(apis []rpc.API) error {
280287 if err := handler .RegisterName (api .Namespace , api .Service ); err != nil {
281288 return err
282289 }
283- log .Debug (fmt .Sprintf ("InProc registered %T under '%s'" , api .Service , api .Namespace ))
290+ n . log .Debug (fmt .Sprintf ("InProc registered %T under '%s'" , api .Service , api .Namespace ))
284291 }
285292 n .inprocHandler = handler
286293 return nil
@@ -306,7 +313,7 @@ func (n *Node) startIPC(apis []rpc.API) error {
306313 if err := handler .RegisterName (api .Namespace , api .Service ); err != nil {
307314 return err
308315 }
309- log .Debug (fmt .Sprintf ("IPC registered %T under '%s'" , api .Service , api .Namespace ))
316+ n . log .Debug (fmt .Sprintf ("IPC registered %T under '%s'" , api .Service , api .Namespace ))
310317 }
311318 // All APIs registered, start the IPC listener
312319 var (
@@ -317,7 +324,7 @@ func (n *Node) startIPC(apis []rpc.API) error {
317324 return err
318325 }
319326 go func () {
320- log .Info (fmt .Sprintf ("IPC endpoint opened: %s" , n .ipcEndpoint ))
327+ n . log .Info (fmt .Sprintf ("IPC endpoint opened: %s" , n .ipcEndpoint ))
321328
322329 for {
323330 conn , err := listener .Accept ()
@@ -330,7 +337,7 @@ func (n *Node) startIPC(apis []rpc.API) error {
330337 return
331338 }
332339 // Not closed, just some error; report and continue
333- log .Error (fmt .Sprintf ("IPC accept failed: %v" , err ))
340+ n . log .Error (fmt .Sprintf ("IPC accept failed: %v" , err ))
334341 continue
335342 }
336343 go handler .ServeCodec (rpc .NewJSONCodec (conn ), rpc .OptionMethodInvocation | rpc .OptionSubscriptions )
@@ -349,7 +356,7 @@ func (n *Node) stopIPC() {
349356 n .ipcListener .Close ()
350357 n .ipcListener = nil
351358
352- log .Info (fmt .Sprintf ("IPC endpoint closed: %s" , n .ipcEndpoint ))
359+ n . log .Info (fmt .Sprintf ("IPC endpoint closed: %s" , n .ipcEndpoint ))
353360 }
354361 if n .ipcHandler != nil {
355362 n .ipcHandler .Stop ()
@@ -375,7 +382,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
375382 if err := handler .RegisterName (api .Namespace , api .Service ); err != nil {
376383 return err
377384 }
378- log .Debug (fmt .Sprintf ("HTTP registered %T under '%s'" , api .Service , api .Namespace ))
385+ n . log .Debug (fmt .Sprintf ("HTTP registered %T under '%s'" , api .Service , api .Namespace ))
379386 }
380387 }
381388 // All APIs registered, start the HTTP listener
@@ -387,7 +394,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
387394 return err
388395 }
389396 go rpc .NewHTTPServer (cors , handler ).Serve (listener )
390- log .Info (fmt .Sprintf ("HTTP endpoint opened: http://%s" , endpoint ))
397+ n . log .Info (fmt .Sprintf ("HTTP endpoint opened: http://%s" , endpoint ))
391398
392399 // All listeners booted successfully
393400 n .httpEndpoint = endpoint
@@ -403,7 +410,7 @@ func (n *Node) stopHTTP() {
403410 n .httpListener .Close ()
404411 n .httpListener = nil
405412
406- log .Info (fmt .Sprintf ("HTTP endpoint closed: http://%s" , n .httpEndpoint ))
413+ n . log .Info (fmt .Sprintf ("HTTP endpoint closed: http://%s" , n .httpEndpoint ))
407414 }
408415 if n .httpHandler != nil {
409416 n .httpHandler .Stop ()
@@ -429,7 +436,7 @@ func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrig
429436 if err := handler .RegisterName (api .Namespace , api .Service ); err != nil {
430437 return err
431438 }
432- log .Debug (fmt .Sprintf ("WebSocket registered %T under '%s'" , api .Service , api .Namespace ))
439+ n . log .Debug (fmt .Sprintf ("WebSocket registered %T under '%s'" , api .Service , api .Namespace ))
433440 }
434441 }
435442 // All APIs registered, start the HTTP listener
@@ -441,7 +448,7 @@ func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrig
441448 return err
442449 }
443450 go rpc .NewWSServer (wsOrigins , handler ).Serve (listener )
444- log .Info (fmt .Sprintf ("WebSocket endpoint opened: ws://%s" , listener .Addr ()))
451+ n . log .Info (fmt .Sprintf ("WebSocket endpoint opened: ws://%s" , listener .Addr ()))
445452
446453 // All listeners booted successfully
447454 n .wsEndpoint = endpoint
@@ -457,7 +464,7 @@ func (n *Node) stopWS() {
457464 n .wsListener .Close ()
458465 n .wsListener = nil
459466
460- log .Info (fmt .Sprintf ("WebSocket endpoint closed: ws://%s" , n .wsEndpoint ))
467+ n . log .Info (fmt .Sprintf ("WebSocket endpoint closed: ws://%s" , n .wsEndpoint ))
461468 }
462469 if n .wsHandler != nil {
463470 n .wsHandler .Stop ()
@@ -496,7 +503,7 @@ func (n *Node) Stop() error {
496503 // Release instance directory lock.
497504 if n .instanceDirLock != nil {
498505 if err := n .instanceDirLock .Release (); err != nil {
499- log .Error ("Can't release datadir lock" , "err" , err )
506+ n . log .Error ("Can't release datadir lock" , "err" , err )
500507 }
501508 n .instanceDirLock = nil
502509 }
0 commit comments