Dual-mode packet formatter. When converted to str (e.g. by logger.error(), raise, f-string, or print()), produces the full hexdump + packet.show() output — identical to the old ppp(). The brief() method produces a compact summary with raw hex bytes that can be expanded later by tes
| 35 | |
| 36 | |
| 37 | class PacketInfo: |
| 38 | """Dual-mode packet formatter. |
| 39 | |
| 40 | When converted to str (e.g. by logger.error(), raise, f-string, or print()), |
| 41 | produces the full hexdump + packet.show() output — identical to the old ppp(). |
| 42 | |
| 43 | The brief() method produces a compact summary with raw hex bytes that can be |
| 44 | expanded later by test/scripts/expand_ppp.py. |
| 45 | |
| 46 | The VPP test logger patches logger.debug() to call brief() automatically, |
| 47 | so debug-level logging is ~9x faster while error-level logging retains full |
| 48 | detail with zero changes to any of the 401 call sites. |
| 49 | """ |
| 50 | |
| 51 | __slots__ = ("headline", "packet") |
| 52 | |
| 53 | def __init__(self, headline, packet): |
| 54 | self.headline = headline |
| 55 | self.packet = packet |
| 56 | |
| 57 | def __str__(self): |
| 58 | """Full output — used by logger.error(), raise, str(), f-string, etc.""" |
| 59 | return "%s\n%s\n\n%s\n" % ( |
| 60 | self.headline, |
| 61 | hexdump(self.packet, dump=True), |
| 62 | self.packet.show(dump=True), |
| 63 | ) |
| 64 | |
| 65 | def brief(self): |
| 66 | """Summary + raw hex — used by logger.debug() via patched logger. |
| 67 | |
| 68 | Format: headline summary |
| 69 | [packet hex: __class__.__name__=ClassName: deadbeef...] |
| 70 | |
| 71 | The hex encoding is lossless — expand_ppp.py can reconstruct the full |
| 72 | hexdump + show() output from the class name and raw bytes. |
| 73 | """ |
| 74 | return "%s %s\n [packet hex: __class__.__name__=%s: %s]\n" % ( |
| 75 | self.headline, |
| 76 | self.packet.summary(), |
| 77 | self.packet.__class__.__name__, |
| 78 | bytes(self.packet).hex(), |
| 79 | ) |
| 80 | |
| 81 | |
| 82 | def ppp(headline, packet): |