Add log method output

This commit is contained in:
Dmitrii Stepanov 2023-03-20 14:06:15 +03:00
parent 72a7b2eca0
commit 0c6a45a4c5
2 changed files with 36 additions and 25 deletions

View file

@ -1,2 +1,14 @@
# log-export
Example for the frostfs-node:
```
go run ./cmd -funcs reportFlushError,reportError -dir ../frostfs-node
```
Command line flags:
```
-funcs - custom functions to export separated by ','
-dir - source code directory
```

View file

@ -10,20 +10,24 @@ import (
"os"
"path/filepath"
"strings"
"sync"
)
var (
customLogs = flag.String("funcs", "", "Custom functions to export delimited by ','.")
directory = flag.String("dir", "", "Source code directory.")
methodsToSearchOnce = &sync.Once{}
methodsToSearch = []string{"Debug", "Info", "Warn", "Error"}
)
func main() {
flag.Parse()
if flag.NArg() != 1 {
if len(*directory) == 0 {
log.Fatalln("directory must be provided")
}
directories, err := getAllSubdirectories(flag.Arg(0))
directories, err := getAllSubdirectories(*directory)
if err != nil {
log.Fatalf("failed to list subdirs: %v", err)
}
@ -41,9 +45,10 @@ func main() {
if !ok {
return true
}
if isLogDot(expr.Fun) && len(expr.Args) > 0 && isStringValue(expr.Args[0]) {
isLog, logMethod := isLogDot(expr.Fun)
if isLog && len(expr.Args) > 0 && isStringValue(expr.Args[0]) {
position := fset.Position(expr.Pos())
fmt.Printf("%v %s\n", position, stringValue(expr.Args[0]))
fmt.Printf("%v %s:%s\n", position, logMethod, stringValue(expr.Args[0]))
return false
}
return true
@ -81,32 +86,26 @@ func getAllSubdirectories(dir string) ([]string, error) {
return result, nil
}
func isLogDot(expr ast.Expr) bool {
func isLogDot(expr ast.Expr) (bool, string) {
sel, ok := expr.(*ast.SelectorExpr)
return ok && (isIdent(sel.Sel, "Debug") ||
isIdent(sel.Sel, "Info") ||
isIdent(sel.Sel, "Warn") ||
isIdent(sel.Sel, "Error") ||
isCustomLog(sel.Sel))
}
func isCustomLog(expr ast.Expr) bool {
id, ok := expr.(*ast.Ident)
if !ok {
return false
return false, ""
}
methodsToSearchOnce.Do(func() {
for _, cl := range strings.Split(*customLogs, ",") {
cl = strings.Trim(cl, " ")
if len(cl) > 0 {
methodsToSearch = append(methodsToSearch, cl)
}
}
})
if len(*customLogs) == 0 {
return false
}
ss := strings.Split(*customLogs, ",")
for i := range ss {
if id.Name == ss[i] {
return true
for _, method := range methodsToSearch {
if isIdent(sel.Sel, method) {
return true, method
}
}
return false
return false, ""
}
func isIdent(expr ast.Expr, ident string) bool {