-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__debug
executable file
·89 lines (80 loc) · 2.81 KB
/
__debug
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env zsh
function __start_debug() {
export __DEBUG_FILE_NAME="${1:-$HOME/.__debug_${__debug_start_time}.log}"
echo ${__DEBUG_FILE_NAME:a} | tee $__debug_file_path_location > /dev/null
touch ${__DEBUG_FILE_NAME:a}
}
function __debug() {
local __debug_file
__debug_file=$(< $__debug_file_path_location)
if [[ $# -gt 1 ]]; then
__debug_file="${1:a}"
shift
fi
if [[ ${1:-} == "hr" ]]; then
echo "==================================================" | tee -a ${__debug_file:-$HOME/.__debug_${__debug_start_time}.log} > /dev/null && exit 0
fi
[[ ! -f ${__debug_file} ]] && touch ${__debug_file}
__timestamp=$(date +"%Y-%m-%d %H:%M:%S")
if [[ ${1:-} == "-" ]]; then
while read line; do
echo "[${__timestamp}] - $line" | tee -a ${__debug_file:-$HOME/.__debug_${__debug_start_time}.log} > /dev/null
done < /dev/stdin
return
fi
echo "[${__timestamp}] - $@" | tee -a ${__debug_file:-$HOME/.__debug_${__debug_start_time}.log} > /dev/null
}
function __tail_debug() {
local __debug_file
__debug_file=$(< $__debug_file_path_location)
if [[ $# -gt 1 ]]; then
__debug_file="${1:a}"
shift
fi
[[ -z ${__debug_file} ]] && echo "No debug file set. Try running __debug start first" && exit 1
echo "Waiting for ${__debug_file}"
tail -f ${__debug_file} -n 0
}
function __file_debug() {
echo ${__debug_file:-$HOME/.__debug_${__debug_start_time}.log}
}
function main() {
if [[ $# -eq 0 ]]; then
echo ""
echo " USAGE: __debug start [file.log]"
echo " __debug [file.log] \"Debug message\""
echo " __debug tail [file.log]"
echo " __debug file"
echo " "
echo " file.log is an optional argument. If you don't pass a file to write to, debug will"
echo " generate a file named after the pattern \$HOME/.__debug_\$__debug_start_time.log"
echo " "
echo " You can use __debug start and the following __debug calls will use the file set by"
echo " the start command. Or you can simply pass __debug file.log \"Debug message\""
echo ""
echo " This is not meant to be used as a full logger. This is useful to debug scripts"
echo " that may be running on the background or things like completion functions, for example"
exit 0
fi
touch $__debug_file_path_location
__debug_start_time=$(date +"%Y_%m_%d-%H%S")
case $1 in
(start)
shift
__start_debug "$@"
;;
(tail)
shift
__tail_debug "$@"
;;
(file)
shift
__file_debug
;;
*)
__debug "$@"
;;
esac
}
__debug_file_path_location="/tmp/__debug_file_name"
main "$@"