blob: c672489a7dbcdf15d244f94100fea3693580bd70 [file] [log] [blame]
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package maintner
import (
"bytes"
"compress/gzip"
"fmt"
"os"
"path/filepath"
"time"
"github.com/golang/protobuf/proto"
"golang.org/x/build/maintner/maintpb"
)
// A MutationLogger logs mutations.
type MutationLogger interface {
Log(*maintpb.Mutation) error
}
// DiskMutationLogger logs mutations to disk.
type DiskMutationLogger struct {
directory string
}
// NewDiskMutationLogger creates a new DiskMutationLogger, which will create
// mutations in the given directory.
func NewDiskMutationLogger(directory string) *DiskMutationLogger {
return &DiskMutationLogger{directory: directory}
}
func (d *DiskMutationLogger) filename() string {
now := time.Now().UTC()
return filepath.Join(d.directory, fmt.Sprintf("maintner-%s.proto.gz", now.Format("2006-01-02")))
}
// Log will write m to disk. If a mutation file does not exist for the current
// day, it will be created.
func (d *DiskMutationLogger) Log(m *maintpb.Mutation) error {
f, err := os.OpenFile(d.filename(), os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return err
}
data, err := proto.Marshal(m)
if err != nil {
return err
}
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
if _, err := zw.Write(data); err != nil {
return err
}
if err := zw.Close(); err != nil {
return err
}
if _, err := f.Write(buf.Bytes()); err != nil {
return err
}
return f.Close()
}