forked from dstepanov-yadro/log-export
Compare commits
1 commit
Author | SHA1 | Date | |
---|---|---|---|
0c6a45a4c5 |
2 changed files with 36 additions and 25 deletions
12
README.md
12
README.md
|
@ -1,2 +1,14 @@
|
||||||
# log-export
|
# 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
|
||||||
|
```
|
49
cmd/main.go
49
cmd/main.go
|
@ -10,20 +10,24 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
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() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if flag.NArg() != 1 {
|
if len(*directory) == 0 {
|
||||||
log.Fatalln("directory must be provided")
|
log.Fatalln("directory must be provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
directories, err := getAllSubdirectories(flag.Arg(0))
|
directories, err := getAllSubdirectories(*directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to list subdirs: %v", err)
|
log.Fatalf("failed to list subdirs: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -41,9 +45,10 @@ func main() {
|
||||||
if !ok {
|
if !ok {
|
||||||
return true
|
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())
|
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 false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -81,32 +86,26 @@ func getAllSubdirectories(dir string) ([]string, error) {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isLogDot(expr ast.Expr) bool {
|
func isLogDot(expr ast.Expr) (bool, string) {
|
||||||
sel, ok := expr.(*ast.SelectorExpr)
|
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 {
|
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 {
|
for _, method := range methodsToSearch {
|
||||||
return false
|
if isIdent(sel.Sel, method) {
|
||||||
}
|
return true, method
|
||||||
|
|
||||||
ss := strings.Split(*customLogs, ",")
|
|
||||||
for i := range ss {
|
|
||||||
if id.Name == ss[i] {
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func isIdent(expr ast.Expr, ident string) bool {
|
func isIdent(expr ast.Expr, ident string) bool {
|
||||||
|
|
Loading…
Add table
Reference in a new issue