You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.5 KiB

5 years ago
5 years ago
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "google.golang.org/protobuf/proto"
  9. "github.com/seaweedfs/seaweedfs/weed/filer"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  13. )
  14. var (
  15. logdataFile = flag.String("logdata", "", "log data file saved under "+filer.SystemLogDir)
  16. )
  17. func main() {
  18. flag.Parse()
  19. util_http.InitGlobalHttpClient()
  20. dst, err := os.OpenFile(*logdataFile, os.O_RDONLY, 0644)
  21. if err != nil {
  22. log.Fatalf("failed to open %s: %v", *logdataFile, err)
  23. }
  24. defer dst.Close()
  25. err = walkLogEntryFile(dst)
  26. if err != nil {
  27. log.Fatalf("failed to visit %s: %v", *logdataFile, err)
  28. }
  29. }
  30. func walkLogEntryFile(dst *os.File) error {
  31. sizeBuf := make([]byte, 4)
  32. for {
  33. if n, err := dst.Read(sizeBuf); n != 4 {
  34. if err == io.EOF {
  35. return nil
  36. }
  37. return err
  38. }
  39. size := util.BytesToUint32(sizeBuf)
  40. data := make([]byte, int(size))
  41. if n, err := dst.Read(data); n != len(data) {
  42. return err
  43. }
  44. logEntry := &filer_pb.LogEntry{}
  45. err := proto.Unmarshal(data, logEntry)
  46. if err != nil {
  47. log.Printf("unexpected unmarshal filer_pb.LogEntry: %v", err)
  48. return nil
  49. }
  50. event := &filer_pb.SubscribeMetadataResponse{}
  51. err = proto.Unmarshal(logEntry.Data, event)
  52. if err != nil {
  53. log.Printf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
  54. return nil
  55. }
  56. fmt.Printf("event: %+v\n", event)
  57. }
  58. }