1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// Log.cs - Wrapper for message logging functions
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
//
// Copyright (c) 2002 Gonzalo Paniagua
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
namespace GLib {
using System;
using System.Runtime.InteropServices;
public delegate void LogFunc (string log_domain, LogLevelFlags log_level, string message);
public delegate void PrintFunc (string message);
[Flags]
public enum LogLevelFlags : int
{
/* 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,
LevelMask = unchecked ((int) 0xFFFFFFFC)
}
public class Log {
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
delegate void LogFuncNative (IntPtr log_domain, LogLevelFlags flags, IntPtr message, IntPtr user_data);
static LogFuncNative native_handler;
static void NativeCallback (IntPtr log_domain_native, LogLevelFlags flags, IntPtr message_native, IntPtr user_data)
{
if (user_data == IntPtr.Zero)
return;
string log_domain = Marshaller.Utf8PtrToString (log_domain_native);
string message = Marshaller.Utf8PtrToString (message_native);
GCHandle gch = (GCHandle) user_data;
LogFunc func = gch.Target as LogFunc;
if (func != null)
func (log_domain, flags, message);
}
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
delegate void PrintFuncNative (IntPtr message);
class PrintHelper {
PrintFuncNative native;
PrintFunc managed;
public PrintHelper (PrintFuncNative native)
{
this.native = native;
}
public PrintHelper (PrintFunc managed)
{
this.managed = managed;
GCHandle.Alloc (this);
}
void Callback (IntPtr nmessage)
{
string message = Marshaller.Utf8PtrToString (nmessage);
managed (message);
}
void Invoke (string message)
{
IntPtr nmessage = Marshaller.StringToPtrGStrdup (message);
native (nmessage);
Marshaller.Free (nmessage);
}
public PrintFuncNative Handler {
get { return new PrintFuncNative (Callback); }
}
public PrintFunc Invoker {
get { return new PrintFunc (Invoke); }
}
}
static System.Collections.Generic.Dictionary<uint, GCHandle> handlers;
static void EnsureHash ()
{
if (handlers == null)
handlers = new System.Collections.Generic.Dictionary<uint, GCHandle> ();
}
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern void g_logv (IntPtr log_domain, LogLevelFlags flags, IntPtr message);
public void WriteLog (string logDomain, LogLevelFlags flags, string format, params object [] args)
{
IntPtr ndom = Marshaller.StringToPtrGStrdup (logDomain);
IntPtr nmessage = Marshaller.StringToPtrGStrdup (String.Format (format, args));
g_logv (ndom, flags, nmessage);
Marshaller.Free (ndom);
Marshaller.Free (nmessage);
}
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern uint g_log_set_handler (IntPtr log_domain, LogLevelFlags flags, LogFuncNative log_func, IntPtr user_data);
public static uint SetLogHandler (string logDomain, LogLevelFlags flags, LogFunc logFunc)
{
if (native_handler == null)
native_handler = new LogFuncNative (NativeCallback);
IntPtr ndom = Marshaller.StringToPtrGStrdup (logDomain);
GCHandle gch = GCHandle.Alloc (logFunc);
uint result = g_log_set_handler (ndom, flags, native_handler, (IntPtr) gch);
Marshaller.Free (ndom);
EnsureHash ();
handlers [result] = gch;
return result;
}
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern uint g_log_remove_handler (IntPtr log_domain, uint handler_id);
public static void RemoveLogHandler (string logDomain, uint handlerID)
{
if (handlers != null && handlers.ContainsKey (handlerID)) {
handlers [handlerID].Free ();
handlers.Remove (handlerID);
}
IntPtr ndom = Marshaller.StringToPtrGStrdup (logDomain);
g_log_remove_handler (ndom, handlerID);
Marshaller.Free (ndom);
}
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern PrintFuncNative g_set_print_handler (PrintFuncNative handler);
public static PrintFunc SetPrintHandler (PrintFunc handler)
{
PrintHelper helper = new PrintHelper (handler);
PrintFuncNative prev = g_set_print_handler (helper.Handler);
helper = new PrintHelper (prev);
return helper.Invoker;
}
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern PrintFuncNative g_set_printerr_handler (PrintFuncNative handler);
public static PrintFunc SetPrintErrorHandler (PrintFunc handler)
{
PrintHelper helper = new PrintHelper (handler);
PrintFuncNative prev = g_set_printerr_handler (helper.Handler);
helper = new PrintHelper (prev);
return helper.Invoker;
}
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern void g_log_default_handler (IntPtr log_domain, LogLevelFlags log_level, IntPtr message, IntPtr unused_data);
public static void DefaultHandler (string logDomain, LogLevelFlags logLevel, string message)
{
IntPtr ndom = Marshaller.StringToPtrGStrdup (logDomain);
IntPtr nmess = Marshaller.StringToPtrGStrdup (message);
g_log_default_handler (ndom, logLevel, nmess, IntPtr.Zero);
Marshaller.Free (ndom);
Marshaller.Free (nmess);
}
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
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 (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
extern static LogLevelFlags g_log_set_fatal_mask (IntPtr log_domain, LogLevelFlags fatal_mask);
public static LogLevelFlags SetAlwaysFatal (string logDomain, LogLevelFlags fatalMask)
{
IntPtr ndom = Marshaller.StringToPtrGStrdup (logDomain);
LogLevelFlags result = g_log_set_fatal_mask (ndom, fatalMask);
Marshaller.Free (ndom);
return result;
}
class Invoker {
LogFuncNative native;
public Invoker (LogFuncNative native)
{
this.native = native;
}
void Invoke (string log_domain, LogLevelFlags flags, string message)
{
IntPtr ndom = Marshaller.StringToPtrGStrdup (log_domain);
IntPtr nmess = Marshaller.StringToPtrGStrdup (message);
native (ndom, flags, nmess, IntPtr.Zero);
Marshaller.Free (ndom);
Marshaller.Free (nmess);
}
public LogFunc Handler {
get { return new LogFunc (Invoke); }
}
}
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
extern static LogFuncNative g_log_set_default_handler (LogFuncNative log_func, IntPtr user_data);
public static LogFunc SetDefaultHandler (LogFunc log_func)
{
if (native_handler == null)
native_handler = new LogFuncNative (NativeCallback);
LogFuncNative prev = g_log_set_default_handler (native_handler, (IntPtr) GCHandle.Alloc (log_func));
if (prev == null)
return null;
Invoker invoker = new Invoker (prev);
return invoker.Handler;
}
/*
* Some common logging methods.
*
* Sample usage:
*
* // Print the messages for the NULL domain
* LogFunc logFunc = new LogFunc (Log.PrintLogFunction);
* Log.SetLogHandler (null, LogLevelFlags.All, logFunc);
*
* // Print messages and stack trace for Gtk critical messages
* logFunc = new LogFunc (Log.PrintTraceLogFunction);
* Log.SetLogHandler ("Gtk", LogLevelFlags.Critical, logFunc);
*
*/
public static void PrintLogFunction (string domain, LogLevelFlags level, string message)
{
Console.WriteLine ("Domain: '{0}' Level: {1}", domain, level);
Console.WriteLine ("Message: {0}", message);
}
public static void PrintTraceLogFunction (string domain, LogLevelFlags level, string message)
{
PrintLogFunction (domain, level, message);
Console.WriteLine ("Trace follows:\n{0}", new System.Diagnostics.StackTrace ());
}
}
}
|