Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent committed Apr 9, 2022
1 parent d32ff05 commit 245d3f8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 35 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,18 @@ $ RecInfoJSON hello foo bar 2>&1 | jq .
```

```console
$ # RecExec
$ RecExec date +%F
$ # RecExecJSON
$ RecExecJSON date +%F
{"timestamp":"2021-11-27T20:25:03+09:00","severity":"INFO","caller":"-bash","message":"$ date +%F"} # <- NOTE: stderr
2021-11-25
```

```console
$ # RecRun
$ RecRun sh -c "echo out; echo err 1>&2; exit 1"
$ # RecRunJSON
$ RecRunJSON sh -c "echo out; echo err 1>&2; exit 1"
{"timestamp":"2021-11-27T20:25:27+09:00","severity":"INFO","caller":"-bash","message":"$ date +%F","command":"date +%F","stdout":"2021-11-27","stderr":"","return":"1"} # <- NOTE: stderr

$ RecRun sh -c "echo out; echo err 1>&2; exit 1" 2>&1 | jq .
$ RecRunJSON sh -c "echo out; echo err 1>&2; exit 1" 2>&1 | jq .
{
"timestamp": "2021-11-27T20:26:46+09:00",
"severity": "INFO",
Expand Down
36 changes: 26 additions & 10 deletions benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ DisplayRuntimeInfo() {
echo
} && DisplayRuntimeInfo

BenchmarkRecDebug_001() {
echo "BenchmarkRecDebug_001"
printf %s 'RecDebug(message only) * 100'
time : "$(for _ in $(seq 1 100); do RecDebug "$(printf "\t\nA\t\nB\t\nC\t\n")"; done >/tmp/rec.sh_BenchmarkRecDebug_001.log 2>&1)"
BenchmarkRecDebugJSON_001() {
echo "BenchmarkRecDebugJSON_001"
printf %s 'RecDebugJSON(message only) * 100'
time : "$(for _ in $(seq 1 100); do RecDebugJSON "$(printf "\t\nA\t\nB\t\nC\t\n")"; done >/tmp/rec.sh_BenchmarkRecDebugJSON_001.log 2>&1)"
echo
} && BenchmarkRecDebug_001
} && BenchmarkRecDebugJSON_001

BenchmarkRecDebug_002() {
echo "BenchmarkRecDebug_002"
printf %s 'RecDebug(with 1 field) * 100'
time : "$(for _ in $(seq 1 100); do RecDebug "$(printf "\t\nA\t\nB\t\nC\t\n")" "test" "$(printf "\t\nA\t\nB\t\nC\t\n")"; done >/tmp/rec.sh_BenchmarkRecDebug_002.log 2>&1)"
BenchmarkRecDebugJSON_002() {
echo "BenchmarkRecDebugJSON_002"
printf %s 'RecDebugJSON(with 1 field) * 100'
time : "$(for _ in $(seq 1 100); do RecDebugJSON "$(printf "\t\nA\t\nB\t\nC\t\n")" "test" "$(printf "\t\nA\t\nB\t\nC\t\n")"; done >/tmp/rec.sh_BenchmarkRecDebugJSON_002.log 2>&1)"
echo
} && BenchmarkRecDebug_002
} && BenchmarkRecDebugJSON_002

# benchmark sample on commit 'init'
# -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -67,3 +67,19 @@ BenchmarkRecDebug_002() {
# user 0m1.154s
# sys 0m3.422s
# -------------------------------------------------------------------------------------------------
# 2022-04-09T07:30:54+00:00 [ INFO] $ sh -c 'uname -s -v -m; sysctl -a machdep.cpu.brand_string'
# Darwin Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64
# machdep.cpu.brand_string: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
#
# BenchmarkRecDebugJSON_001
# RecDebugJSON(message only) * 100
# real 0m1.890s
# user 0m0.730s
# sys 0m1.639s
#
# BenchmarkRecDebugJSON_002
# RecDebugJSON(with 1 field) * 100
# real 0m3.108s
# user 0m1.219s
# sys 0m2.855s
# -------------------------------------------------------------------------------------------------
43 changes: 23 additions & 20 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,40 @@
_recRFC3339() { date "+%Y-%m-%dT%H:%M:%S%z" | sed "s/\(..\)$/:\1/"; }
_recCmd() { for a in "$@"; do if echo "${a:-}" | grep -Eq "[[:blank:]]"; then printf "'%s' " "${a:-}"; else printf "%s " "${a:-}"; fi; done | sed "s/ $//"; }
# Color
RecDefault() { test "${REC_SEVERITY:-0 }" -gt 000 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;35m DEFAULT\\033[0m] \"\$0\"\"}" 1>&2; }
RecDebug() { test "${REC_SEVERITY:-0 }" -gt 100 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;34m DEBUG\\033[0m] \"\$0\"\"}" 1>&2; }
RecInfo() { test "${REC_SEVERITY:-0 }" -gt 200 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;32m INFO\\033[0m] \"\$0\"\"}" 1>&2; }
RecNotice() { test "${REC_SEVERITY:-0 }" -gt 300 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;36m NOTICE\\033[0m] \"\$0\"\"}" 1>&2; }
RecWarning() { test "${REC_SEVERITY:-0 }" -gt 400 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;33m WARNING\\033[0m] \"\$0\"\"}" 1>&2; }
RecError() { test "${REC_SEVERITY:-0 }" -gt 500 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;31m ERROR\\033[0m] \"\$0\"\"}" 1>&2; }
RecCritical() { test "${REC_SEVERITY:-0 }" -gt 600 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;1;31m CRITICAL\\033[0m] \"\$0\"\"}" 1>&2; }
RecAlert() { test "${REC_SEVERITY:-0 }" -gt 700 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;41m ALERT\\033[0m] \"\$0\"\"}" 1>&2; }
RecDefault() { test " ${REC_SEVERITY:-0}" -gt 000 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;35m DEFAULT\\033[0m] \"\$0\"\"}" 1>&2; }
RecDebug() { test " ${REC_SEVERITY:-0}" -gt 100 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;34m DEBUG\\033[0m] \"\$0\"\"}" 1>&2; }
RecInfo() { test " ${REC_SEVERITY:-0}" -gt 200 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;32m INFO\\033[0m] \"\$0\"\"}" 1>&2; }
RecNotice() { test " ${REC_SEVERITY:-0}" -gt 300 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;36m NOTICE\\033[0m] \"\$0\"\"}" 1>&2; }
RecWarning() { test " ${REC_SEVERITY:-0}" -gt 400 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;33m WARNING\\033[0m] \"\$0\"\"}" 1>&2; }
RecError() { test " ${REC_SEVERITY:-0}" -gt 500 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;31m ERROR\\033[0m] \"\$0\"\"}" 1>&2; }
RecCritical() { test " ${REC_SEVERITY:-0}" -gt 600 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;1;31m CRITICAL\\033[0m] \"\$0\"\"}" 1>&2; }
RecAlert() { test " ${REC_SEVERITY:-0}" -gt 700 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;41m ALERT\\033[0m] \"\$0\"\"}" 1>&2; }
RecEmergency() { test "${REC_SEVERITY:-0}" -gt 800 2>/dev/null || echo "$*" | awk "{print \"$(_recRFC3339) [\\033[0;1;41mEMERGENCY\\033[0m] \"\$0\"\"}" 1>&2; }
RecExec() { RecInfo "$ $(_recCmd "$@")" && "$@"; }
RecRun() { _dlm='####R#E#C#D#E#L#I#M#I#T#E#R####' _all=$({ _out=$("$@") && _rtn=$? || _rtn=$? && printf "\n%s" "${_dlm:?}${_out:-}" && return ${_rtn:-0}; } 2>&1) && _rtn=$? || _rtn=$? && _dlmno=$(echo "${_all:-}" | sed -n "/${_dlm:?}/=") && _cmd=$(_recCmd "$@") && _stdout=$(echo "${_all:-}" | tail -n +"${_dlmno:-1}" | sed "s/^${_dlm:?}//") && _stderr=$(echo "${_all:-}" | head -n "${_dlmno:-1}" | grep -v "^${_dlm:?}") && RecInfo "$ ${_cmd:-}" && RecInfo "${_stdout:-}" && { [ -z "${_stderr:-}" ] || RecWarning "${_stderr:-}"; } && return ${_rtn:-0}; }
# bash
# export functions for bash
# shellcheck disable=SC3045
echo "${SHELL-}" | grep -q bash$ && export -f _recRFC3339 _recCmd RecDefault RecDebug RecInfo RecWarning RecError RecCritical RecAlert RecEmergency RecExec RecRun
# JSON

# MIT License Copyright (c) 2021 newtstat https://github.com/rec-logger/rec.sh
# Common
_recRFC3339() { date "+%Y-%m-%dT%H:%M:%S%z" | sed "s/\(..\)$/:\1/"; }
_recCmd() { for a in "$@"; do if echo "${a:-}" | grep -Eq "[[:blank:]]"; then printf "'%s' " "${a:-}"; else printf "%s " "${a:-}"; fi; done | sed "s/ $//"; }
# JSON
_recEscape() { printf %s "${1:-}" | sed "s/\"/\\\"/g; s/\r/\\\r/g; s/\t/\\\t/g; s/$/\\\n/g" | tr -d "[:cntrl:]" | sed "s/\\\n$/\n/"; }
_recJSON() { _svr="${1:?}" _msg="$(_recEscape "${2:-}")" && unset _fld _val && shift 2 && for v in "$@"; do if [ "${_val:-}" ]; then _fld="${_fld:?}:\"$(_recEscape "${v:-}")\"" && unset _val && continue; fi && _fld="${_fld:-}${_fld:+,}\"${v:?"json key is not set"}\"" _val=1; done && test $(($# % 2)) = 1 && _fld="${_fld:?}:\"\"" || true && printf "{%s}\n" "\"${REC_TIMESTAMP_KEY:=timestamp}\":\"$(_recRFC3339)\",\"${REC_SEVERITY_KEY:=severity}\":\"${_svr:?}\",\"${REC_CALLER_KEY:=caller}\":\"$0\",\"${REC_MESSAGE_KEY:=message}\":\"${_msg:-}\"${_fld:+,}${_fld:-}"; }
RecDefaultJSON() { test "${REC_SEVERITY:-0 }" -gt 000 2>/dev/null || _recJSON DEFAULT "$@" 1>&2; }
RecDebugJSON() { test "${REC_SEVERITY:-0 }" -gt 100 2>/dev/null || _recJSON DEBUG "$@" 1>&2; }
RecInfoJSON() { test "${REC_SEVERITY:-0 }" -gt 200 2>/dev/null || _recJSON INFO "$@" 1>&2; }
RecNoticeJSON() { test "${REC_SEVERITY:-0 }" -gt 300 2>/dev/null || _recJSON NOTICE "$@" 1>&2; }
RecWarningJSON() { test "${REC_SEVERITY:-0 }" -gt 400 2>/dev/null || _recJSON WARNING "$@" 1>&2; }
RecErrorJSON() { test "${REC_SEVERITY:-0 }" -gt 500 2>/dev/null || _recJSON ERROR "$@" 1>&2; }
RecCriticalJSON() { test "${REC_SEVERITY:-0 }" -gt 600 2>/dev/null || _recJSON CRITICAL "$@" 1>&2; }
RecAlertJSON() { test "${REC_SEVERITY:-0 }" -gt 700 2>/dev/null || _recJSON ALERT "$@" 1>&2; }
_recJSON() { _svr="${1:?}" _msg="$(_recEscape "${2:-}")" && unset _fld _val && shift 2 && for a in "$@"; do if [ "${_val:-}" ]; then _fld="${_fld:?}:\"$(_recEscape "${a:-}")\"" && unset _val && continue; fi && _fld="${_fld:-}${_fld:+,}\"${a:?"json key is not set"}\"" _val=1; done && test $(($# % 2)) = 1 && _fld="${_fld:?}:\"\"" || true && printf "{%s}\n" "\"${REC_TIMESTAMP_KEY:-timestamp}\":\"$(_recRFC3339)\",\"${REC_SEVERITY_KEY:-severity}\":\"${_svr:?}\",\"${REC_CALLER_KEY:-caller}\":\"$0\",\"${REC_MESSAGE_KEY:-message}\":\"${_msg:-}\"${_fld:+,}${_fld:-}"; }
RecDefaultJSON() { test " ${REC_SEVERITY:-0}" -gt 000 2>/dev/null || _recJSON DEFAULT "$@" 1>&2; }
RecDebugJSON() { test " ${REC_SEVERITY:-0}" -gt 100 2>/dev/null || _recJSON DEBUG "$@" 1>&2; }
RecInfoJSON() { test " ${REC_SEVERITY:-0}" -gt 200 2>/dev/null || _recJSON INFO "$@" 1>&2; }
RecNoticeJSON() { test " ${REC_SEVERITY:-0}" -gt 300 2>/dev/null || _recJSON NOTICE "$@" 1>&2; }
RecWarningJSON() { test " ${REC_SEVERITY:-0}" -gt 400 2>/dev/null || _recJSON WARNING "$@" 1>&2; }
RecErrorJSON() { test " ${REC_SEVERITY:-0}" -gt 500 2>/dev/null || _recJSON ERROR "$@" 1>&2; }
RecCriticalJSON() { test " ${REC_SEVERITY:-0}" -gt 600 2>/dev/null || _recJSON CRITICAL "$@" 1>&2; }
RecAlertJSON() { test " ${REC_SEVERITY:-0}" -gt 700 2>/dev/null || _recJSON ALERT "$@" 1>&2; }
RecEmergencyJSON() { test "${REC_SEVERITY:-0}" -gt 800 2>/dev/null || _recJSON EMERGENCY "$@" 1>&2; }
RecExecJSON() { RecInfoJSON "$ $(_recCmd "$@")" && "$@"; }
RecRunJSON() { _dlm='####R#E#C#D#E#L#I#M#I#T#E#R####' _all=$({ _out=$("$@") && _rtn=$? || _rtn=$? && printf "\n%s" "${_dlm:?}${_out:-}" && return ${_rtn:-0}; } 2>&1) && _rtn=$? || _rtn=$? && _dlmno=$(echo "${_all:-}" | sed -n "/${_dlm:?}/=") && _cmd=$(_recCmd "$@") && _stdout=$(echo "${_all:-}" | tail -n +"${_dlmno:-1}" | sed "s/^${_dlm:?}//") && _stderr=$(echo "${_all:-}" | head -n "${_dlmno:-1}" | grep -v "^${_dlm:?}") && RecInfoJSON "$ ${_cmd:-}" command "${_cmd:-}" stdout "${_stdout:-}" stderr "${_stderr:-}" return "${_rtn:-0}" && return ${_rtn:-0}; }
# bash
# export functions for bash
# shellcheck disable=SC3045
echo "${SHELL-}" | grep -q bash$ && export -f _recRFC3339 _recCmd _recEscape _recJSON RecDefaultJSON RecDebugJSON RecInfoJSON RecNoticeJSON RecWarningJSON RecErrorJSON RecCriticalJSON RecAlertJSON RecEmergencyJSON RecExecJSON RecRunJSON

Expand Down

0 comments on commit 245d3f8

Please sign in to comment.