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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 191 - (show annotations) (download)
Sat Jul 24 06:31:50 2010 UTC (9 years, 1 month 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 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 using gr2lib.core.interfaces;
8 using System.Runtime.InteropServices;
9 using gr2lib.core.helpers;
10
11 namespace gr2lib.core.coretypes.implementation
12 {
13 #region public class grannylog_messageType : igrannylog_messageType
14 /// <summary>
15 /// igrannylog_messageType interface
16 /// </summary>
17 public interface igrannylog_messageType
18 {
19 /// <summary>
20 /// Gets LogTimeStamp
21 /// </summary>
22 DateTime LogTimeStamp { get; }
23 /// <summary>
24 /// Gets LogTimeStamp as string
25 /// </summary>
26 string LogTimeStampAsString { get; }
27
28 /// <summary>
29 /// Message Type as NameValuePair
30 /// </summary>
31 NameValuePair<string, granny_log_message_type> Type { get; }
32 /// <summary>
33 /// Message Origin as NameValuePair
34 /// </summary>
35 NameValuePair<string, granny_log_message_origin> Origin { get; }
36 /// <summary>
37 /// get the message
38 /// </summary>
39 string Message { get; }
40 }
41 /// <summary>
42 /// grannylog_messageType class
43 /// </summary>
44 public class grannylog_messageType : igrannylog_messageType
45 {
46 /// <summary>
47 /// default constructor
48 /// </summary>
49 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 /// <summary>
57 /// default constructor - specify: Type, Origin, Message)
58 /// </summary>
59 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
75 /// <summary>
76 /// Gets LogTimeStamp
77 /// </summary>
78 public DateTime LogTimeStamp { get { return _LogTimeStamp; } }
79 /// <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 public NameValuePair<string, granny_log_message_type> Type { get { return _Type; } }
87 /// <summary>
88 /// Message Origin as NameValuePair
89 /// </summary>
90 public NameValuePair<string, granny_log_message_origin> Origin { get { return _Origin; } }
91 /// <summary>
92 /// get the message
93 /// </summary>
94 public string Message { get { return _Message; } }
95 #endregion
96 }
97 #endregion
98
99 #region public class granny_log_eventargs : EventArgs
100 /// <summary>
101 /// granny_log_eventargs class
102 /// </summary>
103 public class granny_log_eventargs : EventArgs
104 {
105 private string message;
106 private granny_log_message_type type;
107 private granny_log_message_origin origin;
108 /// <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 {
116 this.type = type;
117 this.origin = origin;
118 this.message = message;
119 }
120
121 /// <summary>
122 /// Get Message Log Type value
123 /// </summary>
124 public granny_log_message_type Type { get { return type; } }
125
126 /// <summary>
127 /// Get Message Log Origin value
128 /// </summary>
129 public granny_log_message_origin Origin { get { return origin; } }
130
131 /// <summary>
132 /// Get Message Log Type name
133 /// </summary>
134 public string TypeString { get { return coreapi.EnumToStringSupport.GrannyGetLogMessageTypeString(type); } }
135 /// <summary>
136 /// Get Message Log Origin name
137 /// </summary>
138 public string OriginString { get { return coreapi.EnumToStringSupport.GrannyGetLogMessageOriginString(origin); } }
139 /// <summary>
140 /// Get the Message
141 /// </summary>
142 public string Message { get { return message; } }
143 }
144 #endregion
145
146 #region public class granny_log_callback_builder : igranny_log_callback_builder
147 /// <summary>
148 /// granny_log_callback_builder class
149 /// </summary>
150 public class granny_log_callback_builder : igranny_log_callback_builder
151 {
152 /// <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 [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
162 /// <summary>
163 /// OnMessageLogged delegate
164 /// </summary>
165 /// <param name="sender">object</param>
166 /// <param name="e">granny_log_eventargs</param>
167 public delegate void OnMessageLogged(object sender, granny_log_eventargs e);
168 /// <summary>
169 /// Handler for OnMessageLogged delegate, use this if you wish to handle Granny2 Log Events in your own code
170 /// </summary>
171 public event OnMessageLogged OnMessageLoggedEventHandler;
172
173 internal granny_log_callback _log_call_back;
174 internal granny_log_callback_method _callback = null;
175
176 /// <summary>
177 /// granny_log_callback
178 /// </summary>
179 [StructLayout(LayoutKind.Sequential, Pack = 4), Size(Size = 8)]
180 public struct granny_log_callback
181 {
182 /// <summary>
183 /// the fucntion to call when something is logged
184 /// </summary>
185 [MarshalAs(UnmanagedType.FunctionPtr)]
186 public granny_log_callback_method Function;
187 /// <summary>
188 /// pointer to UserData, in this implementation is always: IntPtr.Zero
189 /// </summary>
190 public IntPtr UserData;
191 }
192
193 #region Log Call Back
194 private void LogCallback(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
195 {
196 string TypeString = coreapi.EnumToStringSupport.GrannyGetLogMessageTypeString(Type);
197 string OriginString = coreapi.EnumToStringSupport.GrannyGetLogMessageOriginString(Origin);
198
199 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 this.Ignored.Add(_msg_ignored);
214 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
239 if (OnMessageLoggedEventHandler == null)
240 {
241 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 OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
250 }
251 GC.KeepAlive(this);
252
253 }
254 #endregion
255
256 /// <summary>
257 /// default constructor
258 /// </summary>
259 public granny_log_callback_builder() { _callback = new granny_log_callback_method(this.LogCallback); }
260 //public granny_log_callback_builder(coreapi.granny_log_callback_method LogCallBack) { this._callback = LogCallBack; }
261 /// <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 public granny_log_callback_builder(OnMessageLogged OnMessageLoggedEvntHandler) { OnMessageLoggedEventHandler = OnMessageLoggedEvntHandler; _callback = new granny_log_callback_method(this.LogCallback); }
266
267 #region Start Logger
268 /// <summary>
269 /// When called will initialize the logger
270 /// </summary>
271 public void StartLogging()
272 {
273 try
274 {
275 _log_call_back = new granny_log_callback();
276 _log_call_back.Function = _callback;
277 _log_call_back.UserData = IntPtr.Zero;
278 GC.KeepAlive(this);
279 coreapi.LoggingSupport.GrannySetLogCallback(ref _log_call_back);
280
281 Console.WriteLine("Granny2 API Logger: {0}", "Activated");
282 }
283 catch
284 {
285 Console.WriteLine("Granny2 API Logger: {0}", "Not Activated");
286 }
287
288 }
289 #endregion
290
291 #region Stop Logger
292 /// <summary>
293 /// When called will destroy the logger
294 /// </summary>
295 public void StopLogging()
296 {
297 try
298 {
299 _log_call_back = new granny_log_callback();
300 _log_call_back = new granny_log_callback();
301 granny_log_callback_method _granny_log_callback_method = null;
302 _log_call_back.Function = _granny_log_callback_method;
303 _log_call_back.UserData = IntPtr.Zero;
304 coreapi.LoggingSupport.GrannySetLogCallback(ref _log_call_back);
305 Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
306 }
307 catch
308 {
309 Console.WriteLine("Granny2 API Logger: {0}", "Not Deactivated");
310 }
311 }
312 #endregion
313
314
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 /// <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 public List<grannylog_messageType> Note { get { return _Note; } }
330 /// <summary>
331 /// Get the Warning Log
332 /// </summary>
333 public List<grannylog_messageType> Warning { get { return _Warning; } }
334 /// <summary>
335 /// Get the Error Log
336 /// </summary>
337 public List<grannylog_messageType> Error { get { return _Error; } }
338 /// <summary>
339 /// Get the OnePastLastMessage Log
340 /// </summary>
341 public List<grannylog_messageType> OnePastLastMessage { get { return _OnePastLastMessage; } }
342 #endregion
343 }
344 #endregion
345 }
346
347

  ViewVC Help
Powered by ViewVC 1.1.22