debug.go (1350B)
1 package state 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 const ( 9 unknown_flag_description = "?unreg?" 10 ) 11 12 type flagDebugger struct { 13 flagStrings map[uint32]string 14 } 15 16 func newFlagDebugger() flagDebugger { 17 fd := flagDebugger{ 18 flagStrings: make(map[uint32]string), 19 } 20 fd.register(FLAG_READIN, "INTERNAL_READIN") 21 fd.register(FLAG_INMATCH, "INTERNAL_INMATCH") 22 fd.register(FLAG_TERMINATE, "INTERNAL_TERMINATE") 23 fd.register(FLAG_DIRTY, "INTERNAL_DIRTY") 24 fd.register(FLAG_WAIT, "INTERNAL_WAIT") 25 fd.register(FLAG_LOADFAIL, "INTERNAL_LOADFAIL") 26 fd.register(FLAG_LANG, "INTERNAL_LANG") 27 return fd 28 } 29 30 func(fd *flagDebugger) register(flag uint32, name string) { 31 fd.flagStrings[flag] = name 32 } 33 34 func(fd *flagDebugger) Register(flag uint32, name string) error { 35 if (flag < 8) { 36 return fmt.Errorf("flag %v is not definable by user", flag) 37 } 38 fd.register(flag, name) 39 return nil 40 } 41 42 func(fd *flagDebugger) AsString(flags []byte, length uint32) string { 43 return strings.Join(fd.AsList(flags, length), ",") 44 } 45 46 func(fd *flagDebugger) AsList(flags []byte, length uint32) []string { 47 var r []string 48 var i uint32 49 for i = 0; i < length + 8; i++ { 50 if getFlag(i, flags) { 51 v, ok := fd.flagStrings[i] 52 if !ok { 53 v = unknown_flag_description 54 } 55 s := fmt.Sprintf("%s(%v)", v, i) 56 r = append(r, s) 57 } 58 } 59 return r 60 } 61 62 var ( 63 FlagDebugger = newFlagDebugger() 64 )