Merge changes I670144f4,I7144cd42

* changes:
  Silence error during microfactory trace import
  Synchronize file rotation
This commit is contained in:
Treehugger Robot 2017-10-18 23:51:45 +00:00 committed by Gerrit Code Review
commit e87ae20e25
3 changed files with 15 additions and 5 deletions

View file

@ -38,6 +38,7 @@ import (
"path/filepath"
"strconv"
"sync"
"syscall"
)
type Logger interface {
@ -93,10 +94,19 @@ func fileRotation(from, baseName, ext string, cur, max int) error {
// existing files to <filename>.#.<ext>, keeping up to maxCount files.
// <filename>.1.<ext> is the most recent backup, <filename>.2.<ext> is the
// second most recent backup, etc.
//
// TODO: This function is not guaranteed to be atomic, if there are multiple
// users attempting to do the same operation, the result is undefined.
func CreateFileWithRotation(filename string, maxCount int) (*os.File, error) {
lockFileName := filepath.Join(filepath.Dir(filename), ".lock_"+filepath.Base(filename))
lockFile, err := os.OpenFile(lockFileName, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return nil, err
}
defer lockFile.Close()
err = syscall.Flock(int(lockFile.Fd()), syscall.LOCK_EX)
if err != nil {
return nil, err
}
if _, err := os.Lstat(filename); err == nil {
ext := filepath.Ext(filename)
basename := filename[:len(filename)-len(ext)]

View file

@ -67,7 +67,7 @@ func TestCreateFileWithRotation(t *testing.T) {
t.Fatalf("Failed to read dir: %v", err)
}
sort.Strings(names)
expected := []string{"build.1.log", "build.2.log", "build.3.log", "build.log"}
expected := []string{".lock_build.log", "build.1.log", "build.2.log", "build.3.log", "build.log"}
if !reflect.DeepEqual(names, expected) {
t.Errorf("File list does not match.")
t.Errorf(" got: %v", names)

View file

@ -28,7 +28,7 @@ func (t *tracerImpl) ImportMicrofactoryLog(filename string) {
f, err := os.Open(filename)
if err != nil {
t.log.Println("Error opening microfactory trace:", err)
t.log.Verboseln("Error opening microfactory trace:", err)
return
}
defer f.Close()