]> Cypherpunks.ru repositories - nncp.git/blob - src/log.go
recfile log format
[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 type LE struct {
31         K string
32         V interface{}
33 }
34 type LEs []LE
35
36 func (les LEs) Rec() string {
37         fields := make([]recfile.Field, 0, len(les)+1)
38         fields = append(fields, recfile.Field{
39                 Name: "When", Value: time.Now().UTC().Format(time.RFC3339Nano),
40         })
41         var val string
42         for _, le := range les {
43                 switch v := le.V.(type) {
44                 case int, int8, uint8, int64, uint64:
45                         val = fmt.Sprintf("%d", v)
46                 default:
47                         val = fmt.Sprintf("%s", v)
48                 }
49                 fields = append(fields, recfile.Field{Name: le.K, Value: val})
50         }
51         b := bytes.NewBuffer(make([]byte, 0, 1<<10))
52         w := recfile.NewWriter(b)
53         _, err := w.RecordStart()
54         if err != nil {
55                 panic(err)
56         }
57         _, err = w.WriteFields(fields...)
58         if err != nil {
59                 panic(err)
60         }
61         return b.String()
62 }
63
64 func (ctx *Ctx) Log(rec string) {
65         fdLock, err := os.OpenFile(
66                 ctx.LogPath+".lock",
67                 os.O_CREATE|os.O_WRONLY,
68                 os.FileMode(0666),
69         )
70         if err != nil {
71                 fmt.Fprintln(os.Stderr, "Can not open lock for log:", err)
72                 return
73         }
74         defer fdLock.Close()
75         fdLockFd := int(fdLock.Fd())
76         err = unix.Flock(fdLockFd, unix.LOCK_EX)
77         if err != nil {
78                 fmt.Fprintln(os.Stderr, "Can not acquire lock for log:", err)
79                 return
80         }
81         defer unix.Flock(fdLockFd, unix.LOCK_UN)
82         fd, err := os.OpenFile(
83                 ctx.LogPath,
84                 os.O_CREATE|os.O_WRONLY|os.O_APPEND,
85                 os.FileMode(0666),
86         )
87         if err != nil {
88                 fmt.Fprintln(os.Stderr, "Can not open log:", err)
89                 return
90         }
91         fd.WriteString(rec) // #nosec G104
92         fd.Close()          // #nosec G104
93 }
94
95 func (ctx *Ctx) LogD(who string, les LEs, msg string) {
96         if !ctx.Debug {
97                 return
98         }
99         les = append(LEs{{"Debug", true}, {"Who", who}}, les...)
100         if msg != "" {
101                 les = append(les, LE{"Msg", msg})
102         }
103         fmt.Fprint(os.Stderr, les.Rec())
104 }
105
106 func (ctx *Ctx) LogI(who string, les LEs, msg string) {
107         les = append(LEs{{"Who", who}}, les...)
108         if msg != "" {
109                 les = append(les, LE{"Msg", msg})
110         }
111         rec := les.Rec()
112         if !ctx.Quiet {
113                 fmt.Fprintln(os.Stderr, ctx.HumanizeRec(rec))
114         }
115         ctx.Log(rec)
116 }
117
118 func (ctx *Ctx) LogE(who string, les LEs, err error, msg string) {
119         les = append(LEs{{"Err", err.Error()}, {"Who", who}}, les...)
120         if msg != "" {
121                 les = append(les, LE{"Msg", msg})
122         }
123         rec := les.Rec()
124         if !ctx.Quiet {
125                 fmt.Fprintln(os.Stderr, ctx.HumanizeRec(rec))
126         }
127         ctx.Log(rec)
128 }