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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 156 - (hide annotations) (download)
Thu Jul 22 07:38:36 2010 UTC (10 years, 2 months ago) by william
File size: 5706 byte(s)
commit full-support for logging messages from Granny2 API

It was neccessary to write the delegate like this in coreapi:

	[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
	public delegate void granny_log_callback_method(int Type, int Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData);

1 william 153 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using gr2lib.core.typedefs;
6     using gr2lib.core.coretypes.native;
7 william 156 using gr2lib.core.interfaces;
8     using System.Runtime.InteropServices;
9 william 153
10     namespace gr2lib.core.coretypes.implementation
11     {
12     public class granny_log_eventargs : EventArgs
13     {
14     private string message;
15 william 156 private int type;
16     private int origin;
17 william 153 private string _type_string;
18     private string _origin_string;
19    
20 william 156 public granny_log_eventargs(int type, int origin, string message)
21 william 153 {
22     this.type = type;
23     this.origin = origin;
24     this.message = message;
25     this._type_string = coreapi.GrannyGetLogMessageTypeString(type);
26     this._origin_string = coreapi.GrannyGetLogMessageOriginString(origin);
27     }
28 william 156 public granny_log_eventargs(int type, int origin, [MarshalAs(UnmanagedType.LPStr)] string message, string type_string, string origin_string)
29 william 153 {
30     this.type = type;
31     this.origin = origin;
32     this.message = message;
33     this._type_string = type_string;
34     this._origin_string = origin_string;
35     }
36    
37 william 156 public int Type { get { return type; } }
38     public int Origin { get { return origin; } }
39 william 153
40     public string TypeString { get { return _type_string; } }
41     public string OriginString { get { return _origin_string; } }
42     public string Message { get { return message; } }
43     }
44    
45 william 156 public class granny_log_callback_builder : igranny_log_callback_builder
46 william 153 {
47    
48     public delegate void OnMessageLogged(object sender, granny_log_eventargs e);
49     public event OnMessageLogged OnMessageLoggedEventHandler;
50    
51 william 156 internal granny_log_callback _log_call_back;
52     private coreapi.granny_log_callback_method _callback = null;
53    
54     private void LogCallback(int Type, int Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
55 william 153 {
56     string TypeString = coreapi.GrannyGetLogMessageTypeString(Type);
57     string OriginString = coreapi.GrannyGetLogMessageOriginString(Origin);
58    
59     Console.WriteLine();
60     Console.WriteLine("Granny2 API Logger: Logging Message -> ");
61     Console.WriteLine();
62     Console.WriteLine("\tMessage Log Type: {0}\n\tMessage Log Origin: {1}\n\tMessage: {2}", TypeString, OriginString, Message);
63     Console.WriteLine();
64    
65 william 156 if (OnMessageLoggedEventHandler != null)
66     {
67     OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
68     }
69     //GC.KeepAlive(this);
70 william 153 }
71    
72 william 156 public granny_log_callback_builder() { _callback = new coreapi.granny_log_callback_method(this.LogCallback); }
73     //public granny_log_callback_builder(coreapi.granny_log_callback_method LogCallBack) { this._callback = LogCallBack; }
74 william 153 public granny_log_callback_builder(OnMessageLogged OnMessageLoggedEventHandler) { this.OnMessageLoggedEventHandler = OnMessageLoggedEventHandler; }
75     public void StartLogging()
76     {
77     try
78     {
79 william 156 _log_call_back = new granny_log_callback();
80     unsafe
81     {
82     coreapi.granny_log_callback_method _granny_log_callback_method = new coreapi.granny_log_callback_method(this.LogCallback);
83    
84     //IntPtr _granny_log_callback_methodPtr = Marshal.GetFunctionPointerForDelegate(_granny_log_callback_method);
85     //_log_call_back.Function = _granny_log_callback_methodPtr;
86     _log_call_back.Function = _granny_log_callback_method;
87    
88     _log_call_back.UserData = IntPtr.Zero;
89     }
90    
91     //int _log_call_back_size = Marshal.SizeOf(_log_call_back);
92     //IntPtr _log_call_backPtr = Marshal.AllocHGlobal(_log_call_back_size);
93     //Marshal.StructureToPtr(_log_call_back_size, _log_call_backPtr, true);
94    
95 william 153 coreapi.GrannySetLogCallback(ref _log_call_back);
96     Console.WriteLine("Granny2 API Logger: {0}", "Activated");
97     }
98     catch
99     {
100     Console.WriteLine("Granny2 API Logger: {0}", "Not Activated");
101     }
102    
103     }
104     public void StopLogging()
105     {
106 william 156 try
107 william 153 {
108 william 156 _log_call_back = new granny_log_callback();
109     unsafe
110     {
111     coreapi.granny_log_callback_method _granny_log_callback_method = new coreapi.granny_log_callback_method(this.LogCallback);
112    
113     //IntPtr _granny_log_callback_methodPtr = Marshal.GetFunctionPointerForDelegate(_granny_log_callback_method);
114     //_log_call_back.Function = _granny_log_callback_methodPtr;
115     _log_call_back.Function = _granny_log_callback_method;
116    
117     _log_call_back.UserData = IntPtr.Zero;
118     }
119    
120    
121     //int _log_call_back_size = Marshal.SizeOf(_log_call_back);
122     //IntPtr _log_call_backPtr = Marshal.AllocHGlobal(_log_call_back_size);
123     //Marshal.StructureToPtr(_log_call_back_size, _log_call_backPtr, true);
124    
125     coreapi.GrannySetLogCallback(ref _log_call_back);
126     Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
127 william 153 }
128 william 156 catch
129     {
130     Console.WriteLine("Granny2 API Logger: {0}", "Not Deactivated");
131     }
132 william 153 }
133    
134     }
135     }
136    

  ViewVC Help
Powered by ViewVC 1.1.22