|
| 1 | +#!/usr/bin/env fish |
| 2 | + |
| 3 | +# Dump a GHC RTS .eventlog to a plain-text file using the ghc-events CLI. |
| 4 | +# Usage: |
| 5 | +# scripts/eventlog-dump.fish <file.eventlog> [output.txt] [starts_with_prefix] [contains_substring] |
| 6 | +# |
| 7 | +# Notes: |
| 8 | +# - Attempts to find ghc-events in PATH, ~/.cabal/bin, or ~/.local/bin. |
| 9 | +# - If not found, will try: cabal install ghc-events |
| 10 | +# - Output defaults to <basename>.events.txt in the current directory. |
| 11 | + |
| 12 | +function usage |
| 13 | + echo "Usage: (basename (status filename)) <file.eventlog> [output.txt] [starts_with_prefix] [contains_substring]" |
| 14 | + exit 2 |
| 15 | +end |
| 16 | + |
| 17 | +if test (count $argv) -lt 1 |
| 18 | + usage |
| 19 | +end |
| 20 | + |
| 21 | +set evlog $argv[1] |
| 22 | +if not test -f $evlog |
| 23 | + echo "error: file not found: $evlog" >&2 |
| 24 | + exit 1 |
| 25 | +end |
| 26 | + |
| 27 | +if test (count $argv) -ge 2 |
| 28 | + set out $argv[2] |
| 29 | +else |
| 30 | + set base (basename $evlog) |
| 31 | + if string match -q '*\.eventlog' $base |
| 32 | + set out (string replace -r '\\.eventlog$' '.events.txt' -- $base) |
| 33 | + else |
| 34 | + set out "$base.events.txt" |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +# Optional prefix filter: only keep lines that start with this string |
| 39 | +set filter_prefix "" |
| 40 | +if test (count $argv) -ge 3 |
| 41 | + set filter_prefix $argv[3] |
| 42 | +end |
| 43 | + |
| 44 | +# Optional contains filter: only keep lines that contain this substring (applied after prefix filter if both provided) |
| 45 | +set filter_contains "" |
| 46 | +if test (count $argv) -ge 4 |
| 47 | + set filter_contains $argv[4] |
| 48 | +end |
| 49 | + |
| 50 | +function find_ghc_events --description "echo absolute path to ghc-events or empty" |
| 51 | + if command -sq ghc-events |
| 52 | + command -s ghc-events |
| 53 | + return 0 |
| 54 | + end |
| 55 | + if test -x ~/.cabal/bin/ghc-events |
| 56 | + echo ~/.cabal/bin/ghc-events |
| 57 | + return 0 |
| 58 | + end |
| 59 | + if test -x ~/.local/bin/ghc-events |
| 60 | + echo ~/.local/bin/ghc-events |
| 61 | + return 0 |
| 62 | + end |
| 63 | + return 1 |
| 64 | +end |
| 65 | + |
| 66 | +set ghc_events_bin (find_ghc_events) |
| 67 | + |
| 68 | +if test -z "$ghc_events_bin" |
| 69 | + echo "ghc-events not found; attempting to install via 'cabal install ghc-events'..." >&2 |
| 70 | + if not command -sq cabal |
| 71 | + echo "error: cabal not found; please install ghc-events manually (e.g., via cabal)." >&2 |
| 72 | + exit 1 |
| 73 | + end |
| 74 | + cabal install ghc-events |
| 75 | + set ghc_events_bin (find_ghc_events) |
| 76 | + if test -z "$ghc_events_bin" |
| 77 | + echo "error: ghc-events still not found after installation." >&2 |
| 78 | + exit 1 |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +echo "Dumping events from $evlog to $out..." |
| 83 | +set -l stream_cmd "$ghc_events_bin show $evlog" |
| 84 | + |
| 85 | +if test -n "$filter_prefix" -o -n "$filter_contains" |
| 86 | + # Stream through filters |
| 87 | + eval $stream_cmd | while read -l line |
| 88 | + set -l keep 1 |
| 89 | + if test -n "$filter_prefix" |
| 90 | + if not string match -q -- "$filter_prefix*" -- $line |
| 91 | + set keep 0 |
| 92 | + end |
| 93 | + end |
| 94 | + if test $keep -eq 1 -a -n "$filter_contains" |
| 95 | + if not string match -q -- "*$filter_contains*" -- $line |
| 96 | + set keep 0 |
| 97 | + end |
| 98 | + end |
| 99 | + if test $keep -eq 1 |
| 100 | + echo $line |
| 101 | + end |
| 102 | + end > $out |
| 103 | +else |
| 104 | + eval $stream_cmd > $out |
| 105 | +end |
| 106 | +set exit_code $status |
| 107 | + |
| 108 | +if test $exit_code -ne 0 |
| 109 | + echo "error: dump failed with exit code $exit_code" >&2 |
| 110 | + exit $exit_code |
| 111 | +end |
| 112 | + |
| 113 | +set -l size "" |
| 114 | +if command -sq stat |
| 115 | + # macOS stat prints size with -f%z; suppress errors if not supported |
| 116 | + set size (stat -f%z $out 2>/dev/null) |
| 117 | +end |
| 118 | +if test -z "$size" |
| 119 | + set size (wc -c < $out) |
| 120 | +end |
| 121 | + |
| 122 | +echo "Wrote $out ($size bytes)." |
0 commit comments