2002-12-10 05:41:56 +00:00
|
|
|
// Log.cs - Wrapper for message logging functions
|
|
|
|
//
|
|
|
|
// Authors:
|
|
|
|
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// (c) 2002 Gonzalo Paniagua
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace GLib {
|
|
|
|
|
|
|
|
using System;
|
2002-12-12 06:51:47 +00:00
|
|
|
using System.Collections;
|
2002-12-10 05:41:56 +00:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
public delegate void LogFunc (string log_domain,
|
|
|
|
LogLevelFlags log_level,
|
2002-12-12 06:51:47 +00:00
|
|
|
string message);
|
2002-12-10 05:41:56 +00:00
|
|
|
|
|
|
|
public delegate void PrintFunc (string message);
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
public enum LogLevelFlags : uint
|
|
|
|
{
|
|
|
|
/* log flags */
|
|
|
|
FlagRecursion = 1 << 0,
|
|
|
|
FlagFatal = 1 << 1,
|
|
|
|
|
|
|
|
/* GLib log levels */
|
|
|
|
Error = 1 << 2, /* always fatal */
|
|
|
|
Critical = 1 << 3,
|
|
|
|
Warning = 1 << 4,
|
|
|
|
Message = 1 << 5,
|
|
|
|
Info = 1 << 6,
|
|
|
|
Debug = 1 << 7,
|
|
|
|
|
|
|
|
/* Convenience values */
|
|
|
|
AllButFatal = 253,
|
|
|
|
AllButRecursion = 254,
|
|
|
|
All = 255,
|
|
|
|
|
|
|
|
FlagMask = 3,
|
2002-12-10 07:00:48 +00:00
|
|
|
LevelMask = (uint) 0xFFFFFFFC
|
2002-12-10 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Log {
|
|
|
|
|
2002-12-12 06:51:47 +00:00
|
|
|
static Hashtable handlers;
|
|
|
|
|
|
|
|
static void EnsureHash ()
|
|
|
|
{
|
|
|
|
if (handlers == null)
|
|
|
|
handlers = new Hashtable ();
|
|
|
|
}
|
|
|
|
|
2002-12-10 05:41:56 +00:00
|
|
|
[DllImport("glib-2.0")]
|
|
|
|
static extern void g_logv (string log_domain, LogLevelFlags flags, string message);
|
|
|
|
|
|
|
|
public void WriteLog (string logDomain, LogLevelFlags flags, string format, 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);
|
|
|
|
|
|
|
|
public static uint SetLogHandler (string logDomain,
|
|
|
|
LogLevelFlags flags,
|
2002-12-12 06:51:47 +00:00
|
|
|
LogFunc logFunc)
|
|
|
|
|
2002-12-10 05:41:56 +00:00
|
|
|
{
|
2002-12-12 06:51:47 +00:00
|
|
|
uint result = g_log_set_handler (logDomain, flags, logFunc, IntPtr.Zero);
|
|
|
|
EnsureHash ();
|
|
|
|
handlers [result] = logFunc;
|
|
|
|
|
|
|
|
return result;
|
2002-12-10 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[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)
|
|
|
|
{
|
2002-12-12 06:51:47 +00:00
|
|
|
if (handlers != null && handlers.ContainsKey (handlerID))
|
|
|
|
handlers.Remove (handlerID);
|
|
|
|
|
2002-12-10 05:41:56 +00:00
|
|
|
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)
|
|
|
|
{
|
2002-12-12 06:51:47 +00:00
|
|
|
EnsureHash ();
|
|
|
|
handlers ["PrintHandler"] = handler;
|
|
|
|
|
2002-12-10 05:41:56 +00:00
|
|
|
return g_set_print_handler (handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
[DllImport("glib-2.0")]
|
|
|
|
static extern PrintFunc g_set_printerr_handler (PrintFunc handler);
|
|
|
|
|
|
|
|
public static PrintFunc SetPrintErrorHandler (PrintFunc handler)
|
|
|
|
{
|
2002-12-12 06:51:47 +00:00
|
|
|
EnsureHash ();
|
|
|
|
handlers ["PrintErrorHandler"] = handler;
|
|
|
|
|
2002-12-10 05:41:56 +00:00
|
|
|
return g_set_printerr_handler (handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
[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,
|
2002-12-12 06:51:47 +00:00
|
|
|
string message)
|
|
|
|
|
2002-12-10 05:41:56 +00:00
|
|
|
{
|
2002-12-12 06:51:47 +00:00
|
|
|
g_log_default_handler (logDomain, logLevel, message, IntPtr.Zero);
|
2002-12-10 05:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[DllImport("glib-2.0")]
|
|
|
|
extern static LogLevelFlags g_log_set_always_fatal (LogLevelFlags fatal_mask);
|
|
|
|
|
|
|
|
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, LogLevelFlags fatal_mask);
|
|
|
|
|
|
|
|
public static LogLevelFlags SetAlwaysFatal (string logDomain, LogLevelFlags fatalMask)
|
|
|
|
{
|
|
|
|
return g_log_set_fatal_mask (logDomain, fatalMask);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some common logging methods.
|
|
|
|
*
|
|
|
|
* Sample usage:
|
|
|
|
*
|
|
|
|
* // Print the messages for the NULL domain
|
2002-12-12 06:51:47 +00:00
|
|
|
* LogFunc logFunc = new LogFunc (Log.PrintLogFunction);
|
|
|
|
* Log.SetLogHandler (null, LogLevelFlags.All, logFunc);
|
2002-12-10 05:41:56 +00:00
|
|
|
*
|
|
|
|
* // Print messages and stack trace for Gtk critical messages
|
2002-12-12 06:51:47 +00:00
|
|
|
* logFunc = new LogFunc (Log.PrintTraceLogFunction);
|
|
|
|
* Log.SetLogHandler ("Gtk", LogLevelFlags.Critical, logFunc);
|
2002-12-10 05:41:56 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-12-12 06:51:47 +00:00
|
|
|
public static void PrintLogFunction (string domain, LogLevelFlags level, string message)
|
2002-12-10 05:41:56 +00:00
|
|
|
{
|
|
|
|
Console.WriteLine ("Domain: '{0}' Level: {1}", domain, level);
|
|
|
|
Console.WriteLine ("Message: {0}", message);
|
|
|
|
}
|
|
|
|
|
2002-12-12 06:51:47 +00:00
|
|
|
public static void PrintTraceLogFunction (string domain, LogLevelFlags level, string message)
|
2002-12-10 05:41:56 +00:00
|
|
|
{
|
2002-12-12 06:51:47 +00:00
|
|
|
PrintLogFunction (domain, level, message);
|
2002-12-10 05:41:56 +00:00
|
|
|
Console.WriteLine ("Trace follows:\n{0}", new System.Diagnostics.StackTrace ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|