-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
70 lines (61 loc) · 1.39 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package primitives
import (
"bytes"
"encoding/json"
"fmt"
"runtime"
"strconv"
)
func Log(format string, args ...interface{}) {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "???"
line = 0
}
fmt.Printf(file+":"+strconv.Itoa(line)+" - "+format+"\n", args...)
}
func LogJSONs(format string, args ...interface{}) {
jsons := []interface{}{}
for _, v := range args {
j, _ := EncodeJSONString(v)
jsons = append(jsons, j)
}
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "???"
line = 0
}
fmt.Printf(file+":"+strconv.Itoa(line)+" - "+format+"\n", jsons...)
}
func DecodeJSON(data []byte, v interface{}) error {
err := json.Unmarshal(data, &v)
return err
}
func EncodeJSON(data interface{}) ([]byte, error) {
encoded, err := json.Marshal(data)
if err != nil {
return nil, err
}
return encoded, nil
}
func EncodeJSONString(data interface{}) (string, error) {
encoded, err := EncodeJSON(data)
if err != nil {
return "", err
}
return string(encoded), err
}
func DecodeJSONString(data string, v interface{}) error {
return DecodeJSON([]byte(data), v)
}
func EncodeJSONToBuffer(data interface{}, b *bytes.Buffer) error {
encoded, err := EncodeJSON(data)
if err != nil {
return err
}
_, err = b.Write(encoded)
return err
}