see_log_entry.go 1.4 KB

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