1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-25 23:21:54 +08:00

cleanup logging setup.

- should not be on init, because need debug flag
This commit is contained in:
Juan Batiz-Benet
2014-10-01 01:56:27 -07:00
parent ba510cbd6d
commit aa79f38580
5 changed files with 38 additions and 26 deletions

View File

@ -6,9 +6,10 @@ import (
"os" "os"
"runtime/pprof" "runtime/pprof"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag" flag "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
"github.com/jbenet/go-ipfs/config"
config "github.com/jbenet/go-ipfs/config"
core "github.com/jbenet/go-ipfs/core" core "github.com/jbenet/go-ipfs/core"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
@ -72,16 +73,24 @@ func ipfsCmd(c *commander.Command, args []string) error {
} }
func main() { func main() {
u.Debug = true u.Debug = false
ofi, err := os.Create("cpu.prof")
if err != nil { // setup logging
fmt.Println(err) u.SetupLogging()
return
// 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() err := CmdIpfs.Dispatch(os.Args[1:])
defer pprof.StopCPUProfile()
err = CmdIpfs.Dispatch(os.Args[1:])
if err != nil { if err != nil {
if len(err.Error()) > 0 { if len(err.Error()) > 0 {
fmt.Fprintf(os.Stderr, "ipfs %s: %v\n", os.Args[1], err) 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) confStr, ok := conf.(string)
if !ok { if !ok {
return "", errors.New("failed to retrieve config flag value.") return "", errors.New("failed to retrieve config flag value")
} }
if len(confStr) == 0 { if len(confStr) == 0 {
return config.PathRoot() return config.PathRoot()

View File

@ -25,7 +25,6 @@ func init() {
} }
func pubCmd(c *commander.Command, inp []string) error { func pubCmd(c *commander.Command, inp []string) error {
u.Debug = true
if len(inp) < 1 { if len(inp) < 1 {
u.POut(c.Long) u.POut(c.Long)
return nil return nil

View File

@ -23,7 +23,6 @@ var cmdIpfsResolve = &commander.Command{
} }
func resolveCmd(c *commander.Command, inp []string) error { func resolveCmd(c *commander.Command, inp []string) error {
u.Debug = true
if len(inp) < 1 { if len(inp) < 1 {
u.POut(c.Long) u.POut(c.Long)
return nil return nil

View File

@ -7,7 +7,6 @@ import (
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander" "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" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
"github.com/jbenet/go-ipfs/daemon" "github.com/jbenet/go-ipfs/daemon"
u "github.com/jbenet/go-ipfs/util"
) )
var cmdIpfsRun = &commander.Command{ var cmdIpfsRun = &commander.Command{
@ -20,8 +19,6 @@ var cmdIpfsRun = &commander.Command{
} }
func runCmd(c *commander.Command, inp []string) error { func runCmd(c *commander.Command, inp []string) error {
u.Debug = true
conf, err := getConfigDir(c.Parent) conf, err := getConfigDir(c.Parent)
if err != nil { if err != nil {
return err return err

View File

@ -11,16 +11,11 @@ import (
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" 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" 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" 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}" // LogFormat is the format used for our logger.
var LogFormat = "%{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))
}
// Debug is a global flag for debugging. // Debug is a global flag for debugging.
var Debug bool var Debug bool
@ -51,6 +46,7 @@ func Hash(data []byte) (mh.Multihash, error) {
return mh.Sum(data, mh.SHA2_256, -1) 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 { func IsValidHash(s string) bool {
out := b58.Decode(s) out := b58.Decode(s)
if out == nil || len(out) == 0 { 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 // ExpandPathnames takes a set of paths and turns them into absolute paths
func ExpandPathnames(paths []string) ([]string, error) { func ExpandPathnames(paths []string) ([]string, error) {
var out []string var out []string