tools: don't mix print(end="") with printb()
While mixing print(end="") with printb(), some messages may miss due to
the underlying buffer handling in python 3.
For example:
# python3 opensnoop.py
PID COMM FD ERR PATH
/proc/18849/cmdline
4109 tmux: server 67 0 /proc/18849/cmdline
4109 tmux: server 67 0 /proc/18849/cmdline
4109 tmux: server 67 0 /proc/18849/cmdline
The PID, COMM, FD, and ERR are printed with print(end=""), and those of
the first instance was eaten by printb() which outputs PATH.
The following scripts mix print(end="") and printb() for the same line:
tools/execsnoop.py
tools/opensnoop.py
tools/tcpaccept.py
tools/tcpconnect.py
Those scripts work fine with python 2 but some messages may miss while
using python 3.
This commit converts print(end="") to printb(nl="") to avoid the
inconsistent outputs.
Signed-off-by: Gary Lin <glin@suse.com>
diff --git a/tools/tcpaccept.py b/tools/tcpaccept.py
index 70202dd..914d518 100755
--- a/tools/tcpaccept.py
+++ b/tools/tcpaccept.py
@@ -196,11 +196,11 @@
event = b["ipv4_events"].event(data)
global start_ts
if args.time:
- print("%-9s" % strftime("%H:%M:%S"), end="")
+ printb(b"%-9s" % strftime("%H:%M:%S").encode('ascii'), nl="")
if args.timestamp:
if start_ts == 0:
start_ts = event.ts_us
- print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="")
+ printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="")
printb(b"%-7d %-12.12s %-2d %-16s %-5d %-16s %-5d" % (event.pid,
event.task, event.ip,
inet_ntop(AF_INET, pack("I", event.daddr)).encode(),
@@ -212,11 +212,11 @@
event = b["ipv6_events"].event(data)
global start_ts
if args.time:
- print("%-9s" % strftime("%H:%M:%S"), end="")
+ printb(b"%-9s" % strftime("%H:%M:%S").encode('ascii'), nl="")
if args.timestamp:
if start_ts == 0:
start_ts = event.ts_us
- print("%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), end="")
+ printb(b"%-9.3f" % ((float(event.ts_us) - start_ts) / 1000000), nl="")
printb(b"%-7d %-12.12s %-2d %-16s %-5d %-16s %-5d" % (event.pid,
event.task, event.ip,
inet_ntop(AF_INET6, event.daddr).encode(),