diff --git a/log.go b/log.go index 90abcb73b5..c0fa778772 100644 --- a/log.go +++ b/log.go @@ -73,6 +73,18 @@ func (l *Log) IsEmpty() bool { return (len(l.entries) == 0) } +// The name of the last command in the log. +func (l *Log) LastCommandName() string { + l.mutex.Lock() + defer l.mutex.Unlock() + if len(l.entries) > 0 { + if command := l.entries[len(l.entries)-1].Command; command != nil { + return command.CommandName() + } + } + return "" +} + //-------------------------------------- // Log Terms //-------------------------------------- diff --git a/server.go b/server.go index a307819e85..7c95e8dc92 100644 --- a/server.go +++ b/server.go @@ -150,17 +150,31 @@ func (s *Server) VotedFor() string { // Retrieves whether the server's log has no entries. func (s *Server) IsLogEmpty() bool { + s.mutex.Lock() + defer s.mutex.Unlock() return s.log.IsEmpty() } // A list of all the log entries. This should only be used for debugging purposes. func (s *Server) LogEntries() []*LogEntry { + s.mutex.Lock() + defer s.mutex.Unlock() if s.log != nil { return s.log.entries } return nil } +// A reference to the command name of the last entry. +func (s *Server) LastCommandName() string { + s.mutex.Lock() + defer s.mutex.Unlock() + if s.log != nil { + return s.log.LastCommandName() + } + return "" +} + //-------------------------------------- // Membership //--------------------------------------