sontek ( John M. Anderson )

June 8, 2008

Debugging Mono with Trace.WriteLine and Debug.WriteLine

Filed under: .NET, C#, Mono, Programming — Tags: , , , , — sontek @ 4:43 pm

The point of Trace.WriteLine and Debug.WriteLine is to give you the ability to monitor certain areas of your code when debugging or tracing but avoid cluttering your screen or hard drive with useless output when you aren’t. When most people stumble upon Trace/Debug they think it should “Just work”–without any setup–this isn’t the case.

Another great benefits of using Trace/Debug instead of the old Console.WriteLine is you can define exactly where to log to in your app config or with environment variables, so sometimes you might want direct output to the Console but other times you might want to log to a file.

To get your output from Trace/Debug you first need to define either DEBUG or TRACE in your preprocessor directives.

You can do this in code by placing:

#define TRACE

in your code or by passing it at compile time:

gmcs -d:TRACE

after you have your preprocessor directives setup, you have to define where to log to. You can do this by defining it in your app config:

<configuration>
  <system.diagnostics>
    <trace autoflush="false" indentsize="4">
      <listeners>
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="TextWriterOutput.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

or you can define it with the environment variable MONO_TRACE_LISTENER:

export MONO_TRACE_LISTENER=Console.Out

and then you can start your logging with System.Diagnostics.Trace.WriteLine(”Hello World”).

To learn more about Trace/Debug check out:the docs here and here. To learn more about other environment variables you can use with mono ‘man mono’ or click here.

May 11, 2008

Python with a modular IDE (Vim)

Filed under: Programming, python, vim — Tags: , , — sontek @ 3:18 pm

On Thursday, May 9th, 2008 the Utah Python User Group decided to settle the debate that has plagued us developers since the beginning of time: If you were a programming language, what editor would you use?

I was tasked with showing Eclipse with the PyDev plugin in all its glory–but we all know–real men / developers don’t use IDE’s, so we are going to talk about using Python and Vim together, reaching a state of Zen that the Dalai LLama would be jealous of and establishing more Feng Shui than Martha Stewart’s Kitchen.

Freely jump between your code and python class libraries

There are 2 ways to add your ability to jump between python class libraries, the first is to setup vim to know where the Python libs are so you can use ‘gf’ to get to them (gf is goto file). You can do this by adding this snippet to your .vimrc:

python << EOF
import os
import sys
import vim
for p in sys.path:
    if os.path.isdir(p):
        vim.command(r"set path+=%s" % (p.replace(" ", r"\ ")))
EOF

With that snippet you will be able to go to your import statements and hit ‘gf’ on one of them and it’ll jump you to that file.

Continuing accessibility of the Python class libraries we are going to want to use ctags to generate an index of all the code for vim to reference:

$ ctags -R -f ~/.vim/tags/python.ctags /usr/lib/python2.5/

and then in your .vimrc

set tags+=$HOME/.vim/tags/python.ctags

This will give you the ability to use CTRL+] to jump to the method/property under your cursor in the system libraries and CTRL+T to jump back to your source code.

I also have 2 tweaks in my .vimrc so you can use CTRL+LeftArrow and CTRL+RightArrow to move between the files with more natural key bindings.

map <silent><C-Left> <C-T>
map <silent><C-Right> <C-]>

You can also see all the tags you’ve been to with “:tags”

Code Completion

To enable code completion support for Python in Vim you should be able to add the following line to your .vimrc:

autocmd FileType python set omnifunc=pythoncomplete#Complete

but this relies on the fact that your distro compiled python support into vim (which they should!).

Then all you have to do to use your code completion is hit the unnatural, wrist breaking, keystrokes CTRL+X, CTRL+O. I’ve re-bound the code completion to CTRL+Space since we are making vim an IDE! Add this command to your .vimrc to get the better keybinding:

inoremap <Nul> <C-x><C-o>

Along with code completion, you will also have call tip support. Here is a screenshot:

Vim with Code Completion
Documentation

No IDE is complete without the ability to access the class libraries documentation! You’ll need to grab this vim plugin. This gives you the ability to type :Pydoc os.path or use the keystrokes <Leader>pw and <Leader>pW to search for the item under the cursor. (Vim’s default <Leader> is “\”). Here is a screenshot:

Vim with PyDoc integration

Syntax Checking

Vim already has built in syntax highlighting for python but I have a small tweak to vim to give you notifications of small syntax errors like forgetting a colon after a for loop. Create a file called ~/.vim/syntax/python.vim and add the following into it:

