On Sun, 05 Apr 2009 07:36:45 +0100
"Sam Liddicott" <[email protected]> wrote:

> I find thay my debug statements are more helpful that my comments; my
> comments explain what I'm trying to do, by debug statements explain
> what is being done and why.
> 
> Debug stataments by definition and out of necessity help one
> understand the code flow.
> 
> But why not just write a debug statement instead of a special
> comment? It's the care in crafting a debug statement that makes it so
> useful.

I actually use a class for debugging, it's (probably) not
fully-featured compared to what some people would like to have, but it
works for me.  It is somewhat specific to the application I am working
on and I haven't had time to generalize it just yet, though that in the
plans since I wound up liking the structure of it, but it's a bit of a
pain to keep in sync---specifically, lots of things that it uses are
currently hard-coded and I'd like it to be more flexible in terms of
what it can do.

If you're interested, though, it's attached.  To use it, first modify
it to your needs:

  * Change the name of the environment variable from ALLTRAY_DEBUG to
    <YOURAPPNAME>_DEBUG.  Do a search/replace, it's in a few different
    places.

  * Update the Subsystem enumeration to fit your needs.

  * Update the switch in emit() to reflect the previous change.

  * Update subsys_to_string() to fit the changes you made to the
    Subsystem enumeration.  These names are decoration for the debug
    statements.

Then, when you want to use it, make a call to init(), it will read the
appropriate environment variable and set things up based on the changes
above.  It will emit warnings if you place debugging options in the
environment variable that the class doesn't handle, and you can combine
things:

Sunday, 2009-Apr-05 at 03:18:22 - m...@zest - Linux v2.6.29.1
Ubuntu Jaunty:[1-35/2423-0]:src> ALLTRAY_DEBUG=ALL ./alltray -D xterm
[2009-04-05 03:18:25 EDT] INFO/MISC: Debugging subsystem setup complete.
[2009-04-05 03:18:25 EDT] INFO/CL: Command line options parsed.
[2009-04-05 03:18:25 EDT] INFO/PROCESS: Child process 15585 (xterm) now running.
[2009-04-05 03:18:27 EDT] INFO/PROCESS: Child process 15585 (xterm) died.
[2009-04-05 03:18:27 EDT] INFO/PROCESS: Cleaning up for child...
[2009-04-05 03:18:27 EDT] INFO/PROCESS: No more children. Dying.

Sunday, 2009-Apr-05 at 03:18:27 - m...@zest - Linux v2.6.29.1
Ubuntu Jaunty:[1-36/2423-0]:src> ALLTRAY_DEBUG=MISC ./alltray -D xterm
[2009-04-05 03:18:40 EDT] INFO/MISC: Debugging subsystem setup complete.

Sunday, 2009-Apr-05 at 03:18:41 - m...@zest - Linux v2.6.29.1
Ubuntu Jaunty:[1-37/2424-0]:src> ALLTRAY_DEBUG=CL ./alltray -D xterm
[2009-04-05 03:18:49 EDT] INFO/CL: Command line options parsed.

Sunday, 2009-Apr-05 at 03:18:50 - m...@zest - Linux v2.6.29.1
Ubuntu Jaunty:[1-38/2425-0]:src> ALLTRAY_DEBUG="MISC CL" ./alltray -D xterm
[2009-04-05 03:18:59 EDT] INFO/MISC: Debugging subsystem setup complete.
[2009-04-05 03:18:59 EDT] INFO/CL: Command line options parsed.

Sunday, 2009-Apr-05 at 03:19:00 - m...@zest - Linux v2.6.29.1
Ubuntu Jaunty:[1-39/2426-0]:src> ALLTRAY_DEBUG="MISC PROCESS" ./alltray -D xterm
[2009-04-05 03:19:05 EDT] INFO/MISC: Debugging subsystem setup complete.
[2009-04-05 03:19:05 EDT] INFO/PROCESS: Child process 15625 (xterm) now running.
[2009-04-05 03:19:06 EDT] INFO/PROCESS: Child process 15625 (xterm) died.
[2009-04-05 03:19:06 EDT] INFO/PROCESS: Cleaning up for child...
[2009-04-05 03:19:06 EDT] INFO/PROCESS: No more children. Dying.

Sunday, 2009-Apr-05 at 03:19:06 - m...@zest - Linux v2.6.29.1
Ubuntu Jaunty:[1-40/2427-0]:src> ALLTRAY_DEBUG="MISC PROCESS FUBAR" ./alltray 
-D xterm

