-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added xgo and started using Trap to intercept function calls
- Loading branch information
1 parent
7ac02f5
commit 51cb4c2
Showing
10 changed files
with
33 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,28 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"context" | ||
"net/http" | ||
"regexp" | ||
"runtime/trace" | ||
"fmt" | ||
"strings" | ||
"github.com/xhd2015/xgo/runtime/core" | ||
"github.com/xhd2015/xgo/runtime/trap" | ||
) | ||
|
||
func TraceWithLogging(next http.HandlerFunc) http.HandlerFunc { | ||
func AutoLog(fn http.HandlerFunc) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var buf bytes.Buffer | ||
|
||
// Start trace capturing | ||
if err := trace.Start(&buf); err != nil { | ||
http.Error(w, "Failed to start trace", http.StatusInternalServerError) | ||
log.Fatalf("Failed to start trace: %v", err) | ||
return | ||
} | ||
defer func() { | ||
trace.Stop() | ||
|
||
// Extract and process trace output | ||
traceOutput := buf.String() | ||
//log.Println("Raw Trace Output (for debugging):", traceOutput) | ||
// Split the trace output into lines | ||
traceLines := strings.Split(traceOutput, "\n") | ||
startedFormatting := false | ||
|
||
// Log each trace line with a line number and format | ||
for _, line := range traceLines { | ||
if !startedFormatting && strings.Contains(line, "start trace") { | ||
startedFormatting = true | ||
} else { | ||
continue | ||
trap.AddInterceptor(&trap.Interceptor{ | ||
Pre: func(ctx context.Context, f *core.FuncInfo, args core.Object, results core.Object) (interface{}, error) { | ||
index := strings.Index(f.File, "sphinx-tribes") | ||
trimmed := f.File | ||
if index != -1 { | ||
trimmed = f.File[index:] | ||
} | ||
fmt.Printf("%s:%d %s\n", trimmed, f.Line, f.Name) | ||
|
||
if line != "" { | ||
traceFormatted := formatTrace(line) | ||
for j, formattedLine := range traceFormatted { | ||
log.Printf("%03d %s", j+1, formattedLine) | ||
} | ||
} | ||
} | ||
}() | ||
|
||
next(w, r) | ||
return nil, nil | ||
}, | ||
}) | ||
fn(w, r) | ||
} | ||
} | ||
|
||
func splitTrace(traceString string) []string { | ||
// Define the regex pattern for detecting file paths ending with .go | ||
re := regexp.MustCompile(`/([^/\s]+\.go)`) | ||
|
||
// Replace each match with the file path followed by a newline | ||
formattedTrace := re.ReplaceAllString(traceString, "$0\n") | ||
|
||
cleanRegex := regexp.MustCompile(`[^a-zA-Z0-9\s\./:_\-@()]`) | ||
cleanedTrace := cleanRegex.ReplaceAllString(formattedTrace, "") | ||
|
||
|
||
// Split the formatted trace into lines and return as a string array | ||
return strings.Split(strings.TrimSpace(cleanedTrace), "\n") | ||
} | ||
func formatTrace(input string) []string { | ||
// Find the position where "start trace" appears | ||
startIdx := strings.Index(input, "start trace") | ||
if startIdx == -1 { | ||
return nil // "start trace" not found, return nil slice | ||
} | ||
|
||
// Slice input string from "start trace" onwards | ||
traceContent := input[startIdx:] | ||
|
||
// Use splitTrace to split and format the trace content | ||
return splitTrace(traceContent) | ||
} | ||
|
||
|