]> Cypherpunks.ru repositories - nncp.git/blob - src/log.go
Logging to opened file descriptor
[nncp.git] / src / log.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package nncp
19
20 import (
21         "bytes"
22         "fmt"
23         "os"
24         "time"
25
26         "go.cypherpunks.ru/recfile"
27         "golang.org/x/sys/unix"
28 )
29
30 const LogFdPrefix = "FD:"
31
32 var LogFd *os.File
33
34 type LE struct {
35         K string
36         V interface{}
37 }
38 type LEs []LE
39
40 func (les LEs) Rec() string {
41         fields := make([]recfile.Field, 0, len(les)+1)
42         fields = append(fields, recfile.Field{
43                 Name: "When", Value: time.Now().UTC().Format(time.RFC3339Nano),
44         })
45         var val string
46         for _, le := range les {
47                 switch v := le.V.(type) {
48                 case int, int8, uint8, int64, uint64:
49                         val = fmt.Sprintf("%d", v)
50                 case bool:
51                         val = fmt.Sprintf("%v", v)
52                 default:
53                         val = fmt.Sprintf("%s", v)
54                 }
55                 fields = append(fields, recfile.Field{Name: le.K, Value: val})
56         }
57         b := bytes.NewBuffer(make([]byte, 0, 1<<10))
58         w := recfile.NewWriter(b)
59         _, err := w.RecordStart()
60         if err != nil {
61                 panic(err)
62         }
63         _, err = w.WriteFields(fields...)
64         if err != nil {
65                 panic(err)
66         }
67         return b.String()
68 }
69
70 func (ctx *Ctx) Log(rec string) {
71         if LogFd != nil {
72                 LogFd.WriteString(rec)
73                 return
74         }
75         fdLock, err := os.OpenFile(
76                 ctx.LogPath+".lock",
77                 os.O_CREATE|os.O_WRONLY,
78                 os.FileMode(0666),
79         )
80         if err != nil {
81                 fmt.Fprintln(os.Stderr, "Can not open lock for log:", err)
82                 return
83         }
84         defer fdLock.Close()
85         fdLockFd := int(fdLock.Fd())
86         err = unix.Flock(fdLockFd, unix.LOCK_EX)
87         if err != nil {
88                 fmt.Fprintln(os.Stderr, "Can not acquire lock for log:", err)
89                 return
90         }
91         defer unix.Flock(fdLockFd, unix.LOCK_UN)
92         fd, err := os.OpenFile(
93                 ctx.LogPath,
94                 os.O_CREATE|os.O_WRONLY|os.O_APPEND,
95                 os.FileMode(0666),
96         )
97         if err != nil {
98                 fmt.Fprintln(os.Stderr, "Can not open log:", err)
99                 return
100         }
101         fd.WriteString(rec) // #nosec G104
102         fd.Close()          // #nosec G104
103 }
104
105 func (ctx *Ctx) LogD(who string, les LEs, msg func(LEs) string) {
106         if !ctx.Debug {
107                 return
108         }
109         les = append(LEs{{"Debug", true}, {"Who", who}}, les...)
110         les = append(les, LE{"Msg", msg(les)})
111         fmt.Fprint(os.Stderr, les.Rec())
112 }
113
114 func (ctx *Ctx) LogI(who string, les LEs, msg func(LEs) string) {
115         les = append(LEs{{"Who", who}}, les...)
116         les = append(les, LE{"Msg", msg(les)})
117         rec := les.Rec()
118         if !ctx.Quiet {
119                 fmt.Fprintln(os.Stderr, ctx.HumanizeRec(rec))
120         }
121         ctx.Log(rec)
122 }
123
124 func (ctx *Ctx) LogE(who string, les LEs, err error, msg func(LEs) string) {
125         les = append(LEs{{"Err", err.Error()}, {"Who", who}}, les...)
126         les = append(les, LE{"Msg", msg(les)})
127         rec := les.Rec()
128         if !ctx.Quiet {
129                 fmt.Fprintln(os.Stderr, ctx.HumanizeRec(rec))
130         }
131         ctx.Log(rec)
132 }