[Gtk-sharp-list] Wrapper for g_log* functions

Gonzalo Paniagua Javier gonzalo@ximian.com
03 Dec 2002 20:26:33 +0100


--=-PUx0iMpQpo+GKMi5mld6
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Tired of seeing those Gtk-Critical messages and adding Console.WriteLine
or using a debugger to find out what is causing it? ;-)

Attached is a wrapper class for g_log* functions.

As an example, using just these 2 lines will print a stack trace for you
when a critital error message is sent from gtk:

LogFunc logFunc = new GLib.LogFunc (Glib.Log.PrintTraceLogFunction);
Log.SetLogHandler ("Gtk", Glib.LogLevelFlags.Critical, logFunc,
IntPtr.Zero);

The only thing I left out by now are g_warning et all and G_LOG_DOMAIN
(needed by those #define). Any idea on how to do this?

Ok to commit?

Any comments appreciated.

-Gonzalo


--=-PUx0iMpQpo+GKMi5mld6
Content-Disposition: attachment; filename=Log.cs
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=Log.cs; charset=ISO-8859-1

// Log.cs - Wrapper for message logging functions
//
// Authors:
//	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//=09
//
// (c) 2002 Gonzalo Paniagua
//

namespace GLib {

	using System;
	using System.Runtime.InteropServices;

	public delegate void LogFunc (string log_domain,
				      LogLevelFlags log_level,
				      string message,
				      IntPtr user_data);

	public delegate void PrintFunc (string message);

	[Flags]
	public enum LogLevelFlags : uint
	{
		/* log flags */
		FlagRecursion          =3D 1 << 0,
		FlagFatal              =3D 1 << 1,

		/* GLib log levels */
		Error                  =3D 1 << 2,       /* always fatal */
		Critical               =3D 1 << 3,
		Warning                =3D 1 << 4,
		Message                =3D 1 << 5,
		Info                   =3D 1 << 6,
		Debug                  =3D 1 << 7,

		/* Convenience values */
		AllButFatal            =3D 253,
		AllButRecursion        =3D 254,
		All                    =3D 255,

		FlagMask               =3D 3,
		LevelMask              =3D (uint) ~3
	}

	public class Log {

		[DllImport("glib-2.0")]
		static extern void g_logv (string log_domain, LogLevelFlags flags, string=
 message);
	=09
		public void WriteLog (string logDomain, LogLevelFlags flags, string forma=
t, params object [] args)
		{
			g_logv (logDomain, flags, String.Format (format, args));
		}

		[DllImport("glib-2.0")]
		static extern uint g_log_set_handler (string log_domain,
						      LogLevelFlags flags,
						      LogFunc log_func,
						      IntPtr user_data);
	=09
		public static uint SetLogHandler (string logDomain,
						  LogLevelFlags flags,
						  LogFunc logFunc,
						  IntPtr userData)
		{
			return g_log_set_handler (logDomain, flags, logFunc, userData);
		}

		[DllImport("glib-2.0")]
		static extern uint g_log_remove_handler (string log_domain, uint handler_=
id);

		public static void RemoveLogHandler (string logDomain, uint handlerID)
		{
			g_log_remove_handler (logDomain, handlerID);
		}


		[DllImport("glib-2.0")]
		static extern PrintFunc g_set_print_handler (PrintFunc handler);

		public static PrintFunc SetPrintHandler (PrintFunc handler)
		{
			return g_set_print_handler (handler);
		}
	=09
		[DllImport("glib-2.0")]
		static extern PrintFunc g_set_printerr_handler (PrintFunc handler);

		public static PrintFunc SetPrintErrorHandler (PrintFunc handler)
		{
			return g_set_printerr_handler (handler);
		}
	=09
		[DllImport("glib-2.0")]
		static extern void g_log_default_handler (string log_domain,
							  LogLevelFlags log_level,
							  string message,
							  IntPtr unused_data);

		public static void DefaultHandler (string logDomain,
						   LogLevelFlags logLevel,
						   string message,
						   IntPtr unusedData)
		{
			g_log_default_handler (logDomain, logLevel, message, unusedData);
		}

		[DllImport("glib-2.0")]
		extern static LogLevelFlags g_log_set_always_fatal (LogLevelFlags fatal_m=
ask);
	=09
		public static LogLevelFlags SetAlwaysFatal (LogLevelFlags fatalMask)
		{
			return g_log_set_always_fatal (fatalMask);
		}

		[DllImport("glib-2.0")]
		extern static LogLevelFlags g_log_set_fatal_mask (string log_domain, LogL=
evelFlags fatal_mask);
	=09
		public static LogLevelFlags SetAlwaysFatal (string logDomain, LogLevelFla=
gs fatalMask)
		{
			return g_log_set_fatal_mask (logDomain, fatalMask);
		}

		/*
		 * These common logging methods are provided because they may be very com=
mon.
		 *
		 * Sample usage:
		 *
		 *	// Print the messages for the NULL domain
		 *	LogFunc logFunc =3D new GLib.LogFunc (Glib.Log.PrintLogFunction);
		 *	Log.SetLogHandler (null, GLib.LogLevelFlags.All, logFunc, IntPtr.Zero)=
;
		 *
		 *	// Print messages and stack trace for Gtk critical messages
		 *	logFunc =3D new GLib.LogFunc (Glib.Log.PrintTraceLogFunction);
		 *	Log.SetLogHandler ("Gtk", Glib.LogLevelFlags.Critical, logFunc, IntPtr=
.Zero);
		 *
		 */

		public static void PrintLogFunction (string domain, LogLevelFlags level, =
string message, IntPtr data)
		{
			Console.WriteLine ("Domain: '{0}' Level: {1}", domain, level);
			Console.WriteLine ("Message: {0}", message);
		}

		public static void PrintTraceLogFunction (string domain, LogLevelFlags le=
vel, string message, IntPtr data)
		{
			PrintLogFunction (domain, level, message, data);
			Console.WriteLine ("Trace follows:\n{0}", new System.Diagnostics.StackTr=
ace ());
		}
	}
}


--=-PUx0iMpQpo+GKMi5mld6--