Merge changes I670144f4,I7144cd42
* changes: Silence error during microfactory trace import Synchronize file rotation
This commit is contained in:
@@ -38,6 +38,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Logger interface {
|
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.
|
// existing files to <filename>.#.<ext>, keeping up to maxCount files.
|
||||||
// <filename>.1.<ext> is the most recent backup, <filename>.2.<ext> is the
|
// <filename>.1.<ext> is the most recent backup, <filename>.2.<ext> is the
|
||||||
// second most recent backup, etc.
|
// 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) {
|
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 {
|
if _, err := os.Lstat(filename); err == nil {
|
||||||
ext := filepath.Ext(filename)
|
ext := filepath.Ext(filename)
|
||||||
basename := filename[:len(filename)-len(ext)]
|
basename := filename[:len(filename)-len(ext)]
|
||||||
|
@@ -67,7 +67,7 @@ func TestCreateFileWithRotation(t *testing.T) {
|
|||||||
t.Fatalf("Failed to read dir: %v", err)
|
t.Fatalf("Failed to read dir: %v", err)
|
||||||
}
|
}
|
||||||
sort.Strings(names)
|
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) {
|
if !reflect.DeepEqual(names, expected) {
|
||||||
t.Errorf("File list does not match.")
|
t.Errorf("File list does not match.")
|
||||||
t.Errorf(" got: %v", names)
|
t.Errorf(" got: %v", names)
|
||||||
|
@@ -28,7 +28,7 @@ func (t *tracerImpl) ImportMicrofactoryLog(filename string) {
|
|||||||
|
|
||||||
f, err := os.Open(filename)
|
f, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.log.Println("Error opening microfactory trace:", err)
|
t.log.Verboseln("Error opening microfactory trace:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
Reference in New Issue
Block a user