Compare commits

...
Sign in to create a new pull request.

2 commits

Author SHA1 Message Date
0c6a45a4c5 Add log method output 2023-03-20 14:06:15 +03:00
72a7b2eca0 cmd: allow to provide custom functions for export
As an example for the frostfs-node:
```
go run ./cmd -funcs reportFlushError,reportError ../node
```

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
2023-03-17 18:59:00 +03:00
2 changed files with 48 additions and 9 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

@ -1,6 +1,7 @@
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
@ -8,14 +9,25 @@ import (
"log"
"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() {
if len(os.Args) != 2 {
flag.Parse()
if len(*directory) == 0 {
log.Fatalln("directory must be provided")
}
directories, err := getAllSubdirectories(os.Args[1])
directories, err := getAllSubdirectories(*directory)
if err != nil {
log.Fatalf("failed to list subdirs: %v", err)
}
@ -33,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
@ -73,12 +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"))
if !ok {
return false, ""
}
methodsToSearchOnce.Do(func() {
for _, cl := range strings.Split(*customLogs, ",") {
cl = strings.Trim(cl, " ")
if len(cl) > 0 {
methodsToSearch = append(methodsToSearch, cl)
}
}
})
for _, method := range methodsToSearch {
if isIdent(sel.Sel, method) {
return true, method
}
}
return false, ""
}
func isIdent(expr ast.Expr, ident string) bool {