-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.sh
50 lines (44 loc) · 1.2 KB
/
utils.sh
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
#!/bin/bash
# change to disable stack logs
export PRINT_LOG_STACK=1 # [ 0 , 1 ]
function log_stack() {
if [[ $PRINT_LOG_STACK == 1 ]];then
local cmd=${FUNCNAME[1]}
local file=${BASH_SOURCE[1]}
if [[ $# > 0 ]]; then cmd="$@"; fi
local prefix="$(date '+%Y-%m-%d %H:%M:%S')"
awk -v file="$file" -v date="$prefix" -v line=${BASH_LINENO[1]} -v funct=$cmd '
BEGIN {
printf "\033[0;34m%-20s\033[0;33m%-10s\033[0;36m%-4s\033[0;31m%-19s\n", date, file, line, funct;
}
'
fi
}
function log() {
local FILE=${BASH_SOURCE[1]}
local DATE=$(date +"%Y-%m-%d %H:%M:%S")
local LINE=$BASH_LINENO
local FUNC=${FUNCNAME[1]}
awk -v file="$FILE" -v date="$DATE" -v line=$LINE -v funct=$FUNC -v logx="$1" '
BEGIN {
printf "\033[0;34m%-20s\033[0;33m%-10s\033[0;36m%-4s\033[0;31m%-19s\033[0m%-50s\n", date, file, line, funct, logx;
}
'
}
function hex2dec() {
hex=${@#0x}
echo "obase=10; ibase=16; ${hex^^}" | bc
}
function requireFile() {
local errorMsg=$2
if [ ! -f "$1" ]; then
log $errorMsg
fi
}
function require_contract_addr() {
local addr=$1
if ! [[ $1 =~ ^cx[a-fA-F0-9]{40}$ ]]; then
log "invalid contract address $addr"
exit 1
fi
}