broke up log file + logging env
IPFS_LOGGING now takes a logging.Level string (no more "all") cc @whyrusleeping
Juan Batiz-Benet committed
Oct 11, 2014 at 04:44 UTC
cb15a437ea29d94763def15fce2fca67d86dd141
3 files changed
+138
-121
util/key.go
new
+73
@@ -0,0 +1,73 @@
1
+package util
2
+
3
+import (
4
+ ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
5
+ b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
6
+ mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
7
+)
8
+
9
+// Key is a string representation of multihash for use with maps.
10
+type Key string
11
+
12
+// String is utililty function for printing out keys as strings (Pretty).
13
+func (k Key) String() string {
14
+ return k.Pretty()
15
+}
16
+
17
+// Pretty returns Key in a b58 encoded string
18
+func (k Key) Pretty() string {
19
+ return b58.Encode([]byte(k))
20
+}
21
+
22
+// DsKey returns a Datastore key
23
+func (k Key) DsKey() ds.Key {
24
+ return ds.NewKey(string(k))
25
+}
26
+
27
+// KeyFromDsKey returns a Datastore key
28
+func KeyFromDsKey(dsk ds.Key) Key {
29
+ return Key(dsk.BaseNamespace())
30
+}
31
+
32
+// DsKeyB58Encode returns a B58 encoded Datastore key
33
+// TODO: this is hacky because it encodes every path component. some
34
+// path components may be proper strings already...
35
+func DsKeyB58Encode(dsk ds.Key) ds.Key {
36
+ k := ds.NewKey("/")
37
+ for _, n := range dsk.Namespaces() {
38
+ k = k.Child(b58.Encode([]byte(n)))
39
+ }
40
+ return k
41
+}
42
+
43
+// DsKeyB58Decode returns a b58 decoded Datastore key
44
+// TODO: this is hacky because it encodes every path component. some
45
+// path components may be proper strings already...
46
+func DsKeyB58Decode(dsk ds.Key) ds.Key {
47
+ k := ds.NewKey("/")
48
+ for _, n := range dsk.Namespaces() {
49
+ k = k.Child(string(b58.Decode(n)))
50
+ }
51
+ return k
52
+}
53
+
54
+// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
55
+func Hash(data []byte) mh.Multihash {
56
+ h, err := mh.Sum(data, mh.SHA2_256, -1)
57
+ if err != nil {
58
+ // this error can be safely ignored (panic) because multihash only fails
59
+ // from the selection of hash function. If the fn + length are valid, it
60
+ // won't error.
61
+ panic("multihash failed to hash using SHA2_256.")
62
+ }
63
+ return h
64
+}
65
+
66
+// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
67
+func IsValidHash(s string) bool {
68
+ out := b58.Decode(s)
69
+ if out == nil || len(out) == 0 {
70
+ return false
71
+ }
72
+ return true
73
+}
util/log.go
new
+65
@@ -0,0 +1,65 @@
1
+package util
2
+
3
+import (
4
+ "fmt"
5
+ "os"
6
+
7
+ logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
8
+)
9
+
10
+func init() {
11
+ SetupLogging()
12
+}
13
+
14
+// LogFormat is the format used for our logger.
15
+var LogFormat = "%{color}%{time:2006-01-02 15:04:05.999999} %{shortfile} %{level}: %{color:reset}%{message}"
16
+
17
+// loggers is the set of loggers in the system
18
+var loggers = map[string]*logging.Logger{}
19
+
20
+// PErr is a shorthand printing function to output to Stderr.
21
+func PErr(format string, a ...interface{}) {
22
+ fmt.Fprintf(os.Stderr, format, a...)
23
+}
24
+
25
+// POut is a shorthand printing function to output to Stdout.
26
+func POut(format string, a ...interface{}) {
27
+ fmt.Fprintf(os.Stdout, format, a...)
28
+}
29
+
30
+// SetupLogging will initialize the logger backend and set the flags.
31
+func SetupLogging() {
32
+ backend := logging.NewLogBackend(os.Stderr, "", 0)
33
+ logging.SetBackend(backend)
34
+ logging.SetFormatter(logging.MustStringFormatter(LogFormat))
35
+
36
+ lvl := logging.ERROR
37
+
38
+ var err error
39
+ if logenv := os.Getenv("IPFS_LOGGING"); logenv != "" {
40
+ lvl, err = logging.LogLevel(logenv)
41
+ if err != nil {
42
+ PErr("invalid logging level: %s\n", logenv)
43
+ PErr("using logging.DEBUG\n")
44
+ lvl = logging.DEBUG
45
+ }
46
+ }
47
+
48
+ SetAllLoggers(lvl)
49
+}
50
+
51
+func SetAllLoggers(lvl logging.Level) {
52
+ logging.SetLevel(lvl, "")
53
+ for n, log := range loggers {
54
+ logging.SetLevel(lvl, n)
55
+ log.Error("setting logger: %s to %v", n, lvl)
56
+ }
57
+}
58
+
59
+// Logger retrieves a particular logger + initializes it at a particular level
60
+func Logger(name string) *logging.Logger {
61
+ log := logging.MustGetLogger(name)
62
+ // logging.SetLevel(lvl, name) // can't set level here.
63
+ loggers[name] = log
64
+ return log
65
+}
util/util.go
-121
@@ -2,28 +2,20 @@ package util
2
3
import (
4
"errors"
5
- "fmt"
5
"io"
6
"math/rand"
8
- "os"
7
"os/user"
8
"path/filepath"
9
"strings"
10
"time"
11
12
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
15
- b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
16
- mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
17
- logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
13
)
14
15
func init() {
16
SetupLogging()
17
}
18
24
-// LogFormat is the format used for our logger.
25
-var LogFormat = "%{color}%{time:2006-01-02 15:04:05.999999} %{shortfile} %{level}: %{color:reset}%{message}"
26
-
19
// Debug is a global flag for debugging.
20
var Debug bool
21
@@ -40,72 +32,6 @@ var ErrSearchIncomplete = errors.New("Error: Search Incomplete.")
32
// ErrNotFound is returned when a search fails to find anything
33
var ErrNotFound = ds.ErrNotFound
34
43
-// Key is a string representation of multihash for use with maps.
44
-type Key string
45
-
46
-// String is utililty function for printing out keys as strings (Pretty).
47
-func (k Key) String() string {
48
- return k.Pretty()
49
-}
50
-
51
-// Pretty returns Key in a b58 encoded string
52
-func (k Key) Pretty() string {
53
- return b58.Encode([]byte(k))
54
-}
55
-
56
-// DsKey returns a Datastore key
57
-func (k Key) DsKey() ds.Key {
58
- return ds.NewKey(string(k))
59
-}
60
-
61
-// KeyFromDsKey returns a Datastore key
62
-func KeyFromDsKey(dsk ds.Key) Key {
63
- return Key(dsk.BaseNamespace())
64
-}
65
-
66
-// DsKeyB58Encode returns a B58 encoded Datastore key
67
-// TODO: this is hacky because it encodes every path component. some
68
-// path components may be proper strings already...
69
-func DsKeyB58Encode(dsk ds.Key) ds.Key {
70
- k := ds.NewKey("/")
71
- for _, n := range dsk.Namespaces() {
72
- k = k.Child(b58.Encode([]byte(n)))
73
- }
74
- return k
75
-}
76
-
77
-// DsKeyB58Decode returns a b58 decoded Datastore key
78
-// TODO: this is hacky because it encodes every path component. some
79
-// path components may be proper strings already...
80
-func DsKeyB58Decode(dsk ds.Key) ds.Key {
81
- k := ds.NewKey("/")
82
- for _, n := range dsk.Namespaces() {
83
- k = k.Child(string(b58.Decode(n)))
84
- }
85
- return k
86
-}
87
-
88
-// Hash is the global IPFS hash function. uses multihash SHA2_256, 256 bits
89
-func Hash(data []byte) mh.Multihash {
90
- h, err := mh.Sum(data, mh.SHA2_256, -1)
91
- if err != nil {
92
- // this error can be safely ignored (panic) because multihash only fails
93
- // from the selection of hash function. If the fn + length are valid, it
94
- // won't error.
95
- panic("multihash failed to hash using SHA2_256.")
96
- }
97
- return h
98
-}
99
-
100
-// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
101
-func IsValidHash(s string) bool {
102
- out := b58.Decode(s)
103
- if out == nil || len(out) == 0 {
104
- return false
105
- }
106
- return true
107
-}
108
-
35
// TildeExpansion expands a filename, which may begin with a tilde.
36
func TildeExpansion(filename string) (string, error) {
37
if strings.HasPrefix(filename, "~/") {
@@ -120,53 +46,6 @@ func TildeExpansion(filename string) (string, error) {
46
return filename, nil
47
}
48
123
-// PErr is a shorthand printing function to output to Stderr.
124
-func PErr(format string, a ...interface{}) {
125
- fmt.Fprintf(os.Stderr, format, a...)
126
-}
127
-
128
-// POut is a shorthand printing function to output to Stdout.
129
-func POut(format string, a ...interface{}) {
130
- fmt.Fprintf(os.Stdout, format, a...)
131
-}
132
-
133
-var loggers = map[string]*logging.Logger{}
134
-
135
-// SetupLogging will initialize the logger backend and set the flags.
136
-func SetupLogging() {
137
- backend := logging.NewLogBackend(os.Stderr, "", 0)
138
- logging.SetBackend(backend)
139
- logging.SetFormatter(logging.MustStringFormatter(LogFormat))
140
-
141
- logging.SetLevel(logging.ERROR, "")
142
-
143
- for n, log := range loggers {
144
- logging.SetLevel(logging.ERROR, n)
145
- log.Error("setting logger: %s to %v", n, logging.ERROR)
146
- }
147
-
148
- logenv := os.Getenv("IPFS_LOGGING")
149
- if logenv == "all" {
150
- AllLoggersOn()
151
- }
152
-}
153
-
154
-func AllLoggersOn() {
155
- logging.SetLevel(logging.DEBUG, "")
156
- for n, log := range loggers {
157
- logging.SetLevel(logging.DEBUG, n)
158
- log.Error("setting logger: %s to %v", n, logging.DEBUG)
159
- }
160
-}
161
-
162
-// Logger retrieves a particular logger + initializes it at a particular level
163
-func Logger(name string) *logging.Logger {
164
- log := logging.MustGetLogger(name)
165
- // logging.SetLevel(lvl, name) // can't set level here.
166
- loggers[name] = log
167
- return log
168
-}
169
-
49
// ExpandPathnames takes a set of paths and turns them into absolute paths
50
func ExpandPathnames(paths []string) ([]string, error) {
51
var out []string