github.com/status-im/status-go@v1.1.0/_assets/scripts/extract_logs.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "encoding/json" 6 "fmt" 7 "os" 8 "regexp" 9 "strings" 10 "time" 11 ) 12 13 var logPattern = regexp.MustCompile(`(?P<timestamp>[\d-]+T[\d:]+\.\d+Z).*(?P<action>sent-message:.*)\s+(?P<logData>{.*})`) 14 15 func main() { 16 if len(os.Args) != 2 { 17 fmt.Println("Usage: go run script.go <filename>") 18 os.Exit(1) 19 } 20 21 filename := os.Args[1] 22 file, err := os.Open(filename) 23 if err != nil { 24 fmt.Printf("Error opening file: %s\n", err) 25 os.Exit(1) 26 } 27 defer file.Close() 28 29 scanner := bufio.NewScanner(file) 30 31 fmt.Printf("Timestamp\tMessageType\tContentType\tMessageID\tHashes\tRecipients\n") 32 for scanner.Scan() { 33 line := scanner.Text() 34 35 // Check if the line contains "sent-message" 36 if strings.Contains(line, "sent-message") { 37 match := logPattern.FindStringSubmatch(line) 38 39 // Ensure the match is not nil and has expected groups 40 if match != nil && len(match) > 3 { 41 logTime, _ := time.Parse(time.RFC3339Nano, matchMap("timestamp", match)) 42 logData := matchMap("logData", match) 43 44 var data map[string]interface{} 45 if err := json.Unmarshal([]byte(logData), &data); err == nil { 46 recipients := arrayToString(data["recipient"]) 47 messageID := fmt.Sprintf("%v", data["messageID"]) 48 messageType := fmt.Sprintf("%v", data["messageType"]) 49 contentType := fmt.Sprintf("%v", data["contentType"]) 50 hashes := arrayToString(data["hashes"]) 51 52 // Print the required information 53 fmt.Printf("%s\t%s\t%s\t%s\t%s\t%s\t\n", 54 logTime.Format(time.RFC3339Nano), messageType, contentType, messageID, hashes, recipients) 55 } 56 } else { 57 fmt.Printf("Warning: Line does not match expected format: %s\n", line) 58 } 59 } 60 } 61 62 if err := scanner.Err(); err != nil { 63 fmt.Printf("Error reading file: %s\n", err) 64 } 65 } 66 67 // Helper function to convert an array to a string 68 func arrayToString(arr interface{}) string { 69 if arr != nil { 70 switch v := arr.(type) { 71 case []interface{}: 72 var result []string 73 for _, item := range v { 74 result = append(result, fmt.Sprintf("%v", item)) 75 } 76 return strings.Join(result, ", ") 77 } 78 } 79 return "" 80 } 81 82 // Helper function to get the value of a named capture group from regex match 83 func matchMap(key string, matches []string) string { 84 for i, name := range logPattern.SubexpNames() { 85 if name == key { 86 return matches[i] 87 } 88 } 89 return "" 90 }