syn match pythonError "^\s*def\s\+\w\+(.*)\s*$" display
syn match pythonError "^\s*class\s\+\w\+(.*)\s*$" display
syn match pythonError "^\s*for\s.*[^:]$” display
syn match pythonError “^\s*except\s*$” display
syn match pythonError “^\s*finally\s*$” display
syn match pythonError “^\s*try\s*$” display
syn match pythonError “^\s*else\s*$” display
syn match pythonError “^\s*else\s*[^:].*” display
syn match pythonError “^\s*if\s.*[^\:]$” display
syn match pythonError “^\s*except\s.*[^\:]$” display
syn match pythonError “[;]$” display
syn keyword pythonError         do

Now that you have the basics covered, lets get more complicated checking added. Add these 2 lines to your .vimrc so you can type :make and get a list of syntax errors:

autocmd BufRead *.py set makeprg=python\ -c\ \"import\ py_compile,sys;\ sys.stderr=sys.stdout;\ py_compile.compile(r'%')\"
autocmd BufRead *.py set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m

You will have the ability to to type :cn and :cp to move around the error list. You can also type :clist to see all the errors, and finally, sometimes you will want to check the syntax of small chunks of code, so we’ll add the ability to execute visually selected lines of code, add this snippet to your .vimrc:

python << EOL
import vim
def EvaluateCurrentRange():
eval(compile('\n'.join(vim.current.range),'','exec'),globals())
EOL
map <C-h> :py EvaluateCurrentRange()

Now you will be able to visually select a method/class and execute it by hitting “Ctrl+h”.

Browsing the source

Moving around the source code is an important feature in most IDE’s with their project explorers, so to get that type of functionality in vim we grab the Tag List plugin. This will give you the ability to view all opened buffers easily and jump to certain method calls in those buffers. Here is a screenshot of it in action:

Vim TagList Plugin

The other must-have feature of an IDE when browsing code is being able to open up multiple files in tabs. To do this you type :tabnew to open up a file in a new tab and than :tabn and :tabp to move around the tabs. Add these to lines to your .vimrc to be able to move between the tabs with ALT+LeftArrow and ALT+RightArrow:


map <silent><A-Right> :tabnext<CR>
map <silent><A-Left> :tabprevious<CR>

Debugging

To add debugging support into vim, we use the pdb module. Add this to your ~/.vim/ftplugin/python.vim to have the ability to quickly add break points and clear them out when you are done debugging:

python << EOF
def SetBreakpoint():
    import re
    nLine = int( vim.eval( 'line(".")'))

    strLine = vim.current.line
    strWhite = re.search( '^(\s*)', strLine).group(1)

    vim.current.buffer.append(
       "%(space)spdb.set_trace() %(mark)s Breakpoint %(mark)s" %
         {'space':strWhite, 'mark': '#' * 30}, nLine - 1)

    for strLine in vim.current.buffer:
        if strLine == "import pdb":
            break
    else:
        vim.current.buffer.append( 'import pdb', 0)
        vim.command( 'normal j1')

vim.command( 'map <f7> :py SetBreakpoint()<cr>')

def RemoveBreakpoints():
    import re

    nCurrentLine = int( vim.eval( 'line(".")'))

    nLines = []
    nLine = 1
    for strLine in vim.current.buffer:
        if strLine == 'import pdb' or strLine.lstrip()[:15] == 'pdb.set_trace()':
            nLines.append( nLine)
        nLine += 1

    nLines.reverse()

    for nLine in nLines:
        vim.command( 'normal %dG' % nLine)
        vim.command( 'normal dd')
        if nLine < nCurrentLine:
            nCurrentLine -= 1

    vim.command( 'normal %dG' % nCurrentLine)

vim.command( 'map <s-f7> :py RemoveBreakpoints()<cr>')
EOF

With that code you can now hit F7 and Shift-F7 to add/remove breakpoints. Then you just launch your application with !python % (percent being the current file, you can declare your main file here if its different).

Another tweak I use is to have my vim inside screen with a horizontal split, that way I can see the python interpreter and debug while still having vim there so I can easily fix my code. Here is a screenshot of that in action:

Vim with Screen

Snippets

A great time saver with stanard IDE’s is code snippets, so you can type a few key strokes and get a lot of code out of it. An example of this would be a django model, instead of typing out the complete declaration you could type ‘mmo<tab><tab>’ and have a skeleton of your model done for you. To do this in vim we grab the Snippets EMU plugin.

