Skip to content

Commit

Permalink
cmd/wit-bindgen-go, wit/bindgen: use logging.Logger; add -v and -vv f…
Browse files Browse the repository at this point in the history
…or verbose and debug logging
  • Loading branch information
ydnar committed Oct 30, 2024
1 parent 91685e5 commit d0e1d41
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
21 changes: 13 additions & 8 deletions cmd/wit-bindgen-go/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/bytecodealliance/wasm-tools-go/internal/go/gen"
"github.com/bytecodealliance/wasm-tools-go/internal/witcli"
"github.com/bytecodealliance/wasm-tools-go/wit/bindgen"
"github.com/bytecodealliance/wasm-tools-go/wit/logging"
"github.com/urfave/cli/v3"
)

Expand Down Expand Up @@ -66,6 +67,7 @@ var Command = &cli.Command{

// Config is the configuration for the `generate` command.
type config struct {
logger logging.Logger
dryRun bool
out string
outPerm os.FileMode
Expand All @@ -90,6 +92,7 @@ func action(ctx context.Context, cmd *cli.Command) error {

packages, err := bindgen.Go(res,
bindgen.GeneratedBy(cmd.Root().Name),
bindgen.Logger(cfg.logger),
bindgen.World(cfg.world),
bindgen.PackageRoot(cfg.pkgRoot),
bindgen.Versioned(cfg.versioned),
Expand All @@ -103,14 +106,15 @@ func action(ctx context.Context, cmd *cli.Command) error {
}

func parseFlags(cmd *cli.Command) (*config, error) {
logger := witcli.Logger(cmd.Bool("verbose"), cmd.Bool("debug"))
dryRun := cmd.Bool("dry-run")
out := cmd.String("out")

info, err := witcli.FindOrCreateDir(out)
if err != nil {
return nil, err
}
fmt.Fprintf(os.Stderr, "Output dir: %s\n", out)
logger.Infof("Output dir: %s\n", out)
outPerm := info.Mode().Perm()

pkgRoot := cmd.String("package-root")
Expand All @@ -120,14 +124,15 @@ func parseFlags(cmd *cli.Command) (*config, error) {
return nil, err
}
}
fmt.Fprintf(os.Stderr, "Package root: %s\n", pkgRoot)
logger.Infof("Package root: %s\n", pkgRoot)

path, err := witcli.LoadPath(cmd.Args().Slice()...)
if err != nil {
return nil, err
}

return &config{
logger,
dryRun,
out,
outPerm,
Expand All @@ -141,21 +146,21 @@ func parseFlags(cmd *cli.Command) (*config, error) {
}

func writeGoPackages(packages []*gen.Package, cfg *config) error {
fmt.Fprintf(os.Stderr, "Generated %d package(s)\n", len(packages))
cfg.logger.Infof("Generated %d Go package(s)\n", len(packages))
for _, pkg := range packages {
if !pkg.HasContent() {
fmt.Fprintf(os.Stderr, "Skipping empty package: %s\n", pkg.Path)
cfg.logger.Debugf("Skipped empty package: %s\n", pkg.Path)
continue
}
fmt.Fprintf(os.Stderr, "Generated package: %s\n", pkg.Path)
cfg.logger.Infof("Generated package: %s\n", pkg.Path)

for _, filename := range codec.SortedKeys(pkg.Files) {
file := pkg.Files[filename]
dir := filepath.Join(cfg.out, strings.TrimPrefix(file.Package.Path, cfg.pkgRoot))
path := filepath.Join(dir, file.Name)

if !file.HasContent() {
fmt.Fprintf(os.Stderr, "Skipping empty file: %s\n", path)
cfg.logger.Debugf("\tSkipping empty file: %s\n", path)
continue
}

Expand All @@ -168,9 +173,9 @@ func writeGoPackages(packages []*gen.Package, cfg *config) error {
if content == nil {
return err
}
fmt.Fprintf(os.Stderr, "Error formatting file: %v\n", err)
cfg.logger.Errorf("\tError formatting file: %v\n", err)
} else {
fmt.Fprintf(os.Stderr, "Generated file: %s\n", path)
cfg.logger.Infof("\t%s\n", path)
}

if cfg.dryRun {
Expand Down
10 changes: 10 additions & 0 deletions cmd/wit-bindgen-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ var Command = &cli.Command{
Name: "force-wit",
Usage: "force loading WIT via wasm-tools",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "print verbose logging messages",
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"vv"},
Usage: "print debug logging messages",
},
},
Action: action,
}
Expand Down
25 changes: 19 additions & 6 deletions wit/bindgen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"go/token"
"io"
"path"
"path/filepath"
"runtime"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/bytecodealliance/wasm-tools-go/internal/go/gen"
"github.com/bytecodealliance/wasm-tools-go/internal/stringio"
"github.com/bytecodealliance/wasm-tools-go/wit"
"github.com/bytecodealliance/wasm-tools-go/wit/logging"
)

const (
Expand Down Expand Up @@ -139,6 +141,9 @@ func newGenerator(res *wit.Resolve, opts ...Option) (*generator, error) {
if err != nil {
return nil, err
}
if g.opts.logger == nil {
g.opts.logger = logging.NewLogger(io.Discard, logging.LevelNever)
}
if g.opts.generatedBy == "" {
_, file, _, _ := runtime.Caller(0)
_, g.opts.generatedBy = filepath.Split(filepath.Dir(filepath.Dir(file)))
Expand Down Expand Up @@ -166,7 +171,7 @@ func (g *generator) generate() ([]*gen.Package, error) {
func (g *generator) detectVersionedPackages() {
if g.opts.versioned {
g.versioned = true
// fmt.Fprintf(os.Stderr, "Generated versions for all package(s)\n")
g.opts.logger.Infof("Generated versions for all package(s)\n")
return
}
packages := make(map[string]string)
Expand All @@ -180,9 +185,9 @@ func (g *generator) detectVersionedPackages() {
packages[path] = pkg.Name.String()
}
}
// if g.versioned {
// fmt.Fprintf(os.Stderr, "Multiple versions of package(s) detected\n")
// }
if g.versioned {
g.opts.logger.Infof("Multiple versions of package(s) detected\n")
}
}

// define marks a world, interface, type, or function as defined.
Expand All @@ -199,7 +204,7 @@ func (g *generator) define(dir wit.Direction, v wit.Node) (defined bool) {
// Options might override the Go package, including combining multiple
// WIT interfaces and/or worlds into a single Go package.
func (g *generator) defineWorlds() error {
// fmt.Fprintf(os.Stderr, "Generating Go for %d world(s)\n", len(g.res.Worlds))
g.opts.logger.Infof("Generating Go for %d world(s)\n", len(g.res.Worlds))
for i, w := range g.res.Worlds {
if w.Match(g.opts.world) || (g.opts.world == "" && i == len(g.res.Worlds)-1) {
err := g.defineWorld(w)
Expand Down Expand Up @@ -493,7 +498,15 @@ func (g *generator) declareTypeDef(file *gen.File, dir wit.Direction, t *wit.Typ
g.types[otherDir][t] = decl
g.define(otherDir, t) // Mark this type as defined
}
// fmt.Fprintf(os.Stderr, "Type:\t%s.%s\n\t%s.%s\n", owner.String(), name, decl.Package.Path, decl.Name)

{
name := "(anonymous)"
if t.Name != nil {
name = *t.Name
}
g.opts.logger.Debugf("Type:\t%s.%s\n\t%s.%s\n",
g.moduleNames[t.Owner], name, file.Package.Path, decl.name)
}

// Predeclare own<T> and borrow<T> for resource types.
if experimentPredeclareHandles {
Expand Down
25 changes: 25 additions & 0 deletions wit/bindgen/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package bindgen

import (
"log/slog"

"github.com/bytecodealliance/wasm-tools-go/wit/logging"
)

// Option represents a single configuration option for this package.
type Option interface {
applyOption(*options) error
Expand All @@ -12,6 +18,9 @@ func (f optionFunc) applyOption(opts *options) error {
}

type options struct {
logger logging.Logger
slogger *slog.Logger

// generatedBy is the name of the program that generates code with this package.
generatedBy string

Expand Down Expand Up @@ -40,6 +49,22 @@ func (opts *options) apply(o ...Option) error {
return nil
}

// Logger returns an [Option] that specifies a [logging.Logger] for logging.
func Logger(logger logging.Logger) Option {
return optionFunc(func(opts *options) error {
opts.logger = logger
return nil
})
}

// Slogger returns an [Option] that specifies a [slog.Slogger] for logging.
func Slogger(logger *slog.Logger) Option {
return optionFunc(func(opts *options) error {
opts.slogger = logger
return nil
})
}

// GeneratedBy returns an [Option] that specifies the name of the program or package
// that will appear in the "Code generated by ..." header on generated files.
func GeneratedBy(name string) Option {
Expand Down

0 comments on commit d0e1d41

Please sign in to comment.