Permalink
Cannot retrieve contributors at this time
#!/bin/bash | |
version() | |
{ | |
cat <<-EndVersion | |
Logger-TXT | |
Version 2.0 | |
Author: Grant Lucas (contact@grantlucas.com) | |
Last updated: 11/02/2014 | |
Release date: 26/07/2010 | |
License: GPL, http://www.gnu.org/copyleft/gpl.html | |
EndVersion | |
exit 1 | |
} | |
usage() | |
{ | |
cat<<-EndUsage | |
Usage: $SCRIPT_NAME [-hVv] [-t type] [-p project] [-d count] [-s|S] [-f path] text | |
Try '$SCRIPT_NAME -h' for more information. | |
EndUsage | |
exit 1 | |
} | |
help() | |
{ | |
cat <<-EndHelp | |
Usage: $SCRIPT_NAME [-hVv] [-t type] [-p project] [-d count] [-s|S] [-f path] text | |
With no options or input, $SCRIPT_NAME outputs the last 10 lines of the log. | |
Options: | |
-t TYPE | |
The type classification that the log event belongs to. example: work, school etc. | |
-p PROJECT | |
The project that the log event belongs to. This helps group log events together | |
which might belong to the same type or which my not belong to a type at all. | |
-c COUNT | |
The number of lines to show when output the tail of the log. Defaults to 10. | |
-s text | |
Case insensitive search of the log file for the given text | |
-S text | |
Case sensitive search of the log file for the given text | |
-f path | |
File path to use for the log file. This is mainly for testing and the recommended | |
method is to export LOGGERTXT_PATH as an environment variable. | |
-h | |
Help Text. | |
-V | v | |
Show version information and credits. | |
-x | |
Deletes the last line from the log file. This allows for quick corrections of | |
log items which were just entered. | |
EndHelp | |
exit 1 | |
} | |
deleteLast() | |
{ | |
#only act if the log file exists | |
if [ -e $LOG_PATH ]; then | |
echo "" | |
echo "Deleted last line from file"; | |
`sed '$d' < $LOG_PATH > $dir"/log.txt.backup"` | |
`mv $dir"/log.txt.backup" $LOG_PATH` | |
fi | |
} | |
confirmDeleteLast() | |
{ | |
#delete the last line from the file. mainly used for quick fixes of mistakes | |
#get the last line for confirmation | |
LAST_LINE=`tail -n 1 $LOG_PATH` | |
echo "" | |
echo "Warning: You are removing the line below which appears at the end of the log file." | |
echo "" | |
echo "-------------------" | |
echo $LAST_LINE | |
echo "-------------------" | |
echo "" | |
echo "Do you wish to continue? (Y/n)" | |
read CONFIRM | |
case $CONFIRM in | |
Y) deleteLast;; | |
n|*) | |
echo "" | |
echo "No line deleted" | |
;; | |
esac | |
exit 1 | |
} | |
check_log_file() | |
{ | |
if [ -e $LOG_PATH ]; then | |
if [ ! -w $LOG_PATH ]; then | |
echo "$app: Log file not writeable" | |
exit 1 | |
fi | |
else | |
# create log file if it does not exist | |
echo "$app: Creating log file" | |
`touch $LOG_PATH` | |
`chmod +w $LOG_PATH` | |
if [ -e $LOG_PATH ]; then | |
echo "$app: Log file successfully created" | |
else | |
echo "$app: Log file couldn't be created" | |
exit 1 | |
fi | |
fi | |
if [ ! -r $LOG_PATH ]; then | |
echo "$app: Log file is not readable" | |
exit 1 | |
fi | |
} | |
# Case insensitive search | |
search_log() | |
{ | |
local file=$1 | |
local term=$2 | |
local count=$3 | |
check_log_file | |
# grep the file for the search term | |
results=`grep -i "$term" "$file" | tail -n $count` | |
# Add to the end of the results | |
OUTPUT="$OUTPUT$results" | |
} | |
# Case sensitive search | |
search_log_sensitive() | |
{ | |
local file=$1 | |
local term=$2 | |
local count=$3 | |
check_log_file | |
# grep the file for the search term | |
results=`grep "$term" "$file" | tail -n $count` | |
# Add to the end of the results | |
OUTPUT="$OUTPUT$results" | |
} | |
############################ | |
# Start of active script | |
############################ | |
# defaults if not yet defined | |
dir=$HOME | |
#set the log path to the environment variable if it is set | |
if [ ! -z $LOGGERTXT_PATH ]; then | |
LOG_PATH=$LOGGERTXT_PATH | |
else | |
LOG_PATH=$dir"/log.txt" | |
fi | |
OUTPUT='' | |
LOG_TYPE=${LOG_TYPE:-''} | |
LOG_DISPLAY_COUNT=${LOG_DISPLAY_COUNT:-10} | |
LOG_PROJ=${LOG_PROJ:-''} | |
now=`date '+%d/%m/%y %H:%M %z'` | |
app="Logger-TXT" | |
SCRIPT_NAME=$(basename "$0") | |
# process options | |
while getopts xt:c:p:s:S:f:Vvh o | |
do case "$o" in | |
x) confirmDeleteLast;; | |
s) SEARCH=$OPTARG;; | |
S) SEARCH_CASE=$OPTARG;; | |
t) LOG_TYPE=`echo "$OPTARG" | tr "[:lower:]" "[:upper:]"`;; | |
c) LOG_DISPLAY_COUNT=$OPTARG;; | |
p) LOG_PROJ=`echo "$OPTARG" | tr "[:lower:]" "[:upper:]"`;; | |
f) LOG_PATH=$OPTARG;; | |
h) help;; | |
V|v) version;; | |
[?]) usage;; | |
esac | |
done | |
# shift the option values out | |
shift $(($OPTIND - 1)) | |
#The remaining text is the log text. | |
#take the input and add to file | |
if [ ! -z "$1" ]; then | |
#add to log file | |
check_log_file | |
if [ ! -z $LOG_TYPE ]; then | |
sep=" - " | |
ltype=" under the type $LOG_TYPE" | |
LOG_TYPE="$LOG_TYPE" | |
fi | |
if [ ! -z $LOG_PROJ ]; then | |
sep=" - " | |
proj=" in the project $LOG_PROJ" | |
LOG_PROJ="($LOG_PROJ)" | |
fi | |
#there is a proj but no type | |
if [ -z $LOG_TYPE ] && [ ! -z $LOG_PROJ ]; then | |
category="$LOG_PROJ$sep" | |
fi | |
#there is a type but no proj | |
if [ ! -z $LOG_TYPE ] && [ -z $LOG_PROJ ]; then | |
category="$LOG_TYPE$sep" | |
fi | |
#there is both | |
if [ ! -z $LOG_TYPE ] && [ ! -z $LOG_PROJ ]; then | |
category="$LOG_TYPE $LOG_PROJ$sep" | |
fi | |
#add text to file | |
echo "$now - $category$*" >> "$LOG_PATH" | |
#output that the event was logged | |
OUTPUT="$OUTPUT\"$*\" logged$ltype$proj" | |
else | |
check_log_file | |
if [ ! -z $SEARCH ]; then | |
search_log "$LOG_PATH" "$SEARCH" "$LOG_DISPLAY_COUNT" | |
elif [ ! -z $SEARCH_CASE ]; then | |
search_log_sensitive "$LOG_PATH" "$SEARCH_CASE" "$LOG_DISPLAY_COUNT" | |
else | |
# Print limited amount of log from the end | |
OUTPUT=`tail -n $LOG_DISPLAY_COUNT $LOG_PATH` | |
fi | |
fi | |
# Print the output | |
echo -e "$OUTPUT" |