Debug and Verbose colouring

Home  >>  Hint  >>  Debug and Verbose colouring

Debug and Verbose colouring

On January 29, 2007, Posted by , In Hint,PowerShell, With 2 Comments

Over the weekend, I set about writing an IRC chat client/channel monitoring tool in PowerShell (more on that in a day or so) and one of the things I was doing a lot was writing verbose and debug information to the console.

By default, Write-Verbose and Write-Debug cmdlets write nothing to the console, because the the value of the variables $VerbosePreference and $DebugPreference respectively are “SilentlyContinue”.

If you set them both to “Continue” the text is written to the console, but both in bright yellow on black (I wonder why?), like this:

PS> $DebugPreference="continue"
PS> $VerbosePreference="continue"
PS> write-debug "debug msg"
DEBUG: debug msg
PS> write-verbose "verbose msg"
VERBOSE: verbose msg

These colours can be changed so you can more easily distinguish between debug and verbose messages, and also not have the real output swamped.

The trick is to change the values of the colours in $host.privatedata, and I do this:

$defbg="darkmagenta"
$deffg="darkyellow"

# normal colours
$r=$host.ui.rawui
$r.BackgroundColor=$defbg
$r.foregroundColor=$deffg

$p=$host.privatedata
# error colours
$p.ErrorBackgroundColor=$defbg
$p.ErrorForegroundColor="red"

# warning colours
$p.WarningBackgroundColor=$defbg
$p.WarningForegroundColor="yellow"

# debug colours
$p.DebugBackgroundColor=$defbg
$p.DebugForegroundColor="darkgray"

# verbose colours
$p.VerboseBackgroundColor=$defbg
$p.VerboseForegroundColor="darkcyan"

# progress colours
$p.ProgressBackgroundColor="darkcyan"
$p.ProgressForegroundColor="yellow"

Now, the IRC app I’m working on, when I run with debug/verbose switched on, yields an output like this:

PS> test
DEBUG: Using connection info:
DEBUG: port: 6667
DEBUG: nick: soapybot
DEBUG: server: chat.freenode.net
DEBUG: hostname: localhost
DEBUG: user: soapybot
DEBUG: realname: inout-irc as soapybot
VERBOSE: >> NICK soapybot
VERBOSE: >> USER soapybot localhost chat.freenode.net :inout-irc as soapybot
VERBOSE: << NOTICE AUTH :*** Looking up your hostname...
VERBOSE: << NOTICE AUTH :*** Found your hostname, welcome back
VERBOSE: << NOTICE AUTH :*** Checking ident
VERBOSE: << NOTICE AUTH :*** No identd (auth) response
VERBOSE: << :brown.freenode.net 001 soapybot :Welcome to the freenode IRC Network 
soapybot
VERBOSE: << :brown.freenode.net 002 soapybot :Your host is 
brown.freenode.net[brown.freenode.net/6667], running version hyperion-1.0.2b
VERBOSE: << NOTICE soapybot :*** Your host is 
brown.freenode.net[brown.freenode.net/6667], running version hyperion-1.0.2b
VERBOSE: << :brown.freenode.net 003 soapybot :This server was created Fri Dec 22 
00:07:52 UTC 2006
VERBOSE: << :brown.freenode.net 004 soapybot brown.freenode.net hyperion-1.0.2b 
aAbBcCdDeEfFGhHiIjkKlLmMnNopPQrRsStTuUvVwWxXyYzZ01234569*@ bcdefFhiIklmnoPqstv
VERBOSE: << :brown.freenode.net 005 soapybot IRCD=dancer CAPAB CHANTYPES=# EXCEPTS INVEX
 CHANMODES=bdeIq,k,lfJD,cgijLmnPQrRstz CHANLIMIT=#:20 PREFIX=(ov)@+ MAXLIST=bdeI:50 
