Permalink
Browse files

add logging.Severe, clean up gateway TODOs

1 parent cdbe2b8 commit 0fc47a001c98cf2d79d03d554653426fd96b9332 @DavidVorick DavidVorick committed Aug 26, 2016
Showing with 71 additions and 19 deletions.
  1. +18 −3 build/critical.go
  2. +29 −0 build/critical_test.go
  3. +2 −6 modules/gateway/peers.go
  4. +22 −10 persist/log.go
View
@@ -6,13 +6,28 @@ import (
"runtime/debug"
)
-// Critical will print a message to os.Stderr unless DEBUG has been set, in
-// which case panic will be called instead.
+// Critical should be called if a sanity check has failed, indicating developer
+// error. Severe is called with an extended message guiding the user to the
+// issue tracker on Github. If the program does not panic, the call stack for
+// the running goroutine is printed to help determine the error.
func Critical(v ...interface{}) {
s := "Critical error: " + fmt.Sprintln(v...) + "Please submit a bug report here: https://github.com/NebulousLabs/Sia/issues\n"
+ os.Stderr.WriteString(s)
if DEBUG {
panic(s)
}
- os.Stderr.WriteString(s)
debug.PrintStack()
}
+
+// Severe will print a message to os.Stderr unless DEBUG has been set, in which
+// case panic will be called instead. Severe should be called in situations
+// which indicate significant problems for the user (such as disk failure or
+// random number generation failure), but where crashing is not strictly
+// required to preserve integrity.
+func Severe(v ...interface{}) {
+ s := "Severe error: " + fmt.Sprintln(v...)
+ os.Stderr.WriteString(s)
+ if DEBUG {
+ panic(s)
+ }
+}
@@ -32,3 +32,32 @@ func TestCriticalVariadic(t *testing.T) {
}()
Critical(k0, k1, k2, k3)
}
+
+// TestSevere checks that a panic is called in debug mode.
+func TestSevere(t *testing.T) {
+ k0 := "severe test killstring"
+ killstring := "Severe error: severe test killstring\n"
+ defer func() {
+ r := recover()
+ if r != killstring {
+ t.Error("panic did not work:", r, killstring)
+ }
+ }()
+ Severe(k0)
+}
+
+// TestSevereVariadic checks that a panic is called in debug mode.
+func TestSevereVariadic(t *testing.T) {
+ k0 := "variadic"
+ k1 := "severe"
+ k2 := "test"
+ k3 := "killstring"
+ killstring := "Severe error: variadic severe test killstring\n"
+ defer func() {
+ r := recover()
+ if r != killstring {
+ t.Error("panic did not work:", r, killstring)
+ }
+ }()
+ Severe(k0, k1, k2, k3)
+}
@@ -79,9 +79,7 @@ func (g *Gateway) randomOutboundPeer() (modules.NetAddress, error) {
// Of the remaining options, select one at random.
r, err := crypto.RandIntn(len(addrs))
if err != nil {
- // TODO: This is not a developer bug, and 'Critical' is for
- // developer bugs.
- g.log.Critical(err)
+ g.log.Severe("Random number generation failure:", err)
}
return addrs[r], nil
}
@@ -242,9 +240,7 @@ func (g *Gateway) acceptPeer(p *peer) {
// Of the remaining options, select one at random.
r, err := crypto.RandIntn(len(addrs))
if err != nil {
- // TODO: This is not a developer bug, and 'Critical' is for
- // developer bugs.
- g.log.Critical(err)
+ g.log.Severe("random number generation failure:", err)
}
kick := addrs[r]
g.peers[kick].sess.Close()
View
@@ -18,8 +18,20 @@ type Logger struct {
w io.Writer
}
-// Critical logs a message with a CRITICAL prefix. If debug mode is enabled,
-// it will also write the message to os.Stderr and panic.
+// Close logs a shutdown message and closes the Logger's underlying io.Writer,
+// if it is also an io.Closer.
+func (l *Logger) Close() error {
+ l.Output(2, "SHUTDOWN: Logging has terminated.")
+ if c, ok := l.w.(io.Closer); ok {
+ return c.Close()
+ }
+ return nil
+}
+
+// Critical logs a message with a CRITICAL prefix that guides the user to the
+// Sia github tracker. If debug mode is enabled, it will also write the message
+// to os.Stderr and panic. Critical should only be called if there has been a
+// developer error, otherwise Severe should be called.
func (l *Logger) Critical(v ...interface{}) {
l.Output(2, "CRITICAL: "+fmt.Sprintln(v...))
build.Critical(v...)
@@ -49,14 +61,14 @@ func (l *Logger) Debugln(v ...interface{}) {
}
}
-// Close logs a shutdown message and closes the Logger's underlying io.Writer,
-// if it is also an io.Closer.
-func (l *Logger) Close() error {
- l.Output(2, "SHUTDOWN: Logging has terminated.")
- if c, ok := l.w.(io.Closer); ok {
- return c.Close()
- }
- return nil
+// Severe logs a message with a SEVERE prefix. If debug mode is enabled, it
+// will also write the message to os.Stderr and panic. Severe should be called
+// if there is a severe problem with the user's machine or setup that should be
+// addressed ASAP but does not necessarily require that the machine crash or
+// exit.
+func (l *Logger) Severe(v ...interface{}) {
+ l.Output(2, "SEVERE: "+fmt.Sprintln(v...))
+ build.Severe(v...)
}
// NewLogger returns a logger that can be closed. Calls should not be made to

0 comments on commit 0fc47a0

Please sign in to comment.