** (alltray:15634): WARNING **: Debug.vala:71: Unrecognized value 'FUBAR' in 
ALLTRAY_DEBUG
[2009-04-05 03:19:51 EDT] INFO/MISC: Debugging subsystem setup complete.
[2009-04-05 03:19:51 EDT] INFO/PROCESS: Child process 15635 (xterm) now running.
[2009-04-05 03:19:52 EDT] INFO/PROCESS: Child process 15635 (xterm) died.
[2009-04-05 03:19:52 EDT] INFO/PROCESS: Cleaning up for child...
[2009-04-05 03:19:52 EDT] INFO/PROCESS: No more children. Dying.

The Vala source is attached.

        --- Mike
/*
 * Debug.vala - Debugging system for AllTray.
 * Copyright (C) 2009 Michael B. Trausch <[email protected]>
 * License: GNU GPL v3 as published by the Free Software Foundation.
 */
using GLib;

namespace AllTray.Debug {
	public enum Subsystem {
		None = 0x00,
		CommandLine = 0x01,
		Display = 0x02,
		SystemTray = 0x04,
		WindowManager = 0x08,
		Process = 0x10,
		Misc = 0x20,
		Bug = 0x40
	}

	public enum Level {
		Information,
		Warning,
		Error,
		Fatal
	}

	public static class Notification {
		private static Subsystem _subsys = Subsystem.None;
		private static bool _enabled = false;

		public static void init() {
			_enabled = true;
			string? enabled_subsystems =
				GLib.Environment.get_variable("ALLTRAY_DEBUG");

			if(enabled_subsystems != null) {
				string[] values = enabled_subsystems.split(" ");
				foreach(string tmpSubsys in values) {
					switch(tmpSubsys) {
					case "CL":
						_subsys |= Subsystem.CommandLine;
						break;
					case "DISPLAY":
						_subsys |= Subsystem.Display;
						break;
					case "SYSTRAY":
						_subsys |= Subsystem.SystemTray;
						break;
					case "WM":
						_subsys |= Subsystem.WindowManager;
						break;
					case "PROCESS":
						_subsys |= Subsystem.Process;
						break;
					case "MISC":
						_subsys |= Subsystem.Misc;
						break;
					case "BUG":
						_subsys |= Subsystem.Bug;
						break;
					case "ALL":
						_subsys |= (Subsystem.CommandLine |
									Subsystem.Display |
									Subsystem.SystemTray |
									Subsystem.WindowManager |
									Subsystem.Process |
									Subsystem.Misc |
									Subsystem.Bug);
						break;
					default:
						GLib.warning("Unrecognized value '%s' in ALLTRAY_DEBUG",
									 tmpSubsys);
						break;
					}
				}

				emit(Subsystem.Misc, Level.Information,
					 "Debugging subsystem setup complete.");
			} else if((enabled_subsystems == null) ||
					  (enabled_subsystems == "")) {
				GLib.warning("ALLTRAY_DEBUG not set!");
			}
		}

		public static void emit(Subsystem subsys, Level lvl, string msg) {
			// Bail out early when we should not run.
			if(!_enabled) return;
			if((_subsys & subsys) != subsys) return;

			string cur_time;

			cur_time = Time.local(time_t()).format("%F %T %Z");

			string lvl_str = lvl_to_string(lvl);
			string subsys_str = subsys_to_string(subsys);
			stderr.printf("[%s] %s/%s: %s\n", 
						  cur_time, lvl_str, subsys_str, msg);

			if(lvl == Level.Fatal) {
				emit(subsys, Level.Information,
					 "Exiting: encountered fatal error.");
				Native.StdC.Stdlib.abort();
			}
		}

		private static string subsys_to_string(Subsystem subsys) {
			string retval = "";

			switch(subsys) {
			case Subsystem.None:
				retval = "None";
				break;

			case Subsystem.CommandLine:
				retval = "CL";
				break;

			case Subsystem.Display:
				retval = "DISPLAY";
				break;

			case Subsystem.SystemTray:
				retval = "SYSTRAY";
				break;

			case Subsystem.WindowManager:
				retval = "WM";
				break;

			case Subsystem.Process:
				retval = "PROCESS";
				break;

			case Subsystem.Misc:
				retval = "MISC";
				break;

			case Subsystem.Bug:
				retval = "BUG";
				break;

			default:
				GLib.error("BUG: Reached default, should not be possible.");
				break;
			}

			return(retval);
		}

		private static string lvl_to_string(Level lvl) {
			string retval = "";

			switch(lvl) {
			case Level.Information:
				retval = "INFO";
				break;

			case Level.Warning:
				retval = "WARN";
				break;

			case Level.Error:
				retval = "ERR";
				break;

			case Level.Fatal:
				retval = "FATAL";
				break;

			default:
				GLib.error("BUG: Reached default, should not be possible.");
				break;
			}

			return(retval);
		}
	}
}
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to