town/external/lockingwriter/lockingwriter.go

65 lines
1.1 KiB
Go
Raw Normal View History

2023-11-01 02:44:22 +00:00
package lockingwriter
import (
"os"
"time"
"github.com/gofrs/flock"
)
// for now this package defines a writer for use with log.New(). It it intended to be used from the external ssh applications. This logger uses a file lock to allow all the various ssh applications to log to the same file.
2023-11-01 03:42:43 +00:00
// the correct way to do this is to send log events to a daemon
// but i'm trying to cut a corner
2023-11-01 02:44:22 +00:00
type LockingWriter struct {
path string
}
2023-11-01 03:25:26 +00:00
const (
fp = "/town/var/log/external.log"
lp = "/town/var/log/log.lock"
)
2023-11-01 02:44:22 +00:00
2023-11-01 03:25:26 +00:00
func New() *LockingWriter {
f, err := os.OpenFile(fp, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0660)
2023-11-01 03:42:43 +00:00
if err != nil && !os.IsExist(err) {
2023-11-01 02:44:22 +00:00
panic(err)
}
2023-11-01 03:42:43 +00:00
f.Close()
2023-11-01 02:44:22 +00:00
return &LockingWriter{
path: fp,
}
}
func (l *LockingWriter) Write(p []byte) (n int, err error) {
2023-11-01 03:25:26 +00:00
fl := flock.New(lp)
2023-11-01 02:44:22 +00:00
var locked bool
for !locked {
locked, err = fl.TryLock()
if err != nil {
return
}
time.Sleep(time.Second)
}
2023-11-01 03:25:26 +00:00
2023-11-01 02:44:22 +00:00
var f *os.File
2023-11-01 03:42:43 +00:00
f, err = os.OpenFile(l.path, os.O_APPEND|os.O_WRONLY, 0660)
2023-11-01 02:44:22 +00:00
if err != nil {
return
}
defer f.Close()
defer fl.Unlock()
2023-11-01 03:42:43 +00:00
n, err = f.Write(p)
if err != nil {
panic(err)
}
return
//return f.Write(p)
2023-11-01 02:44:22 +00:00
}