/[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 165 - (hide annotations) (download)
Thu Jul 22 12:37:13 2010 UTC (10 years, 2 months ago) by william
File size: 11054 byte(s)
fix Granny2 API Log Callback builder bing Garbage collected. GC.KeepAlive(class);

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 158 using gr2lib.core.helpers;
10 william 153
11     namespace gr2lib.core.coretypes.implementation
12     {
13 william 158 #region public class grannylog_messageType : igrannylog_messageType
14     public interface igrannylog_messageType
15     {
16     DateTime LogTimeStamp { get; }
17     string LogTimeStampAsString { get; }
18    
19     NameValuePair<string, granny_log_message_type> Type { get; }
20     NameValuePair<string, granny_log_message_origin> Origin { get; }
21     string Message { get; }
22     }
23     public class grannylog_messageType : igrannylog_messageType
24     {
25    
26     public grannylog_messageType()
27     {
28     _LogTimeStamp = DateTime.Now;
29     _Type = new NameValuePair<string, granny_log_message_type>("default_message", 0);
30     _Origin = new NameValuePair<string, granny_log_message_origin>("default_origin", 0);
31     _Message = "{no message}";
32     }
33    
34     public grannylog_messageType(NameValuePair<string, granny_log_message_type> Type, NameValuePair<string, granny_log_message_origin> Origin, string Message)
35     {
36     _LogTimeStamp = DateTime.Now;
37     this._Type = Type;
38     this._Origin = Origin;
39     this._Message = Message;
40     }
41    
42     #region igrannylog_messageType Members
43    
44     private NameValuePair<string, granny_log_message_type> _Type;
45     private NameValuePair<string, granny_log_message_origin> _Origin;
46     private string _Message;
47    
48     private DateTime _LogTimeStamp;
49     public DateTime LogTimeStamp { get { return _LogTimeStamp; } }
50     public string LogTimeStampAsString { get { return this.LogTimeStamp.ToString("MM/dd/yyyy hh:mm:ss.fff tt"); } }
51     public NameValuePair<string, granny_log_message_type> Type { get { return _Type; } }
52     public NameValuePair<string, granny_log_message_origin> Origin { get { return _Origin; } }
53     public string Message { get { return _Message; } }
54     #endregion
55     }
56     #endregion
57    
58     #region public class granny_log_eventargs : EventArgs
59 william 153 public class granny_log_eventargs : EventArgs
60     {
61     private string message;
62 william 158 private granny_log_message_type type;
63     private granny_log_message_origin origin;
64 william 153 private string _type_string;
65     private string _origin_string;
66    
67 william 158 public granny_log_eventargs(granny_log_message_type type, granny_log_message_origin origin, string message)
68 william 153 {
69     this.type = type;
70     this.origin = origin;
71     this.message = message;
72     this._type_string = coreapi.GrannyGetLogMessageTypeString(type);
73     this._origin_string = coreapi.GrannyGetLogMessageOriginString(origin);
74     }
75 william 158 public granny_log_eventargs(granny_log_message_type type, granny_log_message_origin origin, [MarshalAs(UnmanagedType.LPStr)] string message, string type_string, string origin_string)
76 william 153 {
77     this.type = type;
78     this.origin = origin;
79     this.message = message;
80     this._type_string = type_string;
81     this._origin_string = origin_string;
82     }
83    
84 william 158 public granny_log_message_type Type { get { return type; } }
85     public granny_log_message_origin Origin { get { return origin; } }
86 william 153
87     public string TypeString { get { return _type_string; } }
88     public string OriginString { get { return _origin_string; } }
89     public string Message { get { return message; } }
90     }
91 william 158 #endregion
92 william 153
93 william 158 #region public class granny_log_callback_builder : igranny_log_callback_builder
94 william 156 public class granny_log_callback_builder : igranny_log_callback_builder
95 william 153 {
96 william 165 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
97     public delegate void granny_log_callback_method(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData);
98 william 153
99     public delegate void OnMessageLogged(object sender, granny_log_eventargs e);
100 william 165 public static event OnMessageLogged OnMessageLoggedEventHandler;
101 william 153
102 william 156 internal granny_log_callback _log_call_back;
103 william 165 internal granny_log_callback_method _callback = null;
104 william 156
105 william 165 [StructLayout(LayoutKind.Sequential, Pack = 4), Size(Size = 8)]
106     public struct granny_log_callback
107     {
108     [MarshalAs(UnmanagedType.FunctionPtr)]
109     public granny_log_callback_method Function;
110     public IntPtr UserData;
111     }
112    
113     #region Log Call Back
114 william 158 private void LogCallback(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
115 william 165 {
116 william 153 string TypeString = coreapi.GrannyGetLogMessageTypeString(Type);
117     string OriginString = coreapi.GrannyGetLogMessageOriginString(Origin);
118    
119 william 158 switch (Type)
120     {
121     case granny_log_message_type.GrannyErrorLogMessage:
122     grannylog_messageType _msg_error = new grannylog_messageType(
123     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
124     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
125     Message);
126     this.Error.Add(_msg_error);
127     break;
128     case granny_log_message_type.GrannyIgnoredLogMessage:
129     grannylog_messageType _msg_ignored = new grannylog_messageType(
130     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
131     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
132     Message);
133     this.Ingored.Add(_msg_ignored);
134     break;
135     case granny_log_message_type.GrannyNoteLogMessage:
136     grannylog_messageType _msg_note = new grannylog_messageType(
137     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
138     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
139     Message);
140     this.Note.Add(_msg_note);
141     break;
142     case granny_log_message_type.GrannyOnePastLastMessageType:
143     grannylog_messageType _msg_onepast = new grannylog_messageType(
144     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
145     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
146     Message);
147     this.OnePastLastMessage.Add(_msg_onepast);
148     break;
149     case granny_log_message_type.GrannyWarningLogMessage:
150     grannylog_messageType _msg_warning = new grannylog_messageType(
151     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
152     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
153     Message);
154     this.Warning.Add(_msg_warning);
155     break;
156     default: break;
157     }
158 william 153
159 william 158 if (OnMessageLoggedEventHandler == null)
160 william 156 {
161 william 158 Console.WriteLine();
162     Console.WriteLine("Granny2 API Logger: Logging Message -> ");
163     Console.WriteLine();
164     Console.WriteLine("\tMessage Log Type: {0}\n\tMessage Log Origin: {1}\n\tMessage: {2}", TypeString, OriginString, Message);
165     Console.WriteLine();
166     }
167     else
168     {
169 william 156 OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
170     }
171 william 165 GC.KeepAlive(this);
172    
173 william 153 }
174 william 165 #endregion
175 william 153
176 william 165 public granny_log_callback_builder() { _callback = new granny_log_callback_method(this.LogCallback); }
177 william 156 //public granny_log_callback_builder(coreapi.granny_log_callback_method LogCallBack) { this._callback = LogCallBack; }
178 william 165 public granny_log_callback_builder(OnMessageLogged OnMessageLoggedEvntHandler) { OnMessageLoggedEventHandler = OnMessageLoggedEvntHandler; _callback = new granny_log_callback_method(this.LogCallback); }
179 william 158
180     #region Start Logger
181 william 153 public void StartLogging()
182     {
183     try
184     {
185 william 156 _log_call_back = new granny_log_callback();
186 william 165 _log_call_back.Function = _callback;
187 william 158 _log_call_back.UserData = IntPtr.Zero;
188 william 165 GC.KeepAlive(this);
189 william 153 coreapi.GrannySetLogCallback(ref _log_call_back);
190 william 165
191 william 153 Console.WriteLine("Granny2 API Logger: {0}", "Activated");
192     }
193     catch
194     {
195     Console.WriteLine("Granny2 API Logger: {0}", "Not Activated");
196     }
197    
198     }
199 william 158 #endregion
200    
201     #region Stop Logger
202 william 153 public void StopLogging()
203     {
204 william 156 try
205 william 153 {
206 william 156 _log_call_back = new granny_log_callback();
207 william 158 _log_call_back = new granny_log_callback();
208 william 165 granny_log_callback_method _granny_log_callback_method = null;
209 william 158 _log_call_back.Function = _granny_log_callback_method;
210     _log_call_back.UserData = IntPtr.Zero;
211 william 156 coreapi.GrannySetLogCallback(ref _log_call_back);
212     Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
213 william 153 }
214 william 156 catch
215     {
216     Console.WriteLine("Granny2 API Logger: {0}", "Not Deactivated");
217     }
218 william 153 }
219 william 158 #endregion
220 william 153
221 william 158
222     #region igranny_log_callback_builder Members
223     private List<grannylog_messageType> _Ingored = new List<grannylog_messageType>();
224     private List<grannylog_messageType> _Note = new List<grannylog_messageType>();
225     private List<grannylog_messageType> _Warning = new List<grannylog_messageType>();
226     private List<grannylog_messageType> _Error = new List<grannylog_messageType>();
227     private List<grannylog_messageType> _OnePastLastMessage = new List<grannylog_messageType>();
228    
229     public List<grannylog_messageType> Ingored { get { return _Ingored; } }
230     public List<grannylog_messageType> Note { get { return _Note; } }
231     public List<grannylog_messageType> Warning { get { return _Warning; } }
232     public List<grannylog_messageType> Error { get { return _Error; } }
233     public List<grannylog_messageType> OnePastLastMessage { get { return _OnePastLastMessage; } }
234     #endregion
235 william 153 }
236 william 158 #endregion
237 william 153 }
238    
239 william 158

  ViewVC Help
Powered by ViewVC 1.1.22