MODES=4 STATUSMSG=@ KNOCK NICKLEN=16 :are supported by this server
VERBOSE: << :brown.freenode.net 005 soapybot SAFELIST CASEMAPPING=ascii CHANNELLEN=30 
TOPICLEN=450 KICKLEN=450 KEYLEN=23 USERLEN=10 HOSTLEN=63 SILENCE=50 :are supported by 
this server
VERBOSE: << :brown.freenode.net 251 soapybot :There are 18601 listed and 16168 unlisted 
users on 24 servers
VERBOSE: << :brown.freenode.net 375 soapybot :- brown.freenode.net Message of the Day - 
VERBOSE: << :brown.freenode.net 372 soapybot :- Freenode is a service of Peer-Directed 
Projects Center, an
VERBOSE: << :brown.freenode.net 372 soapybot :- IRS 501(c)(3) not-for-profit 
organization.  Our yearly
VERBOSE: << :brown.freenode.net 372 soapybot :- fundraiser will begin soon; if you'd 
like to donate early,
VERBOSE: << :brown.freenode.net 372 soapybot :- please see 
http://freenode.net/pdpc_donations.shtml for more
VERBOSE: << :brown.freenode.net 372 soapybot :- information.  Thank you for using 
freenode!
VERBOSE: << :brown.freenode.net 372 soapybot :- 
VERBOSE: << :brown.freenode.net 376 soapybot :End of /MOTD command.
VERBOSE: >> JOIN #test,#test2,#archlinux
VERBOSE: << :freenode-connect!freenode@freenode/bot/connect PRIVMSG soapybot :☺VERSION☺
freenode-connect : soapybot : VERSION
VERBOSE: << :soapybot!n=soapybot@*********** JOIN :#test
DEBUG: We may have joined channel #test
VERBOSE: << :brown.freenode.net 332 soapybot #test :Welcome to #test - This channel is 
for testing only. Don't harass or annoy other users here. Don't paste senceless crap 
here. If you need a response for tests type '#say foobar' or '#moo' - Have a nice time 
in here... RIP lilo aka rob levin :'(
DEBUG: We have joined channel #test
VERBOSE: << :soapybot!n=soapybot@*************** JOIN :#test2
DEBUG: We may have joined channel #test2
VERBOSE: >> PRIVMSG #test2 :hello
VERBOSE: << :soapybot!n=soapybot@*************** JOIN :#archlinux
DEBUG: We may have joined channel #archlinux
VERBOSE: << :brown.freenode.net 332 soapybot #archlinux :[ Welcome to #archlinux! 
(!rules) || main: archlinux.org || forums: bbs.archlinux.org || wiki: wiki.archlinux.org
 || bugs: bugs.archlinux.org || AUR: aur.archlinux.org || iso: 
ftp://ftp.archlinux.org/other/0.8 ]
DEBUG: We have joined channel #archlinux
VERBOSE: << :schnappi!n=joy@debiancenter/user/schnappi PRIVMSG #test :Good after-noon, 
soapybot. It's 17:28h.GMT
schnappi : #test : Good after-noon, soapybot. It's 17:28h.GMT
VERBOSE: << :Danny|!n=Danny@89-172-171-230.adsl.net.t-com.hr PART #archlinux :"So Say We
 All."
VERBOSE: << :millinad!n=millinad@*************** PRIVMSG #test2 :hello 
soapybot
millinad : #test2 : hello soapybot
VERBOSE: << :millinad!n=millinad@*************** PRIVMSG #test2 :go now
millinad : #test2 : go now
VERBOSE: << :millinad!n=millinad@*************** PRIVMSG #test2 :wibble
millinad : #test2 : wibble
VERBOSE: >> PART #test2
VERBOSE: >> PART #archlinux
VERBOSE: >> PART #test
VERBOSE: >> QUIT :bye bye

(some of the above has been trimmed for clarity and privacy).

The brighter white output is the normal output from the script, the grey is debug messages and cyan, verbose. It gives me all the output I want, without making it too hard to pick out the real information.

Once I’m happy that all is well, I can revert the $VerbosePreference and $DebugPreference variables back to “SilentlyContinue” and all you get is the default white output.

2 Comments so far:

  1. […] chat bot and monitorDebug and Verbose colouringMaking functions *really* read-onlyUpdated get-bufferhtmlWebserver and RSH in […]