Castor-Gemini/format_log.go

65 lines
1.7 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"go.wit.com/lib/protobuf/chatpb"
"go.wit.com/log"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: go run format_log.go <path_to_log_file>\n")
os.Exit(1)
}
filename := os.Args[1]
data, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading file %s: %v", filename, err)
}
// Use the existing robust unmarshaler from the chatpb package.
logData, err := chatpb.UnmarshalChatsTEXT(data)
if err != nil {
log.Fatalf("Error unmarshaling log file %s: %v", filename, err)
}
// Print header information.
fmt.Printf("---\n")
fmt.Printf("Log File: %s\n", filename)
fmt.Printf("UUID: %s\n", logData.GetUuid())
fmt.Printf("Version: %s\n", logData.GetVersion())
fmt.Println(strings.Repeat("-", 40))
// Iterate through and format each chat entry.
for _, chat := range logData.GetChats() {
// Format the timestamp.
var formattedTime string
if ctime := chat.GetCtime(); ctime != nil {
// Convert protobuf timestamp to Go's time.Time.
t := ctime.AsTime()
formattedTime = t.Format("2006-01-02 15:04:05") // YYYY-MM-DD HH:MM:SS
} else {
formattedTime = "No Timestamp"
}
// Format the author. The .proto file defines an enum for this.
author := chat.GetFrom().String()
fmt.Printf("[%s] (%s):\n", author, formattedTime)
fmt.Printf("%s\n\n", chat.GetContent())
// Bonus: Handle tables if they exist in a chat message.
if table := chat.GetTable(); table != nil {
fmt.Println(" --- Table Data ---")
for _, row := range table.GetRows() {
fmt.Printf(" | %s |\n", strings.Join(row.GetFields(), " | "))
}
fmt.Println(" ------------------\n")
}
}
}