Commit aa79f385 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

cleanup logging setup.

- should not be on init, because need debug flag
parent ba510cbd
......@@ -6,9 +6,10 @@ import (
"os"
"runtime/pprof"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
"github.com/jbenet/go-ipfs/config"
flag "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
config "github.com/jbenet/go-ipfs/config"
core "github.com/jbenet/go-ipfs/core"
u "github.com/jbenet/go-ipfs/util"
)
......@@ -72,16 +73,24 @@ func ipfsCmd(c *commander.Command, args []string) error {
}
func main() {
u.Debug = true
ofi, err := os.Create("cpu.prof")
if err != nil {
fmt.Println(err)
return
u.Debug = false
// setup logging
u.SetupLogging()
// if debugging, setup profiling.
if u.Debug {
ofi, err := os.Create("cpu.prof")
if err != nil {
fmt.Println(err)
return
}
pprof.StartCPUProfile(ofi)
defer ofi.Close()
defer pprof.StopCPUProfile()
}
pprof.StartCPUProfile(ofi)
defer ofi.Close()
defer pprof.StopCPUProfile()
err = CmdIpfs.Dispatch(os.Args[1:])
err := CmdIpfs.Dispatch(os.Args[1:])
if err != nil {
if len(err.Error()) > 0 {
fmt.Fprintf(os.Stderr, "ipfs %s: %v\n", os.Args[1], err)
......@@ -114,7 +123,7 @@ func getConfigDir(c *commander.Command) (string, error) {
}
confStr, ok := conf.(string)
if !ok {
return "", errors.New("failed to retrieve config flag value.")
return "", errors.New("failed to retrieve config flag value")
}
if len(confStr) == 0 {
return config.PathRoot()
......
......@@ -25,7 +25,6 @@ func init() {
}
func pubCmd(c *commander.Command, inp []string) error {
u.Debug = true
if len(inp) < 1 {
u.POut(c.Long)
return nil
......
......@@ -23,7 +23,6 @@ var cmdIpfsResolve = &commander.Command{
}
func resolveCmd(c *commander.Command, inp []string) error {
u.Debug = true
if len(inp) < 1 {
u.POut(c.Long)
return nil
......
......@@ -7,7 +7,6 @@ import (
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
"github.com/jbenet/go-ipfs/daemon"
u "github.com/jbenet/go-ipfs/util"
)
var cmdIpfsRun = &commander.Command{
......@@ -20,8 +19,6 @@ var cmdIpfsRun = &commander.Command{
}
func runCmd(c *commander.Command, inp []string) error {
u.Debug = true
conf, err := getConfigDir(c.Parent)
if err != nil {
return err
......
......@@ -11,16 +11,11 @@ import (
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
)
var format = "%{color}%{time:01-02 15:04:05.9999} %{shortfile} %{level}: %{color:reset}%{message}"
func init() {
backend := logging.NewLogBackend(os.Stderr, "", 0)
logging.SetBackend(backend)
logging.SetFormatter(logging.MustStringFormatter(format))
}
// LogFormat is the format used for our logger.
var LogFormat = "%{color}%{time:01-02 15:04:05.9999} %{shortfile} %{level}: %{color:reset}%{message}"
// Debug is a global flag for debugging.
var Debug bool
......@@ -51,6 +46,7 @@ func Hash(data []byte) (mh.Multihash, error) {
return mh.Sum(data, mh.SHA2_256, -1)
}
// IsValidHash checks whether a given hash is valid (b58 decodable, len > 0)
func IsValidHash(s string) bool {
out := b58.Decode(s)
if out == nil || len(out) == 0 {
......@@ -99,6 +95,18 @@ func DOut(format string, a ...interface{}) {
}
}
// SetupLogging will initialize the logger backend and set the flags.
func SetupLogging() {
backend := logging.NewLogBackend(os.Stderr, "", 0)
logging.SetBackend(backend)
if Debug {
logging.SetLevel(logging.DEBUG, "")
} else {
logging.SetLevel(logging.ERROR, "")
}
logging.SetFormatter(logging.MustStringFormatter(LogFormat))
}
// ExpandPathnames takes a set of paths and turns them into absolute paths
func ExpandPathnames(paths []string) ([]string, error) {
var out []string
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment