Wednesday, February 7, 2007

Command-line logging

I don't understand why modern terminals (with exception of putty) do not allow for logging.

I successfully use my e-mail history and IM logs to great extent when doing post mortems or other general reports. I should have that same functionality at my command prompt. And something a little more useful than "cat ~/.history". Script is handy for this (though an unfortunately hard name to google for). But I want to not have to worry about those pesky filenames so I did up a quick startlog script.


#!/usr/local/bin/bash

LOGDIR="/home/ben/logs/" #where we store teh logs
ITERATION=0 #start off at 0
STARTED=0
DATE=`date "+%F"`
if [ -n "$1" ] #this is what the filename will be prefixed with
then
PREFIX=$1
else
PREFIX="log"
fi

#echo -n "$PREFIX"


while [ "$STARTED" -eq "0" ]
do
ITERATION=$((ITERATION+1))
if [ ! -e $LOGDIR$PREFIX-$DATE-$ITERATION.log ]
then
export LOGFILE="$LOGDIR$PREFIX-$DATE-$ITERATION.log"
script $LOGFILE
echo -n "Logging to $LOGFILE"
STARTED=1
fi

done


I set my PS1 prompt to also show the current time to allow me to have everything timestamped. And, handily enough, I export the log filename to the shell which leads to a new alias in .bashrc:


alias sendlog="cat $LOGFILE | mail -s '$LOGFILE Transcript'"


I haven't actually used this yet but the novelty of it is handy. On a downside, cygwin does not have script which is a real bummer.

I still think this functionality should be built into rxvt, gnome-terminal, konsole, terminal.app, etc. Don't even get me started on cmd.exe "featureset".

1 comment: