@cryptotaxi247 / kubo / commits / aa79f3858

cleanup logging setup.

- should not be on init, because need debug flag

Juan Batiz-Benet committed Oct 1, 2014 at 01:56 UTC aa79f38580661570304a9b72a32f7ccd1e2c7ac8
5 files changed +38 -26
cmd/ipfs/ipfs.go
+22 -13
@@ -6,9 +6,10 @@ import (
6 "os"
7 "runtime/pprof"
8
9 - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
10 - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
11 - "github.com/jbenet/go-ipfs/config"
9 + flag "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
10 + commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
11 +
12 + config "github.com/jbenet/go-ipfs/config"
13 core "github.com/jbenet/go-ipfs/core"
14 u "github.com/jbenet/go-ipfs/util"
15 )
@@ -72,16 +73,24 @@ func ipfsCmd(c *commander.Command, args []string) error {
73 }
74
75 func main() {
75 - u.Debug = true
76 - ofi, err := os.Create("cpu.prof")
77 - if err != nil {
78 - fmt.Println(err)
79 - return
76 + u.Debug = false
77 +
78 + // setup logging
79 + u.SetupLogging()
80 +
81 + // if debugging, setup profiling.
82 + if u.Debug {
83 + ofi, err := os.Create("cpu.prof")
84 + if err != nil {
85 + fmt.Println(err)
86 + return
87 + }
88 + pprof.StartCPUProfile(ofi)
89 + defer ofi.Close()
90 + defer pprof.StopCPUProfile()
91 }
81 - pprof.StartCPUProfile(ofi)
82 - defer ofi.Close()
83 - defer pprof.StopCPUProfile()
84 - err = CmdIpfs.Dispatch(os.Args[1:])
92 +
93 + err := CmdIpfs.Dispatch(os.Args[1:])
94 if err != nil {
95 if len(err.Error()) > 0 {
96 fmt.Fprintf(os.Stderr, "ipfs %s: %v\n", os.Args[1], err)
@@ -114,7 +123,7 @@ func getConfigDir(c *commander.Command) (string, error) {
123 }
124 confStr, ok := conf.(string)
125 if !ok {
117 - return "", errors.New("failed to retrieve config flag value.")
126 + return "", errors.New("failed to retrieve config flag value")
127 }
128 if len(confStr) == 0 {
129 return config.PathRoot()
cmd/ipfs/publish.go
-1
@@ -25,7 +25,6 @@ func init() {
25 }
26
27 func pubCmd(c *commander.Command, inp []string) error {
28 - u.Debug = true
28 if len(inp) < 1 {
29 u.POut(c.Long)
30 return nil
cmd/ipfs/resolve.go
-1
@@ -23,7 +23,6 @@ var cmdIpfsResolve = &commander.Command{
23 }
24
25 func resolveCmd(c *commander.Command, inp []string) error {
26 - u.Debug = true
26 if len(inp) < 1 {
27 u.POut(c.Long)
28 return nil
cmd/ipfs/run.go
-3
@@ -7,7 +7,6 @@ import (
7 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
8 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
9 "github.com/jbenet/go-ipfs/daemon"
10 - u "github.com/jbenet/go-ipfs/util"
10 )
11
12 var cmdIpfsRun = &commander.Command{
@@ -20,8 +19,6 @@ var cmdIpfsRun = &commander.Command{
19 }
20
21 func runCmd(c *commander.Command, inp []string) error {
23 - u.Debug = true
24 -
22 conf, err := getConfigDir(c.Parent)
23 if err != nil {
24 return err
util/util.go
+16 -8
@@ -11,16 +11,11 @@ import (
11 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
12 b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
13 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
14 - "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
14 + logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
15 )
16
17 -var format = "%{color}%{time:01-02 15:04:05.9999} %{shortfile} %{level}: %{color:reset}%{message}"
18 -
19 -func init() {
20 - backend := logging.NewLogBackend(os.Stderr, "", 0)
21 - logging.SetBackend(backend)
22 - logging.SetFormatter(logging.MustStringFormatter(format))
23 -}
17 +// LogFormat is the format used for our logger.
18 +var LogFormat = "%{color}%{time:01-02 15:04:05.9999} %{shortfile} %{level}: %{color:reset}%{message}"
19
20 // Debug is a global flag for debugging.
21 var Debug bool
@@ -51,6 +46,7 @@ func Hash(data []byte) (mh.Multihash, error) {
46 return mh.Sum(data, mh.SHA2_256, -1)
47 }
48
49 +// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
50 func IsValidHash(s string) bool {
51 out := b58.Decode(s)
52 if out == nil || len(out) == 0 {
@@ -99,6 +95,18 @@ func DOut(format string, a ...interface{}) {
95 }
96 }
97
98 +// SetupLogging will initialize the logger backend and set the flags.
99 +func SetupLogging() {
100 + backend := logging.NewLogBackend(os.Stderr, "", 0)
101 + logging.SetBackend(backend)
102 + if Debug {
103 + logging.SetLevel(logging.DEBUG, "")
104 + } else {
105 + logging.SetLevel(logging.ERROR, "")
106 + }
107 + logging.SetFormatter(logging.MustStringFormatter(LogFormat))
108 +}
109 +
110 // ExpandPathnames takes a set of paths and turns them into absolute paths
111 func ExpandPathnames(paths []string) ([]string, error) {
112 var out []string