]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/log.go
go fmt
[nncp.git] / src / log.go
index 7bb59a3b97cf798e5c35c01d0c76c19154445ba6..a2e72b238f61e1a570e194d91edca8c58263c80b 100644 (file)
@@ -21,12 +21,20 @@ import (
        "bytes"
        "fmt"
        "os"
+       "sync"
        "time"
 
        "go.cypherpunks.ru/recfile"
        "golang.org/x/sys/unix"
 )
 
+const LogFdPrefix = "FD:"
+
+var (
+       LogFd     *os.File
+       LogFdLock sync.Mutex
+)
+
 type LE struct {
        K string
        V interface{}
@@ -64,6 +72,12 @@ func (les LEs) Rec() string {
 }
 
 func (ctx *Ctx) Log(rec string) {
+       if LogFd != nil {
+               LogFdLock.Lock()
+               LogFd.WriteString(rec)
+               LogFdLock.Unlock()
+               return
+       }
        fdLock, err := os.OpenFile(
                ctx.LogPath+".lock",
                os.O_CREATE|os.O_WRONLY,
@@ -90,8 +104,8 @@ func (ctx *Ctx) Log(rec string) {
                fmt.Fprintln(os.Stderr, "Can not open log:", err)
                return
        }
-       fd.WriteString(rec) // #nosec G104
-       fd.Close()          // #nosec G104
+       fd.WriteString(rec)
+       fd.Close()
 }
 
 func (ctx *Ctx) LogD(who string, les LEs, msg func(LEs) string) {