forked from grantlucas/Logger-TXT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger-txt
executable file
·280 lines (236 loc) · 5.77 KB
/
logger-txt
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
SAVEFILE=~/.loggertext_mute
version()
{
cat <<-EndVersion
Logger-TXT-Mod
Version 2.0.1
Author: Grant Lucas ([email protected]) / Mod by Daniel Jackel ([email protected])
Last updated: 28/03/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] [-m] [-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] [-m] [-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.
-m
Toggle Silent Mode
-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"
}
muteToggle()
{
if [ ! -f "$SAVEFILE" ]
then
echo "1" > $SAVEFILE
else
if [ $(cat $SAVEFILE) != 1 ]
then
echo 1 > $SAVEFILE
else
echo 0 > $SAVEFILE
fi
fi
[[ $(cat $SAVEFILE) = 1 ]] && OUT="mute" || OUT="normal"
echo -e StdOut ist now: $OUT
exit
}
############################
# 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 '+%Y-%m-%d %H:%M'`
app="Logger-TXT"
SCRIPT_NAME=$(basename "$0")
# process options
while getopts xt:c:p:s:S:f:Vvhm o
do
case "$o" in
x) confirmDeleteLast;;
s) SEARCH=$OPTARG;;
S) SEARCH_CASE=$OPTARG;;
t) LOG_TYPE=`echo "$OPTARG"`;;
c) LOG_DISPLAY_COUNT=$OPTARG;;
p) LOG_PROJ=`echo "$OPTARG" | tr "[:lower:]" "[:upper:]"`;;
f) LOG_PATH=$OPTARG;;
h) help;;
V|v) version;;
m) muteToggle;;
[?]) 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
if [ $(cat $SAVEFILE) != 1 ]
then
echo -e "$OUTPUT"
fi