Check out a great screencast of snippetsEmu in action here

You can get my full setup here

Emacs

Here is a great post on how to do the same with Emacs.

April 16, 2008

Printing in GTK#

Filed under: .NET, Databases, Mono, Postgresql, Programming, Xorg, irssi — Tags: , , , , , — sontek @ 11:58 pm

I just finished porting Tomboy’s print code to GTK so that we would be more portable and I thought other Mono apps looking to move from libgnomeprint or wanting to add printing support might find a code example helpful.

First, To start printing you do something like:

private void PrintButtonClicked (object sender, EventArgs args)
{
Gtk.PrintOperation op = new PrintOperation ();
op.BeginPrint += new BeginPrintHandler (OnBeginPrint);
op.DrawPage += new DrawPageHandler(OnDrawPage);

op.Run (Gtk.PrintOperationAction.PrintDialog, this.Window);
}

after the PrintOperation is set off you need to handle the BeginPrint event. The main things that need to be done are finding out how many lines will fit on a page and how many lines you have:

public void OnBeginPrint(object sender, Gtk.BeginPrintArgs args)
{
PrintOperation op = (PrintOperation)sender;
lines_per_page = (int)Math.Floor ((double)args.Context.Height / (double)font_size);
Gtk.TextIter start_iter, end_iter;
this.Buffer.GetBounds (out start_iter, out end_iter);
lines = this.Buffer.GetText (start_iter, end_iter, false).Split ('\n');
op.NPages = (int)Math.Ceiling ((double)lines.Length / (double)lines_per_page);
}

Finally, now that you have the printing setup, you need to actually render the data to be printed:

public void OnDrawPage(object sender, Gtk.DrawPageArgs args)
{
PrintOperation op = (PrintOperation)sender;
Cairo.Context cr = args.Context.CairoContext;

int line = args.PageNr * lines_per_page;
int num_lines = 0;
if (args.PageNr+1 != op.NPages)
num_lines = line + lines_per_page;
else
num_lines = lines.Length;

cr.MoveTo (0, 0);

for (int i = 0; i < lines_per_page && line < num_lines; i++)
{
Pango.Layout layout = args.Context.CreatePangoLayout ();
Pango.FontDescription desc = Pango.FontDescription.FromString (”sans ” + font_size);
desc.Size = (int)(font_size * Pango.Scale.PangoScale);
layout.FontDescription = desc;

layout.SetText (lines[line]);
Pango.CairoHelper.ShowLayout (cr, layout);
cr.RelMoveTo (0, font_size);
line++;
}
}

This does not take into account styles but will give you the basic idea of what needs to be done.

March 20, 2008

Debugging with strace

Filed under: Bash, Linux, Programming — Tags: , , , — sontek @ 5:16 pm

I was helping a friend debug a problem with gksu (gnomesu alternative) today and we chose to use strace which allows you trace system calls an application makes.

To monitor all system calls an application makes you can redirect the output to a file like so:

strace <command> 2> <file name>
or
strace <command> -o <file name>

These commands return the exact same results, the first command redirects stderr (standard error, which has the file descriptor 2) to the file, strace sends all output to stderr by default, the second command uses the built in -o argument which is much cleaner.

One of the first things I like to do with strace is to check if it is having trouble accessing a file, which I see a lot because the file doesn’t exist or the user executing the command does not have permission to access it, you can do that with these commands:

strace <command> 2>&1 |grep open
or
strace <command> -e open

Again, these commands will return similar results. The first command redirects stderr to stdout so you can use grep to filter the output. The second command is the preferred method because it actually uses the built in -e argument which will trace only the named system call (this is a comma separated list so you can do strace -e open,read).

The only other arguments that I’ve found really helpful are -ff which when used with -o will append the pid (process id) to the file name and -F which will also trace children.

March 12, 2008

Update Twitter from irssi

Filed under: Linux, Perl, Programming, Twitter, irssi — Tags: , , — sontek @ 11:10 pm

I wrote a quick little perl script/irssi plugin that allows you to update twitter from irssi. It also has autocompletion for names from your friends and follower list. You can get it here: http://devtoo.net/svn/twitter/twitter.pl

To use this script place it in ~/.irssi/scripts and then type /load twitter.pl in irssi

Usage:
/twitter u I’m updating twitter from irssi
/twitter d sontek I’m direct messasging sontek from irssi

DISCLAIMER: First perl script I’ve ever written, i’m sure I’ve done things wrong.

November 1, 2007

Matt Asay being facetious?

