From e232bfeef460d50afcd77644cfb733b5a5810c3e Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Fri, 17 Mar 2023 18:23:02 +0300 Subject: [PATCH] export logs by function name and args --- .gitignore | 2 + Makefile | 5 +++ cmd/main.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ 4 files changed, 113 insertions(+) create mode 100644 Makefile create mode 100644 cmd/main.go create mode 100644 go.mod diff --git a/.gitignore b/.gitignore index adf8f72..cee5c7a 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,8 @@ # Dependency directories (remove the comment below to include it) # vendor/ +.vscode +bin/ # Go workspace file go.work diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c747433 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +#!/usr/bin/make -f +SHELL = bash + +build: + CGO_ENABLED=0 go build -v -trimpath -o ./bin/log-export ./cmd/main.go \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..9a7edfe --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,103 @@ +package main + +import ( + "fmt" + "go/ast" + "go/parser" + "go/token" + "log" + "os" + "path/filepath" +) + +func main() { + if len(os.Args) != 2 { + log.Fatalln("directory must be provided") + } + + directories, err := getAllSubdirectories(os.Args[1]) + if err != nil { + log.Fatalf("failed to list subdirs: %v", err) + } + + for _, dir := range directories { + fset := token.NewFileSet() + packages, err := parser.ParseDir(fset, dir, nil, 0) + if err != nil { + log.Fatalf("failed to parse directory %s: %v", dir, err) + } + for _, p := range packages { + for _, f := range p.Files { + ast.Inspect(f, func(n ast.Node) bool { + expr, ok := n.(*ast.CallExpr) + if !ok { + return true + } + if isLogDot(expr.Fun) && len(expr.Args) > 0 && isStringValue(expr.Args[0]) { + position := fset.Position(expr.Pos()) + fmt.Printf("%v %s\n", position, stringValue(expr.Args[0])) + return false + } + return true + }) + } + } + + } +} + +func getAllSubdirectories(dir string) ([]string, error) { + result := make([]string, 0) + + stack := make([]string, 0) + stack = append(stack, dir) + + for len(stack) > 0 { + current := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + entities, err := os.ReadDir(current) + if err != nil { + return nil, err + } + + for _, e := range entities { + if e.IsDir() { + path := filepath.Join(current, e.Name()) + result = append(result, path) + stack = append(stack, path) + } + } + } + + return result, nil +} + +func isLogDot(expr ast.Expr) bool { + sel, ok := expr.(*ast.SelectorExpr) + return ok && (isIdent(sel.Sel, "Debug") || + isIdent(sel.Sel, "Info") || + isIdent(sel.Sel, "Warn") || + isIdent(sel.Sel, "Error")) +} + +func isIdent(expr ast.Expr, ident string) bool { + id, ok := expr.(*ast.Ident) + if !ok { + return false + } + if id.Name == ident { + return true + } + return false +} + +func isStringValue(expr ast.Expr) bool { + basicLit, ok := expr.(*ast.BasicLit) + return ok && basicLit.Kind == token.STRING +} + +func stringValue(expr ast.Expr) string { + basicLit, _ := expr.(*ast.BasicLit) + return basicLit.Value +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5c96400 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.frostfs.info/dstepanov-yadro/log-export + +go 1.20