func (l *Logic) loadLogEntry(filename string) { data, err := ioutil.ReadFile(filename) if err != nil { glog.Info(err) return } buf := bytes.NewBuffer(data) reader := bufio.NewReader(buf) var line string for { data, prefix, err := reader.ReadLine() if err != nil { break } line = line + string(data) if !prefix { glog.Info(line) // lines = append(lines, line) entry := comm.Entry{} err := entry.UnSerialise(line) if err != nil { glog.Error("failed to un serialise log entry:", err) continue } line = "" if entry.LogIndex < len(l.logEntries) { l.logEntries[entry.LogIndex] = entry } else { l.logEntries = append(l.logEntries, entry) } } } }
func (l *Logic) entryToDisk(entry comm.Entry) { glog.Info("log to disk") if l.file == nil { f, err := os.OpenFile("logentry.data", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) if err != nil { glog.Error(err) return } l.file = f } data, err := entry.Serialise() if err != nil { glog.Info("failed to serialise of entry:", entry) return } glog.Info(string(data)) l.file.WriteString(data + "\n") }