Skip to content

Commit

Permalink
Review log,flock functions
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Jan 8, 2017
1 parent 41d7983 commit f296661
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 50 deletions.
6 changes: 4 additions & 2 deletions base/core/core.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ __zplug::core::core::variable()
"defer_3_plugin" "$ZPLUG_CACHE_DIR/defer_3_plugin.zsh"
)

typeset -gx -a _zplug_checkout_locks
_zplug_checkout_locks=()
typeset -gx -A _zplug_lock
_zplug_lock=(
"job" "$ZPLUG_HOME/log/job.lock"
)

if (( $+ZPLUG_SHALLOW )); then
__zplug::io::print::f \
Expand Down
2 changes: 1 addition & 1 deletion base/core/load.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ __zplug::core::load::from_cache()
zstyle -s ':zplug:core:load' 'verbose' is_verbose

# Default
setopt monitor
# setopt monitor

__zplug::core::cache::update

Expand Down
14 changes: 9 additions & 5 deletions base/job/handle.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ __zplug::job::handle::flock()
local is_escape=false
local -a args

local file contents

while (( $#argv > 0 ))
do
case "$argv[1]" in
Expand All @@ -13,7 +15,7 @@ __zplug::job::handle::flock()
-*|--*)
;;
*)
args+=("$argv[1]")
args+=( "$argv[1]" )
;;
esac
shift
Expand All @@ -23,7 +25,8 @@ __zplug::job::handle::flock()
return 1
fi

local file="$args[1]" contents="$args[2]"
file="$args[1]"
contents="$args[2]"

# TODO: Temporary fix to solve #334
if [[ ! -f $file ]]; then
Expand Down Expand Up @@ -53,11 +56,10 @@ __zplug::job::handle::flock()

# Save the status code with LTSV
if $is_escape; then
__zplug::io::print::f "${(q)contents}\n"
print -r "$contents" >>|"$file"
else
__zplug::io::print::f "$contents\n"
print "$contents" >>|"$file"
fi >>|"$file"

)
}

Expand Down Expand Up @@ -246,6 +248,7 @@ __zplug::job::handle::hook()
} & hook_pids[$repo]=$!
# Run the timeout process in background
{
touch "$_zplug_lock[job]"
# kill the process for hook-build after sleeping
# during the number of seconds that has been set as a timeout
sleep "$timeout"
Expand All @@ -257,6 +260,7 @@ __zplug::job::handle::hook()
builtin printf "$repo\n" >>|"$_zplug_build_log[timeout]"
builtin printf "$repo\n" >>|"$_zplug_build_log[rollback]"
fi
rm -f "$_zplug_lock[job]"
} &
fi

Expand Down
19 changes: 13 additions & 6 deletions base/job/polling.zsh
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#export PERIOD=10
export PERIOD=30

__zplug::job::polling::finalizer()
__zplug::job::polling::periodic()
{
# Display the corsor
tput cnorm
# TODO
if [[ -f $_zplug_lock[job] ]]; then
setopt nomonitor
else
if [[ -o monitor ]]; then
return 0
fi
if setopt monitor; then
__zplug::log::write::info "turn monitor on"
fi
fi
}

#add-zsh-hook periodic __zplug::job::polling::finalizer
add-zsh-hook periodic __zplug::job::polling::periodic
17 changes: 15 additions & 2 deletions base/log/capture.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ __zplug::log::capture::error()

__zplug::job::handle::flock --escape \
"$_zplug_log[trace]" \
"$(__zplug::log::format::with_json --level "ERROR" --message "$message" "${argv[@]:-}")"
"$(__zplug::log::format::with_json "ERROR" "$message")"
}

__zplug::log::capture::debug()
Expand All @@ -21,5 +21,18 @@ __zplug::log::capture::debug()

__zplug::job::handle::flock --escape \
"$_zplug_log[trace]" \
"$(__zplug::log::format::with_json --level "DEBUG" --message "$message" "${argv[@]:-}")"
"$(__zplug::log::format::with_json "DEBUG" "$message")"
}

__zplug::log::capture::info()
{
local message="$(<&0)"

if [[ -z $message ]]; then
return 0
fi

__zplug::job::handle::flock --escape \
"$_zplug_log[trace]" \
"$(__zplug::log::format::with_json "INFO" "$message")"
}
48 changes: 16 additions & 32 deletions base/log/format.zsh
Original file line number Diff line number Diff line change
@@ -1,48 +1,32 @@
__zplug::log::format::with_json()
{
local -i i=1
local level="${1:-"INFO"}" message
local level="${1:-"INFO"}" message="$2"
local is_message_json=false

while (( $#argv > 0 ))
do
case "$argv[1]" in
--message)
message="$argv[2]"; shift
;;
--level)
level="$argv[2]"; shift
;;
--message-json)
is_message_json=true
;;
esac
shift
done

# Spit out to JSON
printf '{'
printf '"pid":%d,' "$$"
printf '"shlvl":%d,' "$SHLVL"
printf '"level":"%s",' "$level"
printf '"dir":"%s",' "$PWD"
printf '"message":'
builtin printf '{'
builtin printf '"pid":%d,' "$$"
builtin printf '"shlvl":%d,' "$SHLVL"
builtin printf '"level":"%s",' "$level"
builtin printf '"dir":"%s",' "$PWD"
builtin printf '"message":'
if $is_message_json; then
printf "$message"
builtin printf "$message"
else
printf "$message" \
builtin printf "$message" \
| __zplug::utils::ansi::remove \
| __zplug::utils::shell::json_escape \
| tr -d '\n'
fi
printf ','
printf '"trace":['
builtin printf ','
builtin printf '"trace":['
for ((i = 1; i < $#functrace; i++))
do
printf '"%s",' "$functrace[$i]"
builtin printf '"%s",' "$functrace[$i]"
done
printf '"%s"' "$functrace[$#functrace]"
printf "],"
printf '"date":"%s"' "$(strftime "%FT%T%z" $EPOCHSECONDS)"
printf "}\n"
builtin printf '"%s"' "$functrace[$#functrace]"
builtin printf "],"
builtin printf '"date":"%s"' "$(strftime "%FT%T%z" $EPOCHSECONDS)"
builtin printf "}\n"
}
10 changes: 10 additions & 0 deletions base/log/print.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ __zplug::log::print::error()
"$argv[2,-1]"
}

__zplug::log::print::debug()
{
# No problem since this function ignores
# unrelated arguments passed
__zplug::log::format::with_json \
--level "DEBUG" \
--message "$argv[1]" \
"$argv[2,-1]"
}

__zplug::log::print::info()
{
# No problem since this function ignores
Expand Down
11 changes: 9 additions & 2 deletions base/log/write.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ __zplug::log::write::error()
{
__zplug::job::handle::flock --escape \
"$_zplug_log[trace]" \
"$(__zplug::log::print::error "${argv[@]:-}")"
"$(__zplug::log::format::with_json "ERROR" "$argv[@]")"
}

__zplug::log::write::debug()
{
__zplug::job::handle::flock --escape \
"$_zplug_log[trace]" \
"$(__zplug::log::format::with_json "DEBUG" "$argv[@]")"
}

__zplug::log::write::info()
{
__zplug::job::handle::flock --escape \
"$_zplug_log[trace]" \
"$(__zplug::log::print::info "${argv[@]:-}")"
"$(__zplug::log::format::with_json "INFO" "$argv[@]")"
}

0 comments on commit f296661

Please sign in to comment.