-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugtoggle.sh
More file actions
executable file
·54 lines (47 loc) · 2.13 KB
/
debugtoggle.sh
File metadata and controls
executable file
·54 lines (47 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Toggle network debug logging in Acubiz iOS project
set -e
case "$1" in
on|off|status|toggle) ACTION="$1"; PROJECT_ROOT="${2:-.}" ;;
*) PROJECT_ROOT="${1:-.}"; ACTION="${2:-toggle}" ;;
esac
FILE="$PROJECT_ROOT/Acubiz/Acubiz/Networking/ACBApiManager.swift"
MARKER="// DEBUG_NETWORK_LOGGING"
is_on() { grep -q "$MARKER" "$FILE" 2>/dev/null; }
LOGGER_CODE='// DEBUG_NETWORK_LOGGING
class ACBNetworkLogger: EventMonitor {
func requestDidResume(_ request: Request) {
guard let r = request.request else { return }
print("[REQ] \(r.httpMethod ?? "") \(r.url?.absoluteString ?? "")")
if let body = r.httpBody, let s = String(data: body, encoding: .utf8) {
print("Body: \(s.count > 500 ? String(s.prefix(500)) + "..." : s)")
}
}
func request<V>(_ request: DataRequest, didParseResponse response: DataResponse<V, AFError>) {
print("[RES] \(response.response?.statusCode ?? 0) \(request.request?.url?.absoluteString ?? "")")
if let data = response.data, let s = String(data: data, encoding: .utf8) {
print("Body: \(s.count > 1000 ? String(s.prefix(1000)) + "..." : s)")
}
}
}
// END_DEBUG_NETWORK_LOGGING'
enable() {
[ ! -f "$FILE" ] && echo "Error: $FILE not found" && exit 1
is_on && echo "Already on" && return
LINE=$(grep -n "^import Alamofire$" "$FILE" | head -1 | cut -d: -f1)
{ head -n "$LINE" "$FILE"; echo "$LOGGER_CODE"; tail -n +$((LINE + 1)) "$FILE"; } > "$FILE.tmp" && mv "$FILE.tmp" "$FILE"
sd -F 'Alamofire.Session(configuration: configuration)' 'Alamofire.Session(configuration: configuration, eventMonitors: [ACBNetworkLogger()])' "$FILE"
echo "✅ ON"
}
disable() {
is_on || { echo "Already off"; return; }
sd -f ms '\n// DEBUG_NETWORK_LOGGING.*?// END_DEBUG_NETWORK_LOGGING' '' "$FILE"
sd -F 'Alamofire.Session(configuration: configuration, eventMonitors: [ACBNetworkLogger()])' 'Alamofire.Session(configuration: configuration)' "$FILE"
echo "✅ OFF"
}
case "$ACTION" in
on) enable ;;
off) disable ;;
status) is_on && echo "✅ ON" || echo "❌ OFF" ;;
toggle) is_on && disable || enable ;;
esac