/[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 191 - (hide annotations) (download)
Sat Jul 24 06:31:50 2010 UTC (10 years ago) by william
File size: 14702 byte(s)
In coreapi:
*Add more texture methods
*Move everthing into related classes, to group related methods,etc
*Update all classes to use these grouped classes

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 william 187 /// <summary>
15     /// igrannylog_messageType interface
16     /// </summary>
17 william 158 public interface igrannylog_messageType
18     {
19 william 187 /// <summary>
20     /// Gets LogTimeStamp
21     /// </summary>
22 william 158 DateTime LogTimeStamp { get; }
23 william 187 /// <summary>
24     /// Gets LogTimeStamp as string
25     /// </summary>
26 william 158 string LogTimeStampAsString { get; }
27    
28 william 187 /// <summary>
29     /// Message Type as NameValuePair
30     /// </summary>
31 william 158 NameValuePair<string, granny_log_message_type> Type { get; }
32 william 187 /// <summary>
33     /// Message Origin as NameValuePair
34     /// </summary>
35 william 158 NameValuePair<string, granny_log_message_origin> Origin { get; }
36 william 187 /// <summary>
37     /// get the message
38     /// </summary>
39 william 158 string Message { get; }
40     }
41 william 187 /// <summary>
42     /// grannylog_messageType class
43     /// </summary>
44 william 158 public class grannylog_messageType : igrannylog_messageType
45     {
46 william 187 /// <summary>
47     /// default constructor
48     /// </summary>
49 william 158 public grannylog_messageType()
50     {
51     _LogTimeStamp = DateTime.Now;
52     _Type = new NameValuePair<string, granny_log_message_type>("default_message", 0);
53     _Origin = new NameValuePair<string, granny_log_message_origin>("default_origin", 0);
54     _Message = "{no message}";
55     }
56 william 187 /// <summary>
57     /// default constructor - specify: Type, Origin, Message)
58     /// </summary>
59 william 158 public grannylog_messageType(NameValuePair<string, granny_log_message_type> Type, NameValuePair<string, granny_log_message_origin> Origin, string Message)
60     {
61     _LogTimeStamp = DateTime.Now;
62     this._Type = Type;
63     this._Origin = Origin;
64     this._Message = Message;
65     }
66    
67     #region igrannylog_messageType Members
68    
69     private NameValuePair<string, granny_log_message_type> _Type;
70     private NameValuePair<string, granny_log_message_origin> _Origin;
71     private string _Message;
72    
73     private DateTime _LogTimeStamp;
74 william 187
75     /// <summary>
76     /// Gets LogTimeStamp
77     /// </summary>
78 william 158 public DateTime LogTimeStamp { get { return _LogTimeStamp; } }
79 william 187 /// <summary>
80     /// Gets LogTimeStamp as string
81     /// </summary>
82     public string LogTimeStampAsString { get { return this.LogTimeStamp.ToString("MM/dd/yyyy hh:mm:ss.fff tt"); } }
83     /// <summary>
84     /// Message Type as NameValuePair
85     /// </summary>
86 william 158 public NameValuePair<string, granny_log_message_type> Type { get { return _Type; } }
87 william 187 /// <summary>
88     /// Message Origin as NameValuePair
89     /// </summary>
90 william 158 public NameValuePair<string, granny_log_message_origin> Origin { get { return _Origin; } }
91 william 187 /// <summary>
92     /// get the message
93     /// </summary>
94 william 158 public string Message { get { return _Message; } }
95     #endregion
96     }
97     #endregion
98    
99     #region public class granny_log_eventargs : EventArgs
100 william 187 /// <summary>
101     /// granny_log_eventargs class
102     /// </summary>
103 william 153 public class granny_log_eventargs : EventArgs
104     {
105     private string message;
106 william 158 private granny_log_message_type type;
107     private granny_log_message_origin origin;
108 william 187 /// <summary>
109     /// default constructor - specify: type, origin, message
110     /// </summary>
111     /// <param name="type"></param>
112     /// <param name="origin"></param>
113     /// <param name="message"></param>
114     public granny_log_eventargs(granny_log_message_type type, granny_log_message_origin origin, [MarshalAs(UnmanagedType.LPStr)] string message)
115 william 153 {
116     this.type = type;
117     this.origin = origin;
118     this.message = message;
119     }
120    
121 william 187 /// <summary>
122     /// Get Message Log Type value
123     /// </summary>
124 william 158 public granny_log_message_type Type { get { return type; } }
125 william 187
126     /// <summary>
127     /// Get Message Log Origin value
128     /// </summary>
129 william 158 public granny_log_message_origin Origin { get { return origin; } }
130 william 153
131 william 187 /// <summary>
132     /// Get Message Log Type name
133     /// </summary>
134 william 191 public string TypeString { get { return coreapi.EnumToStringSupport.GrannyGetLogMessageTypeString(type); } }
135 william 187 /// <summary>
136     /// Get Message Log Origin name
137     /// </summary>
138 william 191 public string OriginString { get { return coreapi.EnumToStringSupport.GrannyGetLogMessageOriginString(origin); } }
139 william 187 /// <summary>
140     /// Get the Message
141     /// </summary>
142 william 153 public string Message { get { return message; } }
143     }
144 william 158 #endregion
145 william 153
146 william 158 #region public class granny_log_callback_builder : igranny_log_callback_builder
147 william 187 /// <summary>
148     /// granny_log_callback_builder class
149     /// </summary>
150 william 156 public class granny_log_callback_builder : igranny_log_callback_builder
151 william 153 {
152 william 187 /// <summary>
153     /// granny_log_callback_method delegate
154     /// </summary>
155     /// <param name="Type">granny_log_message_type</param>
156     /// <param name="Origin">granny_log_message_origin</param>
157     /// <param name="Message">The log message</param>
158     /// <param name="UserData">Pointer to UserData, in this implementation is always: IntPtr.Zero</param>
159 william 165 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
160     public delegate void granny_log_callback_method(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData);
161 william 153
162 william 187 /// <summary>
163     /// OnMessageLogged delegate
164     /// </summary>
165     /// <param name="sender">object</param>
166     /// <param name="e">granny_log_eventargs</param>
167 william 153 public delegate void OnMessageLogged(object sender, granny_log_eventargs e);
168 william 187 /// <summary>
169     /// Handler for OnMessageLogged delegate, use this if you wish to handle Granny2 Log Events in your own code
170     /// </summary>
171 william 166 public event OnMessageLogged OnMessageLoggedEventHandler;
172 william 153
173 william 156 internal granny_log_callback _log_call_back;
174 william 165 internal granny_log_callback_method _callback = null;
175 william 156
176 william 187 /// <summary>
177     /// granny_log_callback
178     /// </summary>
179 william 165 [StructLayout(LayoutKind.Sequential, Pack = 4), Size(Size = 8)]
180     public struct granny_log_callback
181     {
182 william 187 /// <summary>
183     /// the fucntion to call when something is logged
184     /// </summary>
185 william 165 [MarshalAs(UnmanagedType.FunctionPtr)]
186     public granny_log_callback_method Function;
187 william 187 /// <summary>
188     /// pointer to UserData, in this implementation is always: IntPtr.Zero
189     /// </summary>
190 william 165 public IntPtr UserData;
191     }
192    
193     #region Log Call Back
194 william 158 private void LogCallback(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
195 william 165 {
196 william 191 string TypeString = coreapi.EnumToStringSupport.GrannyGetLogMessageTypeString(Type);
197     string OriginString = coreapi.EnumToStringSupport.GrannyGetLogMessageOriginString(Origin);
198 william 153
199 william 158 switch (Type)
200     {
201     case granny_log_message_type.GrannyErrorLogMessage:
202     grannylog_messageType _msg_error = new grannylog_messageType(
203     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
204     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
205     Message);
206     this.Error.Add(_msg_error);
207     break;
208     case granny_log_message_type.GrannyIgnoredLogMessage:
209     grannylog_messageType _msg_ignored = new grannylog_messageType(
210     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
211     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
212     Message);
213 william 187 this.Ignored.Add(_msg_ignored);
214 william 158 break;
215     case granny_log_message_type.GrannyNoteLogMessage:
216     grannylog_messageType _msg_note = new grannylog_messageType(
217     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
218     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
219     Message);
220     this.Note.Add(_msg_note);
221     break;
222     case granny_log_message_type.GrannyOnePastLastMessageType:
223     grannylog_messageType _msg_onepast = new grannylog_messageType(
224     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
225     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
226     Message);
227     this.OnePastLastMessage.Add(_msg_onepast);
228     break;
229     case granny_log_message_type.GrannyWarningLogMessage:
230     grannylog_messageType _msg_warning = new grannylog_messageType(
231     new NameValuePair<string, granny_log_message_type>(TypeString, Type),
232     new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
233     Message);
234     this.Warning.Add(_msg_warning);
235     break;
236     default: break;
237     }
238 william 153
239 william 158 if (OnMessageLoggedEventHandler == null)
240 william 156 {
241 william 158 Console.WriteLine();
242     Console.WriteLine("Granny2 API Logger: Logging Message -> ");
243     Console.WriteLine();
244     Console.WriteLine("\tMessage Log Type: {0}\n\tMessage Log Origin: {1}\n\tMessage: {2}", TypeString, OriginString, Message);
245     Console.WriteLine();
246     }
247     else
248     {
249 william 156 OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
250     }
251 william 165 GC.KeepAlive(this);
252    
253 william 153 }
254 william 165 #endregion
255 william 153
256 william 187 /// <summary>
257     /// default constructor
258     /// </summary>
259 william 165 public granny_log_callback_builder() { _callback = new granny_log_callback_method(this.LogCallback); }
260 william 156 //public granny_log_callback_builder(coreapi.granny_log_callback_method LogCallBack) { this._callback = LogCallBack; }
261 william 187 /// <summary>
262     /// default constructor - specify: OnMessageLoggedEvntHandler
263     /// </summary>
264     /// <param name="OnMessageLoggedEvntHandler">delegate that will be called when ever something is logged (use this if you wish to handle Granny2 Log Events in your own code)</param>
265 william 165 public granny_log_callback_builder(OnMessageLogged OnMessageLoggedEvntHandler) { OnMessageLoggedEventHandler = OnMessageLoggedEvntHandler; _callback = new granny_log_callback_method(this.LogCallback); }
266 william 158
267     #region Start Logger
268 william 187 /// <summary>
269     /// When called will initialize the logger
270     /// </summary>
271 william 153 public void StartLogging()
272     {
273     try
274     {
275 william 156 _log_call_back = new granny_log_callback();
276 william 165 _log_call_back.Function = _callback;
277 william 158 _log_call_back.UserData = IntPtr.Zero;
278 william 165 GC.KeepAlive(this);
279 william 191 coreapi.LoggingSupport.GrannySetLogCallback(ref _log_call_back);
280 william 165
281 william 153 Console.WriteLine("Granny2 API Logger: {0}", "Activated");
282     }
283     catch
284     {
285     Console.WriteLine("Granny2 API Logger: {0}", "Not Activated");
286     }
287    
288     }
289 william 158 #endregion
290    
291     #region Stop Logger
292 william 187 /// <summary>
293     /// When called will destroy the logger
294     /// </summary>
295 william 153 public void StopLogging()
296     {
297 william 156 try
298 william 153 {
299 william 156 _log_call_back = new granny_log_callback();
300 william 158 _log_call_back = new granny_log_callback();
301 william 165 granny_log_callback_method _granny_log_callback_method = null;
302 william 158 _log_call_back.Function = _granny_log_callback_method;
303     _log_call_back.UserData = IntPtr.Zero;
304 william 191 coreapi.LoggingSupport.GrannySetLogCallback(ref _log_call_back);
305 william 156 Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
306 william 153 }
307 william 156 catch
308     {
309     Console.WriteLine("Granny2 API Logger: {0}", "Not Deactivated");
310     }
311 william 153 }
312 william 158 #endregion
313 william 153
314 william 158
315     #region igranny_log_callback_builder Members
316     private List<grannylog_messageType> _Ingored = new List<grannylog_messageType>();
317     private List<grannylog_messageType> _Note = new List<grannylog_messageType>();
318     private List<grannylog_messageType> _Warning = new List<grannylog_messageType>();
319     private List<grannylog_messageType> _Error = new List<grannylog_messageType>();
320     private List<grannylog_messageType> _OnePastLastMessage = new List<grannylog_messageType>();
321    
322 william 187 /// <summary>
323     /// Get the Ignored Log
324     /// </summary>
325     public List<grannylog_messageType> Ignored { get { return _Ingored; } }
326     /// <summary>
327     /// Get the Note Log
328     /// </summary>
329 william 158 public List<grannylog_messageType> Note { get { return _Note; } }
330 william 187 /// <summary>
331     /// Get the Warning Log
332     /// </summary>
333 william 158 public List<grannylog_messageType> Warning { get { return _Warning; } }
334 william 187 /// <summary>
335     /// Get the Error Log
336     /// </summary>
337 william 158 public List<grannylog_messageType> Error { get { return _Error; } }
338 william 187 /// <summary>
339     /// Get the OnePastLastMessage Log
340     /// </summary>
341 william 158 public List<grannylog_messageType> OnePastLastMessage { get { return _OnePastLastMessage; } }
342     #endregion
343 william 153 }
344 william 158 #endregion
345 william 153 }
346    
347 william 158

  ViewVC Help
Powered by ViewVC 1.1.22