Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Refactor logging #363

Merged
merged 3 commits into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"runtime"
"strings"
"time"

"github.com/constabulary/gb/log"
)

// Build builds each of pkgs in succession. If pkg is a command, then the results of build include
Expand Down Expand Up @@ -43,14 +45,14 @@ func BuildPackages(pkgs ...*Package) (*Action, error) {
build := Action{
Name: fmt.Sprintf("build: %s", strings.Join(names(pkgs), ",")),
Task: TaskFn(func() error {
Debugf("build duration: %v %v", time.Since(t0), pkgs[0].Statistics.String())
log.Debugf("build duration: %v %v", time.Since(t0), pkgs[0].Statistics.String())
return nil
}),
}

for _, pkg := range pkgs {
if len(pkg.GoFiles)+len(pkg.CgoFiles) == 0 {
Debugf("skipping %v: no go files", pkg.ImportPath)
log.Debugf("skipping %v: no go files", pkg.ImportPath)
continue
}
a, err := BuildPackage(targets, pkg)
Expand Down Expand Up @@ -234,7 +236,7 @@ func gc(pkg *Package, gofiles []string) error {
t0 := time.Now()
if pkg.Scope != "test" {
// only log compilation message if not in test scope
Infof(pkg.ImportPath)
log.Infof(pkg.ImportPath)
}
includes := pkg.IncludePaths()
importpath := pkg.ImportPath
Expand Down
14 changes: 8 additions & 6 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"reflect"
"sort"
"testing"

"github.com/constabulary/gb/log"
)

func TestBuild(t *testing.T) {
Verbose = false
defer func() { Verbose = false }()
log.Verbose = false
defer func() { log.Verbose = false }()
tests := []struct {
pkg string
err error
Expand Down Expand Up @@ -73,8 +75,8 @@ func TestBuild(t *testing.T) {
}

func TestBuildPackage(t *testing.T) {
Verbose = false
defer func() { Verbose = false }()
log.Verbose = false
defer func() { log.Verbose = false }()
tests := []struct {
pkg string
err error
Expand Down Expand Up @@ -126,8 +128,8 @@ func TestBuildPackage(t *testing.T) {
}

func TestBuildPackages(t *testing.T) {
Verbose = false
defer func() { Verbose = false }()
log.Verbose = false
defer func() { log.Verbose = false }()
tests := []struct {
pkgs []string
actions []string
Expand Down
5 changes: 3 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/constabulary/gb"
"github.com/constabulary/gb/log"
)

// Command represents a subcommand, or plugin that is executed within
Expand Down Expand Up @@ -65,7 +66,7 @@ func RunCommand(fs *flag.FlagSet, cmd *Command, projectroot, goroot string, args
}
defer ctx.Destroy()

gb.Debugf("args: %v", args)
log.Debugf("args: %v", args)
return cmd.Run(ctx, args)
}

Expand All @@ -84,7 +85,7 @@ func NewContext(projectroot string, options ...func(*gb.Context) error) (*gb.Con
gb.SourceDir(filepath.Join(root, "vendor", "src")),
)

gb.Debugf("project root %q", project.Projectdir())
log.Debugf("project root %q", project.Projectdir())
return project.NewContext(options...)
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"fmt"
"log"
"os"
"strings"

"github.com/constabulary/gb"
)

func MustGetwd() string {
wd, err := os.Getwd()
if err != nil {
gb.Fatalf("unable to determine current working directory: %v", err)
log.Fatalf("unable to determine current working directory: %v", err)
}
return wd
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/gb-vendor/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
"github.com/constabulary/gb/log"
"github.com/constabulary/gb/vendor"
)

Expand Down Expand Up @@ -189,7 +190,7 @@ func fetch(ctx *gb.Context, path string, recurse bool) error {
keys := keys(missing)
sort.Strings(keys)
pkg := keys[0]
gb.Infof("fetching recursive dependency %s", pkg)
log.Infof("fetching recursive dependency %s", pkg)
if err := fetch(ctx, pkg, false); err != nil {
return err
}
Expand Down
17 changes: 9 additions & 8 deletions cmd/gb-vendor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
"github.com/constabulary/gb/log"
)

var (
Expand Down Expand Up @@ -42,18 +43,18 @@ func main() {
help(args[1:])
return
case projectroot == "":
gb.Fatalf("don't run this binary directly, it is meant to be run as 'gb vendor ...'")
log.Fatalf("don't run this binary directly, it is meant to be run as 'gb vendor ...'")
default:
}

root, err := cmd.FindProjectroot(projectroot)
if err != nil {
gb.Fatalf("could not locate project root: %v", err)
log.Fatalf("could not locate project root: %v", err)
}
project := gb.NewProject(root,
gb.SourceDir(filepath.Join(root, "src")),
gb.SourceDir(filepath.Join(root, "vendor", "src")))
gb.Debugf("project root %q", project.Projectdir())
log.Debugf("project root %q", project.Projectdir())

for _, command := range commands {
if command.Name == args[0] && command.Runnable() {
Expand All @@ -69,26 +70,26 @@ func main() {
err = fs.Parse(args[1:])
}
if err != nil {
gb.Fatalf("could not parse flags: %v", err)
log.Fatalf("could not parse flags: %v", err)
}
args = fs.Args() // reset args to the leftovers from fs.Parse
gb.Debugf("args: %v", args)
log.Debugf("args: %v", args)

ctx, err := project.NewContext(
gb.GcToolchain(),
)
if err != nil {
gb.Fatalf("unable to construct context: %v", err)
log.Fatalf("unable to construct context: %v", err)
}
defer ctx.Destroy()

if err := command.Run(ctx, args); err != nil {
gb.Fatalf("command %q failed: %v", command.Name, err)
log.Fatalf("command %q failed: %v", command.Name, err)
}
return
}
}
gb.Fatalf("unknown command %q ", args[0])
log.Fatalf("unknown command %q ", args[0])
}

const manifestfile = "manifest"
Expand Down
3 changes: 2 additions & 1 deletion cmd/gb/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"text/template"
Expand Down Expand Up @@ -62,7 +63,7 @@ func list(ctx *gb.Context, args []string) error {
}
pkgs, err := cmd.ResolvePackages(ctx, args...)
if err != nil {
gb.Fatalf("unable to resolve: %v", err)
log.Fatalf("unable to resolve: %v", err)
}

if jsonOutput {
Expand Down
17 changes: 9 additions & 8 deletions cmd/gb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/constabulary/gb"
"github.com/constabulary/gb/cmd"
"github.com/constabulary/gb/log"
)

var (
Expand All @@ -23,8 +24,8 @@ const (
)

func init() {
fs.BoolVar(&gb.Quiet, "q", gb.Quiet, "suppress log messages below ERROR level")
fs.BoolVar(&gb.Verbose, "v", gb.Verbose, "enable log levels below INFO level")
fs.BoolVar(&log.Quiet, "q", log.Quiet, "suppress log messages below ERROR level")
fs.BoolVar(&log.Verbose, "v", log.Verbose, "enable log levels below INFO level")
fs.StringVar(&cwd, "R", cmd.MustGetwd(), "set the project root") // actually the working directory to start the project root search

fs.Usage = usage
Expand Down Expand Up @@ -54,7 +55,7 @@ func main() {
if (command != nil && !command.Runnable()) || !ok {
plugin, err := lookupPlugin(name)
if err != nil {
gb.Errorf("unknown command %q", name)
fmt.Fprintf(os.Stderr, "FATAL: unknown command %q\n", name)
fs.Usage()
os.Exit(1)
}
Expand Down Expand Up @@ -98,7 +99,7 @@ func main() {
err = fs.Parse(args[2:])
}
if err != nil {
gb.Fatalf("could not parse flags: %v", err)
log.Fatalf("could not parse flags: %v", err)
}

args = fs.Args() // reset args to the leftovers from fs.Parse
Expand All @@ -107,7 +108,7 @@ func main() {
}
cwd, err := filepath.Abs(cwd) // if cwd was passed in via -R, make sure it is absolute
if err != nil {
gb.Fatalf("could not make project root absolute: %v", err)
log.Fatalf("could not make project root absolute: %v", err)
}

ctx, err := cmd.NewContext(
Expand All @@ -117,7 +118,7 @@ func main() {
gb.Ldflags(ldflags),
)
if err != nil {
gb.Fatalf("unable to construct context: %v", err)
log.Fatalf("unable to construct context: %v", err)
}

if !noDestroyContext {
Expand All @@ -130,8 +131,8 @@ func main() {
args = cmd.ImportPaths(ctx, cwd, args)
}

gb.Debugf("args: %v", args)
log.Debugf("args: %v", args)
if err := command.Run(ctx, args); err != nil {
gb.Fatalf("command %q failed: %v", name, err)
log.Fatalf("command %q failed: %v", name, err)
}
}
3 changes: 2 additions & 1 deletion cmd/gotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"unicode/utf8"

"github.com/constabulary/gb"
"github.com/constabulary/gb/log"
)

type coverInfo struct {
Expand Down Expand Up @@ -91,7 +92,7 @@ func loadTestFuncs(ptest *build.Package) (*testFuncs, error) {
t := &testFuncs{
Package: ptest,
}
gb.Debugf("loadTestFuncs: %v, %v", ptest.TestGoFiles, ptest.XTestGoFiles)
log.Debugf("loadTestFuncs: %v, %v", ptest.TestGoFiles, ptest.XTestGoFiles)
for _, file := range ptest.TestGoFiles {
if err := t.load(filepath.Join(ptest.Dir, file), "_test", &t.ImportTest, &t.NeedTest); err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion cmd/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -47,7 +48,7 @@ func relImportPath(root, path string) string {
var err error
path, err = filepath.Rel(root, path)
if err != nil {
gb.Fatalf("could not convert relative path %q to absolute: %v", path, err)
log.Fatalf("could not convert relative path %q to absolute: %v", path, err)
}
}
return path
Expand Down
3 changes: 2 additions & 1 deletion cmd/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/build"

"github.com/constabulary/gb"
"github.com/constabulary/gb/log"
)

// Resolver resolves packages.
Expand Down Expand Up @@ -48,7 +49,7 @@ func ResolvePackagesWithTests(r Resolver, paths ...string) ([]*gb.Package, error
pkg, err := r.ResolvePackageWithTests(path)
if err != nil {
if _, ok := err.(*build.NoGoError); ok {
gb.Debugf("skipping %q", path)
log.Debugf("skipping %q", path)
continue
}
return pkgs, fmt.Errorf("failed to resolve package %q: %v", path, err)
Expand Down
5 changes: 3 additions & 2 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/constabulary/gb"
"github.com/constabulary/gb/log"
)

// Test returns a Target representing the result of compiling the
Expand Down Expand Up @@ -45,7 +46,7 @@ func TestPackages(flags []string, pkgs ...*gb.Package) (*gb.Action, error) {
test := gb.Action{
Name: fmt.Sprintf("test: %s", strings.Join(names(pkgs), ",")),
Task: gb.TaskFn(func() error {
gb.Debugf("test duration: %v %v", time.Since(t0), pkgs[0].Statistics.String())
log.Debugf("test duration: %v %v", time.Since(t0), pkgs[0].Statistics.String())
return nil
}),
}
Expand Down Expand Up @@ -167,7 +168,7 @@ func TestPackage(targets map[string]*gb.Action, pkg *gb.Package, flags []string)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

gb.Debugf("scheduling run of %v", cmd.Args)
log.Debugf("scheduling run of %v", cmd.Args)
return &gb.Action{
Name: fmt.Sprintf("run: %s", cmd.Args),
Deps: []*gb.Action{testmain},
Expand Down
5 changes: 3 additions & 2 deletions cmd/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"time"

"github.com/constabulary/gb"
"github.com/constabulary/gb/log"
)

func TestTest(t *testing.T) {
gb.Verbose = false
defer func() { gb.Verbose = false }()
log.Verbose = false
defer func() { log.Verbose = false }()
tests := []struct {
pkg string
testArgs []string
Expand Down
Loading