chore_: Add script for extracting logs

This commit is contained in:
Andrea Maria Piana 2024-02-27 10:40:40 +00:00
parent 4c6ce176a4
commit 5cfaa00195
4 changed files with 135 additions and 4 deletions

13
_assets/scripts/README.md Normal file
View File

@ -0,0 +1,13 @@
# Scripts
# extract_logs.go
This script analyzes geth.log files in a specific format and extracts information related to "sent-message" actions. It then prints relevant details such as timestamp, recipients, message ID, message type, and hashes to the console.
## Usage
```bash
go run extract_logs.go <filename>
```
It will output in tab separated values (TSV)

View File

@ -0,0 +1,90 @@
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"time"
)
var logPattern = regexp.MustCompile(`(?P<timestamp>[\d-]+T[\d:]+\.\d+Z).*(?P<action>sent-message:.*)\s+(?P<logData>{.*})`)
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run script.go <filename>")
os.Exit(1)
}
filename := os.Args[1]
file, err := os.Open(filename)
if err != nil {
fmt.Printf("Error opening file: %s\n", err)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
fmt.Printf("Timestamp\tMessageType\tContentType\tMessageID\tHashes\tRecipients\n")
for scanner.Scan() {
line := scanner.Text()
// Check if the line contains "sent-message"
if strings.Contains(line, "sent-message") {
match := logPattern.FindStringSubmatch(line)
// Ensure the match is not nil and has expected groups
if match != nil && len(match) > 3 {
logTime, _ := time.Parse(time.RFC3339Nano, matchMap("timestamp", match))
logData := matchMap("logData", match)
var data map[string]interface{}
if err := json.Unmarshal([]byte(logData), &data); err == nil {
recipients := arrayToString(data["recipient"])
messageID := fmt.Sprintf("%v", data["messageID"])
messageType := fmt.Sprintf("%v", data["messageType"])
contentType := fmt.Sprintf("%v", data["contentType"])
hashes := arrayToString(data["hashes"])
// Print the required information
fmt.Printf("%s\t%s\t%s\t%s\t%s\t%s\t\n",
logTime.Format(time.RFC3339Nano), messageType, contentType, messageID, hashes, recipients)
}
} else {
fmt.Printf("Warning: Line does not match expected format: %s\n", line)
}
}
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading file: %s\n", err)
}
}
// Helper function to convert an array to a string
func arrayToString(arr interface{}) string {
if arr != nil {
switch v := arr.(type) {
case []interface{}:
var result []string
for _, item := range v {
result = append(result, fmt.Sprintf("%v", item))
}
return strings.Join(result, ", ")
}
}
return ""
}
// Helper function to get the value of a named capture group from regex match
func matchMap(key string, matches []string) string {
for i, name := range logPattern.SubexpNames() {
if name == key {
return matches[i]
}
}
return ""
}

View File

@ -85,6 +85,14 @@ func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
}
func PubkeysToHex(keys []*ecdsa.PublicKey) []string {
var result []string
for _, k := range keys {
result = append(result, PubkeyToHex(k))
}
return result
}
func PubkeyToHex(key *ecdsa.PublicKey) string {
return types.EncodeHex(crypto.FromECDSAPub(key))
}

View File

@ -384,7 +384,12 @@ func (s *MessageSender) sendCommunity(
}
}
s.logger.Debug("sent community message ", zap.String("messageID", messageID.String()), zap.Strings("hashes", types.EncodeHexes(hashes)))
s.logger.Debug("sent-message: community ",
zap.Strings("recipient", PubkeysToHex(rawMessage.Recipients)),
zap.String("messageID", messageID.String()),
zap.String("messageType", "community"),
zap.Any("contentType", rawMessage.MessageType),
zap.Strings("hashes", types.EncodeHexes(hashes)))
s.transport.Track(messageID, hashes, newMessages)
return messageID, nil
@ -470,7 +475,12 @@ func (s *MessageSender) sendPrivate(
return nil, errors.Wrap(err, "failed to send a message spec")
}
s.logger.Debug("sent private message skipProtocolLayer", zap.String("messageID", messageID.String()), zap.Strings("hashes", types.EncodeHexes(hashes)))
s.logger.Debug("sent-message: private skipProtocolLayer",
zap.Strings("recipient", PubkeysToHex(rawMessage.Recipients)),
zap.String("messageID", messageID.String()),
zap.String("messageType", "private"),
zap.Any("contentType", rawMessage.MessageType),
zap.Strings("hashes", types.EncodeHexes(hashes)))
s.transport.Track(messageID, hashes, newMessages)
} else {
@ -485,7 +495,12 @@ func (s *MessageSender) sendPrivate(
return nil, errors.Wrap(err, "failed to send a message spec")
}
s.logger.Debug("sent private message without datasync", zap.String("messageID", messageID.String()), zap.Strings("hashes", types.EncodeHexes(hashes)))
s.logger.Debug("sent-message: private without datasync",
zap.Strings("recipient", PubkeysToHex(rawMessage.Recipients)),
zap.String("messageID", messageID.String()),
zap.Any("contentType", rawMessage.MessageType),
zap.String("messageType", "private"),
zap.Strings("hashes", types.EncodeHexes(hashes)))
s.transport.Track(messageID, hashes, newMessages)
}
@ -737,7 +752,12 @@ func (s *MessageSender) SendPublic(
s.notifyOnSentMessage(sentMessage)
s.logger.Debug("sent public message", zap.String("messageID", messageID.String()), zap.Strings("hashes", types.EncodeHexes(hashes)))
s.logger.Debug("sent-message: public message",
zap.Strings("recipient", PubkeysToHex(rawMessage.Recipients)),
zap.String("messageID", messageID.String()),
zap.Any("contentType", rawMessage.MessageType),
zap.String("messageType", "public"),
zap.Strings("hashes", types.EncodeHexes(hashes)))
s.transport.Track(messageID, hashes, newMessages)
return messageID, nil