Filed under: .NET, C#, Linux, Mono, Programming — Tags: , , , — sontek @ 9:03 pm

Matt Asay has posted a blog here claiming that Miguel de Icaza is wasting away his talents by “cloning” C#/.NET with mono, rather than

“going back to the innovation in GNOME that originally made you one of the most interesting developers on the planet”

C# and CLI are open standards, so they are just implementing an open standard, not cloning it. This short sighted outlook on the future of Linux and GNOME shows the difference between Miguel and Matt. Miguel and his team at Ximian/Novell have been leading innovation in Linux for over 10 years with GNOME and Evolution and the reason they began

“Squandering one of the industry’s best open source talents”

by working on Mono was because they realized that Linux could not compete with Windows without a better development environment, so rather than always trying to play catch up to the proprietary world, Miguel and his team set out to even the playing field.

And Joe Shaw points out the most frivolous point in Matt’s post here.

Matt says:

“You [Miguel], personally, would convince more by going back to the innovation in GNOME that originally made you one of the most interesting developers on the planet. I want the old Miguel (and Nat - where has Nat Friedman been?) back, the one who demo’d Nat’s Dashboard with Nat at OSCON. The one who led and pushed GNOME forward for so many years.”

Dashboard is one of the very first C#/.NET/Mono desktop applications on Linux. It is still one of the most innovative applications on the Linux desktop, and Matt agrees with this, which shows just how great Mono is.

Miguel isn’t only allowing for innovation on the Linux desktop but he is also allowing corporations that were mainly a C#/.NET shop to be able to port their applications to the Mac and Linux platforms.

Matt, I respect your opinion and believe you did great things when pushing open source at Novell, but I think you need to look at the bigger picture and get past the fact that .NET was designed by Micrososft. C# is a great language and the .NET class library makes it easy to quickly develop desktop applications such as F-Spot, Beagle, Tomboy, and Banshee.

Jeffrey Steadfast also provides an interesting view on this here.

October 24, 2007

SHTorrent - Schedule torrent downloads with a shell script

Filed under: Bash, Programming — Tags: , , — sontek @ 11:16 pm

If you would like to parse torrent RSS feeds on a schedule and don’t want to bog your server down with Azureus, I wrote a basic shell script that you can drop into your cron jobs and have it do all the work for you.

You can download the latest release here or check out the latest code with svn co http://devtoo.net/svn/shtorrent

and then all you have to do is copy shtorrent-cron into cron jobs and you’re set!

You can submit bugs or feature requests at http://devtoo.net/projects/shtorrent/

The original concept was taken from BashT

October 7, 2007

GCC adds CLI support

Filed under: .NET, C#, Linux, Programming — Tags: , , , , , — sontek @ 1:28 am

GCC has just added CLI (Common Language Infrastructure) support, this is a great step for mono because there has been a lot of controversy over implementing a language/infrastructure designed by Microsoft, even if it was released as an open standard from ECMA. This shows that the open source community is finally embracing mono as a viable solution for doing development. You can read more about it here: http://gcc.gnu.org/projects/cli.html

September 30, 2007

Setting up Postgresql on SUSE

Filed under: Databases, Linux, Postgresql, Programming, SUSE — Tags: , , , — sontek @ 12:54 am

To install postgresql you just need to use the SUSE package manager, either from yast or from zypper:

zypper in postgresql-server
or
yast -i postgresql-server

By default Postgresql on SUSE is setup in ‘ident’ mode, which matches your SQL user with your unix user, so only the unix user ’sontek’ can login to the database with the username ’sontek’. This is great security but is confusing the first time you are setting up the server because you wont be able to login with your user or root.

What you have to do is switch your user to postgres :
su - postgres

and then you will be able to setup your own user account: 
createuser ’sontek’ 

and now your user will be able to login to the postgresql server with your account.

The other issue with ident is now only unix users will be able to connect to your database server, so if you are a programmer and need to write an application that connects to the database you will need to setup a non-ident user. To do this you first need to create a user with a password:

createuser ‘appuser’ –pwprompt -E

This will create a user named ‘appuser’ and will prompt you for a password that will be encrypted.

and then add this line to your /var/lib/pgsql/data/pg_hba.conf file:

host   all        appuser         127.0.0.1/32          md5

This will setup your server to  allow ‘appuser’ to be authenticated via an md5 password. After you modify the pg_hba.conf file you will need to restart postgresql and then you are all set to start working with postgresql on SUSE!

/etc/init.d/postgresql restart

Powered by WordPress