Skip to content

Commit

Permalink
Write debug logs for autogen excluding for #86
Browse files Browse the repository at this point in the history
  • Loading branch information
jirfag committed Jun 12, 2018
1 parent b4bf038 commit f9027f7
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
5 changes: 4 additions & 1 deletion pkg/commands/root.go
Expand Up @@ -8,6 +8,7 @@ import (
"runtime/pprof"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/printers"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -16,7 +17,9 @@ import (

func setupLog(isVerbose bool) {
log.SetFlags(0) // don't print time
if isVerbose {
if logutils.IsDebugEnabled() {
logrus.SetLevel(logrus.DebugLevel)
} else if isVerbose {
logrus.SetLevel(logrus.InfoLevel)
}
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/logutils/logutils.go
Expand Up @@ -2,6 +2,7 @@ package logutils

import (
"os"
"strings"

"github.com/sirupsen/logrus"
)
Expand All @@ -15,3 +16,38 @@ func HiddenWarnf(format string, args ...interface{}) {
logrus.Infof(format, args...)
}
}

func getEnabledDebugs() map[string]bool {
ret := map[string]bool{}
debugVar := os.Getenv("GL_DEBUG")
if debugVar == "" {
return ret
}

for _, tag := range strings.Split(debugVar, ",") {
ret[tag] = true
}

return ret
}

var enabledDebugs = getEnabledDebugs()

type DebugFunc func(format string, args ...interface{})

func nopDebugf(format string, args ...interface{}) {}

func Debug(tag string) DebugFunc {
if !enabledDebugs[tag] {
return nopDebugf
}

return func(format string, args ...interface{}) {
newArgs := append([]interface{}{tag}, args...)
logrus.Debugf("%s: "+format, newArgs...)
}
}

func IsDebugEnabled() bool {
return len(enabledDebugs) != 0
}
31 changes: 26 additions & 5 deletions pkg/result/processors/autogenerated_exclude.go
Expand Up @@ -7,9 +7,12 @@ import (
"strings"

"github.com/golangci/golangci-lint/pkg/lint/astcache"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)

var autogenDebugf = logutils.Debug("autogen_exclude")

type ageFileSummary struct {
isGenerated bool
}
Expand Down Expand Up @@ -62,10 +65,12 @@ func isGeneratedFileByComment(doc string) bool {
doc = strings.ToLower(doc)
for _, marker := range markers {
if strings.Contains(doc, marker) {
autogenDebugf("doc contains marker %q: file is generated", marker)
return true
}
}

autogenDebugf("doc of len %d doesn't contain any of markers: %s", len(doc), markers)
return false
}

Expand All @@ -83,27 +88,43 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile
return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), f.Err)
}

doc := getDoc(f.F, f.Fset)
autogenDebugf("file %q: astcache file is %+v", i.FilePath(), *f)

doc := getDoc(f.F, f.Fset, i.FilePath())

fs.isGenerated = isGeneratedFileByComment(doc)
autogenDebugf("file %q is generated: %t", i.FilePath(), fs.isGenerated)
return fs, nil
}

func getDoc(f *ast.File, fset *token.FileSet) string {
func getDoc(f *ast.File, fset *token.FileSet, filePath string) string {
// don't use just f.Doc: e.g. mockgen leaves extra line between comment and package name

importPos := f.End()
var importPos token.Pos
if len(f.Imports) != 0 {
importPos = f.Imports[0].Pos()
autogenDebugf("file %q: search comments until first import pos %d (%s)", filePath, importPos, fset.Position(importPos))
} else {
importPos = f.End()
autogenDebugf("file %q: search comments until EOF pos %d (%s)", filePath, importPos, fset.Position(importPos))
}

var neededComments []string
for _, g := range f.Comments {
if g.Pos() < importPos && fset.Position(g.Pos()).Column == 1 {
neededComments = append(neededComments, g.Text())
pos := g.Pos()
filePos := fset.Position(pos)
text := g.Text()
isAllowed := pos < importPos && filePos.Column == 1
if isAllowed {
autogenDebugf("file %q: pos=%d, filePos=%s: comment %q: it's allowed", filePath, pos, filePos, text)
neededComments = append(neededComments, text)
} else {
autogenDebugf("file %q: pos=%d, filePos=%s: comment %q: it's NOT allowed", filePath, pos, filePos, text)
}
}

autogenDebugf("file %q: got %d allowed comments", filePath, len(neededComments))

if len(neededComments) == 0 {
return ""
}
Expand Down
24 changes: 0 additions & 24 deletions pkg/result/processors/autogenerated_exclude_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/testdata/autogenerated/mockgen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f9027f7

Please sign in to comment.