Skip to content
Snippets Groups Projects
Commit ff5374de authored by Manish R Jain's avatar Manish R Jain
Browse files

Log write, rotate and reload works.

parent 74cabf90
No related branches found
No related tags found
No related merge requests found
......@@ -152,22 +152,6 @@ func (l *Logger) handleFile(path string, info os.FileInfo, err error) error {
// Handle if we find the current log file.
if tstring == "current" {
var err error
l.size = info.Size()
l.curFile, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, os.FileMode(0644))
if err != nil {
glog.WithError(err).Fatal("Unable to open file in write mode.")
}
/*
ret, err := l.curFile.Seek(info.Size(), 0)
if err != nil || ret != info.Size() {
glog.WithError(err).Fatal("Unable to seek to end of file.")
}
*/
l.lastLogTs, err = lastTimestamp(path)
if err != nil {
glog.WithError(err).Fatal("Unable to read last log timestamp.")
}
return nil
}
......@@ -186,6 +170,28 @@ func (l *Logger) Init() {
l.Lock()
defer l.Unlock()
{
// First check if we have a current file.
path := filepath.Join(l.dir, fmt.Sprintf("%s-current.log", l.filePrefix))
fi, err := os.Stat(path)
if err == nil {
// we have the file. Derive information for counters.
l.size = fi.Size()
l.logsSinceLastSync = 0
l.lastLogTs, err = lastTimestamp(path)
if err != nil {
glog.WithError(err).Fatal("Unable to read last log timestamp.")
}
// Open file for append.
l.curFile, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY,
os.FileMode(0644))
if err != nil {
glog.WithError(err).Fatal("Unable to open current file in append mode.")
}
}
}
if err := filepath.Walk(l.dir, l.handleFile); err != nil {
glog.WithError(err).Fatal("While walking over directory")
}
......
......@@ -129,29 +129,31 @@ func TestRotatingLog(t *testing.T) {
t.Errorf("Expected ts: %v. Got: %v", ts+int64(8), l.lastLogTs)
}
l.Close()
l = nil // Important to avoid re-use later.
// Now, let's test a re-init of logger.
nl := NewLogger(dir, "dgraph", 1024)
nl.Init()
defer nl.Close()
if len(nl.list) != 4 {
t.Errorf("Expected 4 files. Got: %v", len(nl.list))
}
if nl.size != 416 {
t.Errorf("Expected size 416. Got: %v", nl.size)
}
if err := l.AddLog(ts+int64(100), 0, data); err != nil {
if err := nl.AddLog(ts+int64(100), 0, data); err != nil {
t.Error(err)
return
}
if nl.size != 832 {
t.Errorf("Expected size 832. Got: %v", nl.size)
}
if err := l.AddLog(ts+int64(113), 0, data); err != nil {
if err := nl.AddLog(ts+int64(113), 0, data); err != nil {
t.Error(err)
return
}
if len(nl.list) != 5 {
t.Errorf("Expected 4 files. Got: %v", len(nl.list))
t.Errorf("Expected 5 files. Got: %v", len(nl.list))
}
if nl.list[4].endTs != ts+int64(100) {
t.Errorf("Expected ts: %v. Got: %v", ts+int64(100), nl.list[4].endTs)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment