Add logging to pi-apps

pull/779/head
Botspot 3 years ago
parent 47355898fc
commit 81feed3662

@ -0,0 +1,71 @@
#!/bin/bash
DIRECTORY="$(readlink -f "$(dirname "$(dirname "$0")")")"
function error {
echo -e "\e[91m$1\e[39m"
exit 1
}
#remove week-old logfiles
find "$DIRECTORY/logs" -type f -mtime +7 -exec rm -f {} \;
#list of all files within "$DIRECTORY/logs", newest first
logfiles="$(ls "$DIRECTORY/logs"/* -t)"
if [ ! -z "$(date +%p)" ];then
#system locale uses AM and PM in time/dates
ampm=1
else
#date command didn't output anything - system locale uses 24-hour clock
ampm=0
fi
IFS=$'\n'
for file in $logfiles;do
#Parse various tidbits based on the filename
#$app: the name of the app for this logfile
app="$(echo "$(basename "$file")" | sed 's/^install-//g' | sed 's/^uninstall-//g' | sed 's/^incomplete-//g' | sed 's/^fail-//g' | sed 's/^success-//g' | sed 's/.log.*$//g')"
#$action: will be 'install' or 'uninstall'
action="$(echo "$(basename "$file")" | sed 's/-fail-//g' | sed 's/-success-//g' | sed 's/-incomplete-//g' | sed 's/'"$app"'.*$//g')"
#$result: will be 'success' or 'fail'
result="$(echo "$(basename "$file")" | sed 's/^install-//g' | sed 's/^uninstall-//g' | sed 's/-'"$app"'.*$//g')"
#$date: human-readable timestamp.
if [ $ampm == 1 ];then
#AM/PM timestamp: 'Friday 6:21 PM'
date="$(date -r "$file" '+%A %l:%M %p' | sed 's/ / /g' | sed "s/$(date +%A)/Today/g" | sed "s/$(date +%A --date=' 1 days ago')/Yesterday/g")"
else
#24h timestamp: 'Friday 18:21'
date="$(date -r "$file" '+%A %k:%M' | sed 's/ / /g' | sed "s/$(date +%A)/Today/g" | sed "s/$(date +%A --date=' 1 days ago')/Yesterday/g")"
fi
#echo "$app\n $action\n $result\n"
#add lines to the yad list
LIST="$LIST
$date
${DIRECTORY}/apps/${app}/icon-24.png
$(echo $action | sed "s+^uninstall$+${DIRECTORY}/icons/uninstall-short.png+g" | sed "s+^install$+${DIRECTORY}/icons/install-short.png+g")
$(echo $result | sed "s+^success$+${DIRECTORY}/icons/${action}ed.png+g" | sed "s+^fail$+${DIRECTORY}/icons/corrupted.png+g" | sed "s+^incomplete$+${DIRECTORY}/icons/corrupted.png+g")
$(echo $action | sed 's/^uninstall$/Uninstalling/g' | sed 's/^install$/Installing/g') $app $(echo $result | sed 's/success/succeeded./g' | sed 's/fail/failed./g' | sed 's/incomplete/was interrupted./g')
$file"
done
LIST="$(echo "$LIST" | grep .)"
echo "$LIST"
output="$(echo -e "$LIST" | yad --center --title='Log file viewer' --width=500 --height=400 \
--text="Review the errors from installing or uninstalling apps."$'\n'"Week-old log files will be deleted." \
--list --separator='\n' --window-icon="${DIRECTORY}/icons/logo.png" \
--column=Day --column=I:IMG --column=A:IMG --column=R:IMG --column=Description --column=tooltip:HD \
--print-column=6 --tooltip-column=6 --select-action="$(dirname "$0")/viewlog" \
--button='Delete all'!"${DIRECTORY}/icons/trash.png"!"Delete all log files from $DIRECTORY/logs":0 \
--button=Close!"${DIRECTORY}/icons/exit.png":1
)"
button=$?
if [ $button == 0 ];then
rm -rf "$DIRECTORY/logs"
mkdir "$DIRECTORY/logs"
echo "Deleted everything inside of $DIRECTORY/logs"
fi

@ -0,0 +1,26 @@
#!/bin/bash
DIRECTORY="$(readlink -f "$(dirname "$(dirname "$0")")")"
file="$6"
echo "$file" > "$DIRECTORY/data/current-viewed-logfile"
if command -v mousepad ;then
mousepad "$file" &
pid=$!
elif command -v leafpad ;then
leafpad "$file" &
pid=$!
else
#for text_editor function
source "${DIRECTORY}/api"
text_editor "$file" &
pid=$!
fi
while [ "$(cat "$DIRECTORY/data/current-viewed-logfile")" == "$file" ];do
sleep 1
done
kill $pid 2>/dev/null

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

@ -14,6 +14,14 @@ if [ -z "$1" ];then
error "You need to specify an operation, and in most cases, which app to operate on."
fi
rm_esc() { #remove ANSI escape sequences from a given file
[ -z "$1" ] && error "rm_esc: no filename given!"
[ ! -f "$1" ] && error "rm_esc: given filename ($1) does not exist or is not a file!"
sed -i 's/\x1b\[[0-9;]*m//g' "$1"
}
source "${DIRECTORY}/api" || error "failed to source ${DIRECTORY}/api"
mkdir -p "${DIRECTORY}/data/status" "${DIRECTORY}/data/update-status"
@ -139,21 +147,45 @@ elif [ "$1" == 'install' ];then
installscript="${DIRECTORY}/apps/${app}/${scriptname}"
[ -z "$scriptname" ] && error "It appears $app does not have an install-${arch} script suitable for your ${arch}-bit OS."
echo -e "\e[96mInstalling $app with $scriptname script\e[39m"
#determine path for log file to be created
logfile="${DIRECTORY}/logs/install-incomplete-${app}.log"
if [ -f "$logfile" ] || [ -f "$(echo "$logfile" | sed 's+/-incomplete-+/-success-+g')" ] || [ -f "$(echo "$logfile" | sed 's+/-incomplete-+/-fail-+g')" ];then
#append a number to logfile's file-extension if the original filename already exists
i=1
while true;do
#if variable $i is 2, then example newlogfile value: /path/to/install-Discord.log2
newlogfile="$logfile$i"
if [ ! -f "$newlogfile" ] && [ ! -f "$(echo "$newlogfile" | sed 's+/-incomplete-+/-success-+g')" ] && [ ! -f "$(echo "$newlogfile" | sed 's+/-incomplete-+/-fail-+g')" ];then
logfile="${newlogfile}"
break
fi
i=$((i+1))
done
fi
#for piping to tee, the install script's exit code is preserved by enabling the pipefail bash builtin
set -o pipefail
echo -e "\e[96mInstalling $app with $scriptname script\e[39m" | tee -a "$logfile"
cd $HOME
echo 'corrupted' > "${DIRECTORY}/data/status/${app}"
if nice "$installscript" ; then
if nice "$installscript" 2>&1 | tee -a "$logfile" ; then
echo 'installed' > "${DIRECTORY}/data/status/${app}"
echo -e "\n\e[42m\e[30mInstalled ${app} successfully.\e[39m\e[49m"
echo -e "\n\e[42m\e[30mInstalled ${app} successfully.\e[39m\e[49m" | tee -a "$logfile"
rm_esc "$logfile" #remove escape sequences from logfile
mv "$logfile" "$(echo "$logfile" | sed 's+-incomplete-+-success-+g')" #
exit 0
else
echo -e "\n\e[41m\e[30mFailed to install ${app} with $scriptname script"\!"\e[39m\e[49m
\e[40m\e[93m\e[5m▲\e[25m\e[39m \e[49m\e[93mNeed help? Copy the above error output or take a screenshot.
Please ask on Github: \e[94m\e[4mhttps://github.com/Botspot/pi-apps/issues/new/choose\e[24m\e[93m
Or on Discord: \e[94m\e[4mhttps://discord.gg/RXSTvaUvuu\e[24m\e[39m"
Or on Discord: \e[94m\e[4mhttps://discord.gg/RXSTvaUvuu\e[24m\e[39m" | tee -a "$logfile"
rm_esc "$logfile" #remove escape sequences from logfile
mv "$logfile" "$(echo "$logfile" | sed 's+-incomplete-+-fail-+g')"
exit 1
fi
elif [ "$1" == 'uninstall' ];then
#UNINSTALL
#for this operation, a program name must be specified.
@ -174,15 +206,37 @@ elif [ "$1" == 'uninstall' ];then
#analytics
"${DIRECTORY}/etc/bitlylink" "$app" uninstall
echo -e "\e[96mUninstalling $app\e[39m"
#determine path for log file to be created
logfile="${DIRECTORY}/logs/uninstall-incomplete-${app}.log"
if [ -f "$logfile" ] || [ -f "$(echo "$logfile" | sed 's+/-incomplete-+/-success-+g')" ] || [ -f "$(echo "$logfile" | sed 's+/-incomplete-+/-fail-+g')" ];then
#append a number to logfile's file-extension if the original filename already exists
i=1
while true;do
#if variable $i is 2, then example newlogfile value: /path/to/install-Discord.log2
newlogfile="$logfile$i"
if [ ! -f "$newlogfile" ] && [ ! -f "$(echo "$newlogfile" | sed 's+/-incomplete-+/-success-+g')" ] && [ ! -f "$(echo "$newlogfile" | sed 's+/-incomplete-+/-fail-+g')" ];then
logfile="${newlogfile}"
break
fi
i=$((i+1))
done
fi
#for piping to tee, the install script's exit code is preserved by enabling the pipefail bash builtin
set -o pipefail
echo -e "\e[96mUninstalling $app\e[39m" | tee -a "$logfile"
cd $HOME
echo 'corrupted' > "${DIRECTORY}/data/status/${app}"
if "${DIRECTORY}/apps/${app}/uninstall" ; then
if nice "${DIRECTORY}/apps/${app}/uninstall" 2>&1 | tee -a "$logfile"; then
echo 'uninstalled' > "${DIRECTORY}/data/status/${app}"
echo -e "\n\e[42m\e[30mUninstalled ${app} successfully.\e[39m\e[49m"
echo -e "\n\e[42m\e[30mUninstalled ${app} successfully.\e[39m\e[49m" | tee -a "$logfile"
rm_esc "$logfile" #remove escape sequences from logfile
mv "$logfile" "$(echo "$logfile" | sed 's+-incomplete-+-success-+g')"
exit 0
else
echo -e "\n\e[41m\e[30mFailed to uninstall ${app}!\e[39m\e[49m"
echo -e "\n\e[41m\e[30mFailed to uninstall ${app}!\e[39m\e[49m" | tee -a "$logfile"
rm_esc "$logfile" #remove escape sequences from logfile
mv "$logfile" "$(echo "$logfile" | sed 's+-incomplete-+-fail-+g')"
exit 1
fi
elif [ "$1" == 'update' ];then

@ -105,6 +105,7 @@ ${params}"
--form --separator='\n' --window-icon="${DIRECTORY}/icons/logo.png" --columns=2 \
$tooltips \
--field='Categories...'!"${DIRECTORY}/icons/categories.png":FBTN "bash -c '${DIRECTORY}/etc/categoryedit &>/dev/null'" \
--field='Log files...'!"${DIRECTORY}/icons/log-file.png":FBTN "bash -c '${DIRECTORY}/etc/logviewer &>/dev/null'" \
$yadparams \
--field='New App...'!"${DIRECTORY}/icons/create.png":FBTN "bash -c '${DIRECTORY}/createapp &>/dev/null'" \
--button='Reset'!"${DIRECTORY}/icons/backup.png"!'Reset all settings to their defaults':2 \

Loading…
Cancel
Save