Skip to content

Commit ad21325

Browse files
committed
ostrace: fix --nojson flag and add ANSI color-coded output for plain text mode
1 parent d596a56 commit ad21325

1 file changed

Lines changed: 53 additions & 5 deletions

File tree

main.go

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"golang.org/x/crypto/pkcs12"
24+
"golang.org/x/sys/unix"
2425

2526
"github.com/danielpaulus/go-ios/ios/debugproxy"
2627
"github.com/danielpaulus/go-ios/ios/deviceinfo"
@@ -2773,6 +2774,13 @@ func runOsTrace(device ios.DeviceEntry, pid int, processName string, messageFilt
27732774
log.Debug("Run OsTrace.")
27742775
// Note: streaming log messages places significant CPU load on the device.
27752776

2777+
formatEntry := func(e ostrace.LogEntry) string {
2778+
return convertToJSONString(e)
2779+
}
2780+
if JSONdisabled {
2781+
formatEntry = formatEntryPlain
2782+
}
2783+
27762784
if processName != "" && pid == -1 {
27772785
service, err := instruments.NewDeviceInfoService(device)
27782786
exitIfError("failed opening deviceInfoService for resolving process name", err)
@@ -2795,11 +2803,7 @@ func runOsTrace(device ios.DeviceEntry, pid int, processName string, messageFilt
27952803
done <- err
27962804
return
27972805
}
2798-
if JSONdisabled {
2799-
fmt.Printf("%s %s %d %s\n", entry.Timestamp.Format(time.RFC3339Nano), entry.LevelName, entry.PID, entry.Message)
2800-
} else {
2801-
fmt.Println(convertToJSONString(entry))
2802-
}
2806+
fmt.Println(formatEntry(entry))
28032807
}
28042808
}()
28052809
c := make(chan os.Signal, 1)
@@ -2812,6 +2816,50 @@ func runOsTrace(device ios.DeviceEntry, pid int, processName string, messageFilt
28122816
}
28132817
}
28142818

2819+
func colorForLevel(level ostrace.LogLevel) string {
2820+
switch level {
2821+
case ostrace.LogLevelInfo:
2822+
return "\033[36m" // cyan
2823+
case ostrace.LogLevelDebug:
2824+
return "\033[90m" // bright black (gray)
2825+
case ostrace.LogLevelError:
2826+
return "\033[31m" // red
2827+
case ostrace.LogLevelFault:
2828+
return "\033[1;31m" // bold red
2829+
default:
2830+
return "" // no color
2831+
}
2832+
}
2833+
2834+
func isTerminal(fd int) bool {
2835+
_, err := unix.IoctlGetTermios(fd, unix.TCGETS)
2836+
return err == nil
2837+
}
2838+
2839+
func formatEntryPlain(entry ostrace.LogEntry) string {
2840+
ts := entry.Timestamp.Format("2006-01-02T15:04:05.000Z07:00")
2841+
useColor := isTerminal(int(os.Stdout.Fd()))
2842+
dim, reset, color := "", "", ""
2843+
if useColor {
2844+
dim = "\033[90m"
2845+
reset = "\033[0m"
2846+
color = colorForLevel(entry.Level)
2847+
}
2848+
if entry.Label != nil {
2849+
return fmt.Sprintf("%s%s%s %sPID:%-5d%s %s<%-7s>%s %s[%s:%s]%s %s%s%s",
2850+
dim, ts, reset,
2851+
dim, entry.PID, reset,
2852+
color, entry.LevelName, reset,
2853+
dim, entry.Label.Subsystem, entry.Label.Category, reset,
2854+
color, entry.Message, reset)
2855+
}
2856+
return fmt.Sprintf("%s%s%s %sPID:%-5d%s %s<%-7s>%s %s%s%s",
2857+
dim, ts, reset,
2858+
dim, entry.PID, reset,
2859+
color, entry.LevelName, reset,
2860+
color, entry.Message, reset)
2861+
}
2862+
28152863
func rawSyslog(log string) string {
28162864
return log
28172865
}

0 commit comments

Comments
 (0)