From 0c6a45a4c5fa279b8ac8225c2c99ca50bd2326d5 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Mon, 20 Mar 2023 14:06:15 +0300 Subject: [PATCH] Add log method output --- README.md | 12 ++++++++++++ cmd/main.go | 49 ++++++++++++++++++++++++------------------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 7cc4135..08fbc80 100644 --- a/README.md +++ b/README.md @@ -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 +``` \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index 201d9ef..2d8a1e5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -10,20 +10,24 @@ import ( "os" "path/filepath" "strings" + "sync" ) var ( - customLogs = flag.String("funcs", "", "Custom functions to export delimited by ','.") + 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 {