diff --git a/debug.go b/debug.go index dd255036cf..07f13ae4f3 100644 --- a/debug.go +++ b/debug.go @@ -2,6 +2,7 @@ package raft import ( "log" + "os" ) //------------------------------------------------------------------------------ @@ -12,6 +13,11 @@ import ( // A flag stating if debug statements should be evaluated. var Debug bool = false +var logger *log.Logger + +func init() { + logger = log.New(os.Stdout, "", log.Lmicroseconds) +} //------------------------------------------------------------------------------ // @@ -23,7 +29,7 @@ var Debug bool = false // are handled in the manner of fmt.Print. func debug(v ...interface{}) { if Debug { - log.Print(v...) + logger.Print(v...) } } @@ -31,7 +37,8 @@ func debug(v ...interface{}) { // are handled in the manner of fmt.Printf. func debugf(format string, v ...interface{}) { if Debug { - log.Printf(format, v...) + + logger.Printf(format, v...) } } @@ -39,6 +46,6 @@ func debugf(format string, v ...interface{}) { // are handled in the manner of debugln. func debugln(v ...interface{}) { if Debug { - log.Println(v...) + logger.Println(v...) } } diff --git a/server.go b/server.go index 3c9f9e737f..1667b460b1 100644 --- a/server.go +++ b/server.go @@ -216,6 +216,11 @@ func (s *Server) LastCommandName() string { return "" } +// Get the state of the server for debugging +func (s *Server) GetState() string { + return fmt.Sprintf("State: %s, Term: %v, Index: %v ", s.state, s.currentTerm, s.CommittedIndex()) +} + //-------------------------------------- // Membership //-------------------------------------- @@ -431,7 +436,7 @@ func (s *Server) commitCenter() { if commitIndex > committedIndex { debugln(indices) - debugln("[CommitCenter] Going to Commit ", commitIndex) + debugln(s.GetState(), "[CommitCenter] Going to Commit ", commitIndex) s.log.SetCommitIndex(commitIndex) debugln("[CommitCenter] Commit ", commitIndex)