/[gr2lib]/trunk/gr2lib/core/coretypes/implementation/granny_log_callback_builder.cs
ViewVC logotype

Diff of /trunk/gr2lib/core/coretypes/implementation/granny_log_callback_builder.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/gr2lib/core/coretypes/implementation/granny_log_callback_builder.cs	2010/07/22 10:23:14	164
+++ trunk/gr2lib/core/coretypes/implementation/granny_log_callback_builder.cs	2010/07/22 12:37:13	165
@@ -93,15 +93,26 @@
     #region public class granny_log_callback_builder : igranny_log_callback_builder
     public class granny_log_callback_builder : igranny_log_callback_builder
     {
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        public delegate void granny_log_callback_method(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData);
 
         public delegate void OnMessageLogged(object sender, granny_log_eventargs e);
-        public event OnMessageLogged OnMessageLoggedEventHandler;
+        public static event OnMessageLogged OnMessageLoggedEventHandler;
 
         internal granny_log_callback _log_call_back;
-        private coreapi.granny_log_callback_method _callback = null;
+        internal granny_log_callback_method _callback = null;
 
-        private void LogCallback(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
+        [StructLayout(LayoutKind.Sequential, Pack = 4), Size(Size = 8)]
+        public struct granny_log_callback
         {
+            [MarshalAs(UnmanagedType.FunctionPtr)]
+            public granny_log_callback_method Function;
+            public IntPtr UserData;
+        }
+
+        #region Log Call Back
+        private void LogCallback(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
+        {          
             string TypeString = coreapi.GrannyGetLogMessageTypeString(Type);
             string OriginString = coreapi.GrannyGetLogMessageOriginString(Origin);
 
@@ -157,11 +168,14 @@
             {          
                 OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
             }
+            GC.KeepAlive(this);
+
         }
+        #endregion
 
-        public granny_log_callback_builder() { _callback = new coreapi.granny_log_callback_method(this.LogCallback); }
+        public granny_log_callback_builder() { _callback = new granny_log_callback_method(this.LogCallback); }
         //public granny_log_callback_builder(coreapi.granny_log_callback_method LogCallBack) { this._callback = LogCallBack; }
-        public granny_log_callback_builder(OnMessageLogged OnMessageLoggedEventHandler) { this.OnMessageLoggedEventHandler = OnMessageLoggedEventHandler; }
+        public granny_log_callback_builder(OnMessageLogged OnMessageLoggedEvntHandler) { OnMessageLoggedEventHandler = OnMessageLoggedEvntHandler; _callback = new granny_log_callback_method(this.LogCallback); }
 
         #region Start Logger
         public void StartLogging()
@@ -169,10 +183,11 @@
             try
             {
                 _log_call_back = new granny_log_callback();
-                coreapi.granny_log_callback_method _granny_log_callback_method = new coreapi.granny_log_callback_method(this.LogCallback);
-                _log_call_back.Function = _granny_log_callback_method;
+                _log_call_back.Function = _callback;
                 _log_call_back.UserData = IntPtr.Zero;
+                GC.KeepAlive(this);
                 coreapi.GrannySetLogCallback(ref _log_call_back);
+                
                 Console.WriteLine("Granny2 API Logger: {0}", "Activated");
             }
             catch
@@ -190,11 +205,10 @@
             {
                 _log_call_back = new granny_log_callback();
                 _log_call_back = new granny_log_callback();
-                coreapi.granny_log_callback_method _granny_log_callback_method = null;
+                granny_log_callback_method _granny_log_callback_method = null;
                 _log_call_back.Function = _granny_log_callback_method;
                 _log_call_back.UserData = IntPtr.Zero;
                 coreapi.GrannySetLogCallback(ref _log_call_back);
-                coreapi.GrannySetLogCallback(ref _log_call_back);
                 Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
             }
             catch

 

  ViewVC Help
Powered by ViewVC 1.1.22