2 NNCP -- Node-to-Node CoPy
3 Copyright (C) 2016-2017 Sergey Matveev <stargrave@stargrave.org>
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, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 "golang.org/x/sys/unix"
33 type SDS map[string]interface{}
35 func sdFmt(who string, sds SDS) string {
36 keys := make([]string, 0, len(sds))
37 for k, _ := range sds {
38 keys = append(keys, k)
41 result := make([]string, 0, 1+len(keys))
42 result = append(result, "["+who)
43 for _, k := range keys {
44 result = append(result, fmt.Sprintf(`%s="%s"`, k, sds[k]))
46 return strings.Join(result, " ") + "]"
49 func msgFmt(level LogLevel, who string, sds SDS, msg string) string {
50 result := fmt.Sprintf(
53 time.Now().UTC().Format(time.RFC3339Nano),
62 func (ctx *Ctx) Log(msg string) {
63 fdLock, err := os.OpenFile(
65 os.O_CREATE|os.O_WRONLY,
69 fmt.Fprintln(os.Stderr, "Can not open lock for log:", err)
72 fdLockFd := int(fdLock.Fd())
73 err = unix.Flock(fdLockFd, unix.LOCK_EX)
75 fmt.Fprintln(os.Stderr, "Can not acquire lock for log:", err)
78 defer unix.Flock(fdLockFd, unix.LOCK_UN)
79 fd, err := os.OpenFile(
81 os.O_CREATE|os.O_WRONLY|os.O_APPEND,
85 fmt.Fprintln(os.Stderr, "Can not open log:", err)
92 func (ctx *Ctx) LogD(who string, sds SDS, msg string) {
96 fmt.Fprint(os.Stderr, msgFmt(LogLevel("D"), who, sds, msg))
99 func (ctx *Ctx) LogI(who string, sds SDS, msg string) {
100 msg = msgFmt(LogLevel("I"), who, sds, msg)
101 fmt.Fprintln(os.Stderr, ctx.Humanize(msg))
105 func (ctx *Ctx) LogP(who string, sds SDS, msg string) {
106 fmt.Fprintln(os.Stderr, ctx.Humanize(msgFmt(LogLevel("P"), who, sds, msg)))
109 func (ctx *Ctx) LogE(who string, sds SDS, msg string) {
110 msg = msgFmt(LogLevel("E"), who, sds, msg)
111 fmt.Fprintln(os.Stderr, ctx.Humanize(msg))
115 func SdsAdd(sds, add SDS) SDS {
117 for k, v := range sds {
120 for k, v := range add {