ps1.soapyfrog.com

doing inappropriate things with powershell

Pages

  • About
  • Downloads
  • Archives

Search

Popular Posts

  • Grrr 1.1 and Big Invaders
  • Space Invaders
  • Convert images to text (ASCII art)
  • Cmdlet clashes
  • Console screen grabs in html

Recent Posts

  • Grrr source code, including Invaders
  • Google going down the pan
  • Cmdlet clashes
  • Grrr 1.1 and Big Invaders
  • Grrr, Cmdlets and PSInvaders revival

Categories

  • Announce (7)
  • Cmdlets (2)
  • Cool (16)
  • Grrr (6)
  • Hint (2)
  • Invaders (5)
  • Odd (2)
  • PowerShell (27)
  • Quiz (3)
  • Rant (7)
  • Uncategorized (1)
  • Utility (5)

Months

  • August 2007 (1)
  • April 2007 (1)
  • March 2007 (1)
  • February 2007 (3)
  • January 2007 (25)
  • December 2006 (1)

Bookmarks

  • Blogroll
    • $script Fanatics
    • blog.soapyfrog.com
    • Brian Long
    • Lee Holmes
    • Nik Crabtree
    • PowerShell-Scripting (French)
    • Richy Rich
    • The PowerShell Guy
    • Windows PowerShell
  • Links
    • Carbon-neutral web hosting!

Meta

  • Log in
  • Posts RSS
  • Comments RSS
  • Valid XHTML
  • Valid CSS
« Making functions *really* read-only
IRC chat bot and monitor »
 

Debug and Verbose colouring

29 Jan 2007 05:59 pm// Hint, PowerShell    

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 to “Debug and Verbose colouring”

  1. On 31 Jan 2007 at 2:52 pm, ps1.soapyfrog.com » IRC chat bot and monitor said:   

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

  2. On 31 Oct 2007 at 12:35 am, BS on PoSH - Blog » Get-CitrixHotfix: The Bitter/Sweet of Write-Verbose said:   

    [...] The good news is you can control the color using $host.PrivateDataMore Info http://ps1.soapyfrog.com/2007/01/29/debug-and-verbose-colouring/ [...]

Copyright © 2006-2008 Adrian Milliner

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.