WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: How to add a simple debugging window to your script  (Read 2486 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11178
How to add a simple debugging window to your script
« on: July 04, 2012, 11:04:01 PM »
Many scripts redirect stderr and/or sdtout to /dev/null to reduce clutter on the terminal that they are running in.
Here is an example of how you can add a basic debugging window that can be enabled/disabled by setting
one variable, allowing you to see error messages or any other text you wish to display in another terminal
while debugging.
Code: [Select]
#!/bin/sh
# DebugWindow script written by Richard Rost July 5,2012
# A simple example of one way to send messages that normally get sent to  /dev/null
# to a separate terminal while debugging scripts. The same technique can easily be
# adapted to C programs. While not intended to replace ncurses, you can send ANSI
# escape sequences to move the cursor, clear the screen, change text color, change
# the title bars text, and possibly do other things. Set DEBUG=1 for script
# debugging, and DEBUG=0 for normal script operation. You can run this script in
# any directory to see what it does. It creates 4 files in the /tmp directory and
# removes them when done. Read the comments for more information.
. /etc/init.d/tc-functions
null="/dev/null"
errors="/dev/null"
warnings="/dev/null"
info="/dev/null"
DEBUG=1
if [ $DEBUG == 1 ]
then
null=`mktemp`     # Create a unique file name
rm $null          # Remove the temporary file, we just want the name
mkfifo $null      # Create fifo used to send data to the debug window
errors=$null'errors'; :>$errors     # Create 3 empty files for sending colored text to window
warnings=$null'warnings'; :>$warnings
info=$null'info'; :>$info
# Open a new window for debugging messages
aterm -e sh -c "while [ -e $null ]; do cat $null; done" &
# Set the title bar
echo -ne "\033]2;$0 Deubugging Window\007" > $null
fi

logmsg()
{
if [ $DEBUG != 1 ]; then return; fi
if [ -s $errors ]; then echo "${RED}`cat $errors`${NORMAL}" > $null; :>$errors; fi
if [ -s $warnings ]; then echo "${YELLOW}`cat $warnings`${NORMAL}" > $null; :>$warnings; fi
if [ -s $info ]; then echo "${GREEN}`cat $info`${NORMAL}" > $null; :>$info; fi
}

# Examples
echo "Demonstration of DebugWindows script using ls to look" > $info; logmsg
echo "for non-existent files to generate output on stderr" > $info; logmsg
ls -l $null $null'5' &>$errors; logmsg      # Send stdout and stderr in red to debug window
ls $null $null'5' &> $null                  # Send stdout and stderr in white to debug window
ls -l $null $null'5' 2>$warnings; logmsg    # Send stderr in yellow to debug window, stdout to terminal
ls -l $null $null'5' 1>$info 2>$errors; logmsg  # Send stdout in green and stderr in red to debug window
echo "Click the close button when done with this window." > $info; logmsg

if [ $DEBUG == 1 ]; then rm $null*; fi    # Clean up temporary files
Download the attached file and make it executable to try it out. The script cleans up the temporary files it
created when it finishes. Between the comments in the script and the contents of the newly created window
it should be easy to understand how it works.
« Last Edit: July 05, 2012, 07:48:06 AM by Rich »

Offline coreplayer2

  • Hero Member
  • *****
  • Posts: 3020
Re: How to add a simple debugging window to your script
« Reply #1 on: July 06, 2012, 11:46:12 PM »
Cool thanx   will check it out :)