diff --git a/README.md b/README.md index 80f33d9..eb0dc55 100644 --- a/README.md +++ b/README.md @@ -67,3 +67,5 @@ type IP struct { To skip the tag for the generated XXX_* fields, use `-XXX_skip=yaml,xml` flag. + +To enable verbose logging, use `-verbose` \ No newline at end of file diff --git a/file.go b/file.go index b6b21bf..2049e79 100644 --- a/file.go +++ b/file.go @@ -6,7 +6,6 @@ import ( "go/parser" "go/token" "io/ioutil" - "log" "os" "regexp" "strings" @@ -26,7 +25,7 @@ type textArea struct { } func parseFile(inputPath string, xxxSkip []string) (areas []textArea, err error) { - log.Printf("parsing file %q for inject tag comments", inputPath) + logf("parsing file %q for inject tag comments", inputPath) fset := token.NewFileSet() f, err := parser.ParseFile(fset, inputPath, nil, parser.ParseComments) if err != nil { @@ -103,7 +102,7 @@ func parseFile(inputPath string, xxxSkip []string) (areas []textArea, err error) } } } - log.Printf("parsed file %q, number of fields to inject custom tags: %d", inputPath, len(areas)) + logf("parsed file %q, number of fields to inject custom tags: %d", inputPath, len(areas)) return } @@ -125,7 +124,7 @@ func writeFile(inputPath string, areas []textArea) (err error) { // inject custom tags from tail of file first to preserve order for i := range areas { area := areas[len(areas)-i-1] - log.Printf("inject custom tag %q to expression %q", area.InjectTag, string(contents[area.Start-1:area.End-1])) + logf("inject custom tag %q to expression %q", area.InjectTag, string(contents[area.Start-1:area.End-1])) contents = injectTag(contents, area) } if err = ioutil.WriteFile(inputPath, contents, 0644); err != nil { @@ -133,7 +132,7 @@ func writeFile(inputPath string, areas []textArea) (err error) { } if len(areas) > 0 { - log.Printf("file %q is injected with custom tags", inputPath) + logf("file %q is injected with custom tags", inputPath) } return } diff --git a/main.go b/main.go index 54d13fb..4306399 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ func main() { var xxxTags string flag.StringVar(&inputFile, "input", "", "path to input file") flag.StringVar(&xxxTags, "XXX_skip", "", "skip tags to inject on XXX fields") + flag.BoolVar(&verbose, "verbose", false, "verbose logging") flag.Parse() diff --git a/main_test.go b/main_test.go index 51da35d..ee73f98 100644 --- a/main_test.go +++ b/main_test.go @@ -1,7 +1,9 @@ package main import ( + "bytes" "io/ioutil" + "log" "os" "strings" "testing" @@ -160,3 +162,18 @@ func TestContinueParsingWhenSkippingFields(t *testing.T) { } } } + +func TestVerbose(t *testing.T) { + b := new(bytes.Buffer) + log.SetOutput(b) + verbose = false + logf("test") + if len(b.Bytes()) > 0 { + t.Errorf("verbose should be off") + } + verbose = true + logf("test") + if len(b.Bytes()) == 0 { + t.Errorf("verbose should be on") + } +} diff --git a/verbose.go b/verbose.go new file mode 100644 index 0000000..0e6fcc2 --- /dev/null +++ b/verbose.go @@ -0,0 +1,14 @@ +package main + +import ( + "log" +) + +var verbose = false + +func logf(format string, v ...interface{}) { + if !verbose { + return + } + log.Printf(format, v...) +}