]> Cypherpunks.ru repositories - nncp.git/blob - src/log.go
Operations progress
[nncp.git] / src / log.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 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         "fmt"
22         "os"
23         "sort"
24         "strings"
25         "time"
26
27         "golang.org/x/sys/unix"
28 )
29
30 type LogLevel string
31
32 type SDS map[string]interface{}
33
34 func sdFmt(who string, sds SDS) string {
35         keys := make([]string, 0, len(sds))
36         for k, _ := range sds {
37                 keys = append(keys, k)
38         }
39         sort.Strings(keys)
40         result := make([]string, 0, 1+len(keys))
41         result = append(result, "["+who)
42         for _, k := range keys {
43                 var value string
44                 switch v := sds[k].(type) {
45                 case int, int8, uint8, int64, uint64:
46                         value = fmt.Sprintf("%d", v)
47                 default:
48                         value = fmt.Sprintf("%s", v)
49                 }
50                 result = append(result, fmt.Sprintf(`%s="%s"`, k, value))
51         }
52         return strings.Join(result, " ") + "]"
53 }
54
55 func msgFmt(level LogLevel, who string, sds SDS, msg string) string {
56         result := fmt.Sprintf(
57                 "%s %s %s",
58                 level,
59                 time.Now().UTC().Format(time.RFC3339Nano),
60                 sdFmt(who, sds),
61         )
62         if len(msg) > 0 {
63                 result += " " + msg
64         }
65         return result + "\n"
66 }
67
68 func (ctx *Ctx) Log(msg string) {
69         fdLock, err := os.OpenFile(
70                 ctx.LogPath+".lock",
71                 os.O_CREATE|os.O_WRONLY,
72                 os.FileMode(0666),
73         )
74         if err != nil {
75                 fmt.Fprintln(os.Stderr, "Can not open lock for log:", err)
76                 return
77         }
78         fdLockFd := int(fdLock.Fd())
79         err = unix.Flock(fdLockFd, unix.LOCK_EX)
80         if err != nil {
81                 fmt.Fprintln(os.Stderr, "Can not acquire lock for log:", err)
82                 return
83         }
84         defer unix.Flock(fdLockFd, unix.LOCK_UN)
85         fd, err := os.OpenFile(
86                 ctx.LogPath,
87                 os.O_CREATE|os.O_WRONLY|os.O_APPEND,
88                 os.FileMode(0666),
89         )
90         if err != nil {
91                 fmt.Fprintln(os.Stderr, "Can not open log:", err)
92                 return
93         }
94         fd.WriteString(msg)
95         fd.Close()
96 }
97
98 func (ctx *Ctx) LogD(who string, sds SDS, msg string) {
99         if !ctx.Debug {
100                 return
101         }
102         fmt.Fprint(os.Stderr, msgFmt(LogLevel("D"), who, sds, msg))
103 }
104
105 func (ctx *Ctx) LogI(who string, sds SDS, msg string) {
106         msg = msgFmt(LogLevel("I"), who, sds, msg)
107         if !ctx.Quiet {
108                 fmt.Fprintln(os.Stderr, ctx.Humanize(msg))
109         }
110         ctx.Log(msg)
111 }
112
113 func (ctx *Ctx) LogE(who string, sds SDS, err error, msg string) {
114         sds["err"] = err.Error()
115         msg = msgFmt(LogLevel("E"), who, sds, msg)
116         if len(msg) > 2048 {
117                 msg = msg[:2048]
118         }
119         fmt.Fprintln(os.Stderr, ctx.Humanize(msg))
120         ctx.Log(msg)
121 }
122
123 func SdsAdd(sds, add SDS) SDS {
124         neu := SDS{}
125         for k, v := range sds {
126                 neu[k] = v
127         }
128         for k, v := range add {
129                 neu[k] = v
130         }
131         return neu
132 }