/[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/20 10:27:30	155
+++ trunk/gr2lib/core/coretypes/implementation/granny_log_callback_builder.cs	2010/07/22 07:38:36	156
@@ -4,18 +4,20 @@
 using System.Text;
 using gr2lib.core.typedefs;
 using gr2lib.core.coretypes.native;
+using gr2lib.core.interfaces;
+using System.Runtime.InteropServices;
 
 namespace gr2lib.core.coretypes.implementation
 {
     public class granny_log_eventargs : EventArgs
     {
         private string message;
-        private granny_log_message_type type;
-        private granny_log_message_origin origin;
+        private int type;
+        private int origin;
         private string _type_string;
         private string _origin_string;
 
-        public granny_log_eventargs(granny_log_message_type type, granny_log_message_origin origin, string message)
+        public granny_log_eventargs(int type, int origin, string message)
         {
             this.type = type;
             this.origin = origin;
@@ -23,7 +25,7 @@
             this._type_string = coreapi.GrannyGetLogMessageTypeString(type);
             this._origin_string = coreapi.GrannyGetLogMessageOriginString(origin);
         }
-        public granny_log_eventargs(granny_log_message_type type, granny_log_message_origin origin, string message, string type_string, string origin_string)
+        public granny_log_eventargs(int type, int origin, [MarshalAs(UnmanagedType.LPStr)] string message, string type_string, string origin_string)
         {
             this.type = type;
             this.origin = origin;
@@ -32,21 +34,24 @@
             this._origin_string = origin_string;
         }
 
-        public granny_log_message_type Type { get { return type; } }
-        public granny_log_message_origin Origin { get { return origin; } }
+        public int Type { get { return type; } }
+        public int Origin { get { return origin; } }
 
         public string TypeString { get { return _type_string; } }
         public string OriginString { get { return _origin_string; } }
         public string Message { get { return message; } }
     }
 
-    public class granny_log_callback_builder
+    public class granny_log_callback_builder : igranny_log_callback_builder
     {
 
         public delegate void OnMessageLogged(object sender, granny_log_eventargs e);
         public event OnMessageLogged OnMessageLoggedEventHandler;
 
-        private void LogCallback(granny_log_message_type Type, granny_log_message_origin Origin, string Message, IntPtr UserData)
+        internal granny_log_callback _log_call_back;
+        private coreapi.granny_log_callback_method _callback = null;
+
+        private void LogCallback(int Type, int Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
         {
             string TypeString = coreapi.GrannyGetLogMessageTypeString(Type);
             string OriginString = coreapi.GrannyGetLogMessageOriginString(Origin);
@@ -57,22 +62,36 @@
             Console.WriteLine("\tMessage Log Type: {0}\n\tMessage Log Origin: {1}\n\tMessage: {2}", TypeString, OriginString, Message);
             Console.WriteLine();
 
-            //if (OnMessageLoggedEventHandler != null)
-            //{
-            //    OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
-            //}
+            if (OnMessageLoggedEventHandler != null)
+            {
+                OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
+            }
+            //GC.KeepAlive(this);
         }
 
-        public granny_log_callback_builder() { }
+        public granny_log_callback_builder() { _callback = new coreapi.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 void StartLogging()
         {
             try
             {
-                granny_log_callback _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.UserData = IntPtr.Zero;
+                _log_call_back = new granny_log_callback();
+                unsafe
+                {
+                    coreapi.granny_log_callback_method _granny_log_callback_method = new coreapi.granny_log_callback_method(this.LogCallback);
+
+                    //IntPtr _granny_log_callback_methodPtr = Marshal.GetFunctionPointerForDelegate(_granny_log_callback_method);
+                    //_log_call_back.Function = _granny_log_callback_methodPtr;
+                    _log_call_back.Function = _granny_log_callback_method;
+
+                    _log_call_back.UserData = IntPtr.Zero;
+                }
+
+                //int _log_call_back_size = Marshal.SizeOf(_log_call_back);
+                //IntPtr _log_call_backPtr = Marshal.AllocHGlobal(_log_call_back_size);
+                //Marshal.StructureToPtr(_log_call_back_size, _log_call_backPtr, true);
+
                 coreapi.GrannySetLogCallback(ref _log_call_back);
                 Console.WriteLine("Granny2 API Logger: {0}", "Activated");
             }
@@ -84,19 +103,32 @@
         }
         public void StopLogging()
         {
-             try
+            try
+            {
+                _log_call_back = new granny_log_callback();
+                unsafe
+                {
+                    coreapi.granny_log_callback_method _granny_log_callback_method = new coreapi.granny_log_callback_method(this.LogCallback);
+
+                    //IntPtr _granny_log_callback_methodPtr = Marshal.GetFunctionPointerForDelegate(_granny_log_callback_method);
+                    //_log_call_back.Function = _granny_log_callback_methodPtr;
+                    _log_call_back.Function = _granny_log_callback_method;
+
+                    _log_call_back.UserData = IntPtr.Zero; 
+                }
+
+
+                //int _log_call_back_size = Marshal.SizeOf(_log_call_back);
+                //IntPtr _log_call_backPtr = Marshal.AllocHGlobal(_log_call_back_size);
+                //Marshal.StructureToPtr(_log_call_back_size, _log_call_backPtr, true);
+
+                coreapi.GrannySetLogCallback(ref _log_call_back);
+                Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
+            }
+            catch
             {
-            granny_log_callback _log_call_back = new granny_log_callback();
-            coreapi.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);
-            Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
+                Console.WriteLine("Granny2 API Logger: {0}", "Not Deactivated");
             }
-             catch
-             {
-                 Console.WriteLine("Granny2 API Logger: {0}", "Not Deactivated");
-             }
         }
 
     }

 

  ViewVC Help
Powered by ViewVC 1.1.22