Archive for March, 2009

Logging with GLib

Sunday, March 1st, 2009

I use glib’s logging functions quite a bit while working on Gnome-Mud.   If you don’t recognize them by that name then perhaps g_message, g_warning, etc you’ve heard of: same thing.  These are especially handy for logging entire Telnet sessions with every negotiation and byte transferred easily trackable.  I’m sure there are uber developers out there who can crank out bugfree telnet implementations without breaking a sweat, sadly I am definitely not one of them.

The bad part about using logging like glorified printf’s is that it really spams the console and I personally hate when programs do that so I’ve simply removed my logging messages prior to a release of Gnome-Mud in the past.  This is really quite lame because then I have to go back in and re-add all my logging stuff.  So really, what I wanted was a way to show logging information only on development builds and then an easy way to turn it off without deleting all of it for releases.

The first (and worst) thing that came to my head was simply to wrap every g_message with #ifdef DEBUG…#endif warts but this makes the source looks pretty awful so I threw that idea out quickly and decided to actually use the tools GLib provides for this very task.

GLib lets you set a logging handler that bypasses the default way GLib reports logging info (ie, spamming it to the console).  Once I got my head around what was needed actually implementing a logging handler was pretty simple (the docs are really quite good in this area.)   One of the neat things about creating a custom logging handler is that you can display this information anyway you please.   After a productive day of hacking I now have this up and running:

Debug Logger

Debug Logger

Using colors isn’t always the greatest idea since there’s no gaurentee that any particular GTK+ theme will look good with them but they make it easy for me to quickly parse a bunch of logging.  Using colors is disabled by default just to be safe though.  All the colors that are used are all customizable with gobject parameters so its a fairly self-contained little setup.

I made a simple API that lets you add tabs (or logging domains) pretty easily.  I am somewhat abusing the glib log domain since it’s technically for libraries but I find it handy to be able to seperate logging for various subsystems.  The neat part is that when this is activated (currently via a bunch of autofoo in gnome-mud) all logging is handled by the UI.  When it is inactive it will print g_warnings and g_criticals to the console still but all lower level logging is simply gagged.

All in all pretty good work for a day’s hacking.  I think this is useful enough to merit a release seperate from Gnome-Mud itself for anyone who wants simple drop-in gui based logging.  Tomorrow I’m going to generalize some bits of it (and remove all the autofoo dependant stuff) and add some more api to allow for things I’m not doing in Gnome-Mud’s usage of it.  Documenting it should be quick too so hopefully by Wednesday I’ll get this out there.

If you’d like to see it sooner then that just look at debug-logger.[ch] in the trunk of Gnome-Mud’s SVN repo (svn://svn.gnome.org/svn/gnome-mud/trunk)