]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/log.go
Fix tossing of self/rx when nncp-toss -cycle
[nncp.git] / src / log.go
index 5c98bd7e14916427c236553fea506f39f9668d61..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{}
@@ -43,6 +51,8 @@ func (les LEs) Rec() string {
                switch v := le.V.(type) {
                case int, int8, uint8, int64, uint64:
                        val = fmt.Sprintf("%d", v)
+               case bool:
+                       val = fmt.Sprintf("%v", v)
                default:
                        val = fmt.Sprintf("%s", v)
                }
@@ -62,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,
@@ -88,26 +104,22 @@ 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 string) {
+func (ctx *Ctx) LogD(who string, les LEs, msg func(LEs) string) {
        if !ctx.Debug {
                return
        }
        les = append(LEs{{"Debug", true}, {"Who", who}}, les...)
-       if msg != "" {
-               les = append(les, LE{"Msg", msg})
-       }
+       les = append(les, LE{"Msg", msg(les)})
        fmt.Fprint(os.Stderr, les.Rec())
 }
 
-func (ctx *Ctx) LogI(who string, les LEs, msg string) {
+func (ctx *Ctx) LogI(who string, les LEs, msg func(LEs) string) {
        les = append(LEs{{"Who", who}}, les...)
-       if msg != "" {
-               les = append(les, LE{"Msg", msg})
-       }
+       les = append(les, LE{"Msg", msg(les)})
        rec := les.Rec()
        if !ctx.Quiet {
                fmt.Fprintln(os.Stderr, ctx.HumanizeRec(rec))
@@ -115,11 +127,9 @@ func (ctx *Ctx) LogI(who string, les LEs, msg string) {
        ctx.Log(rec)
 }
 
-func (ctx *Ctx) LogE(who string, les LEs, err error, msg string) {
+func (ctx *Ctx) LogE(who string, les LEs, err error, msg func(LEs) string) {
        les = append(LEs{{"Err", err.Error()}, {"Who", who}}, les...)
-       if msg != "" {
-               les = append(les, LE{"Msg", msg})
-       }
+       les = append(les, LE{"Msg", msg(les)})
        rec := les.Rec()
        if !ctx.Quiet {
                fmt.Fprintln(os.Stderr, ctx.HumanizeRec(rec))