/[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 158 - (show annotations) (download)
Thu Jul 22 09:06:12 2010 UTC (10 years, 2 months ago) by william
File size: 10554 byte(s)
Added support for viewing the Granny2 API Log through a windows form

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 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 public class granny_log_eventargs : EventArgs
60 {
61 private string message;
62 private granny_log_message_type type;
63 private granny_log_message_origin origin;
64 private string _type_string;
65 private string _origin_string;
66
67 public granny_log_eventargs(granny_log_message_type type, granny_log_message_origin origin, string message)
68 {
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 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 {
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 public granny_log_message_type Type { get { return type; } }
85 public granny_log_message_origin Origin { get { return origin; } }
86
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 #endregion
92
93 #region public class granny_log_callback_builder : igranny_log_callback_builder
94 public class granny_log_callback_builder : igranny_log_callback_builder
95 {
96
97 public delegate void OnMessageLogged(object sender, granny_log_eventargs e);
98 public event OnMessageLogged OnMessageLoggedEventHandler;
99
100 internal granny_log_callback _log_call_back;
101 private coreapi.granny_log_callback_method _callback = null;
102
103 private void LogCallback(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
104 {
105 string TypeString = coreapi.GrannyGetLogMessageTypeString(Type);
106 string OriginString = coreapi.GrannyGetLogMessageOriginString(Origin);
107
108 switch (Type)
109 {
110 case granny_log_message_type.GrannyErrorLogMessage:
111 grannylog_messageType _msg_error = new grannylog_messageType(
112 new NameValuePair<string, granny_log_message_type>(TypeString, Type),
113 new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
114 Message);
115 this.Error.Add(_msg_error);
116 break;
117 case granny_log_message_type.GrannyIgnoredLogMessage:
118 grannylog_messageType _msg_ignored = new grannylog_messageType(
119 new NameValuePair<string, granny_log_message_type>(TypeString, Type),
120 new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
121 Message);
122 this.Ingored.Add(_msg_ignored);
123 break;
124 case granny_log_message_type.GrannyNoteLogMessage:
125 grannylog_messageType _msg_note = new grannylog_messageType(
126 new NameValuePair<string, granny_log_message_type>(TypeString, Type),
127 new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
128 Message);
129 this.Note.Add(_msg_note);
130 break;
131 case granny_log_message_type.GrannyOnePastLastMessageType:
132 grannylog_messageType _msg_onepast = new grannylog_messageType(
133 new NameValuePair<string, granny_log_message_type>(TypeString, Type),
134 new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
135 Message);
136 this.OnePastLastMessage.Add(_msg_onepast);
137 break;
138 case granny_log_message_type.GrannyWarningLogMessage:
139 grannylog_messageType _msg_warning = new grannylog_messageType(
140 new NameValuePair<string, granny_log_message_type>(TypeString, Type),
141 new NameValuePair<string, granny_log_message_origin>(OriginString, Origin),
142 Message);
143 this.Warning.Add(_msg_warning);
144 break;
145 default: break;
146 }
147
148 if (OnMessageLoggedEventHandler == null)
149 {
150 Console.WriteLine();
151 Console.WriteLine("Granny2 API Logger: Logging Message -> ");
152 Console.WriteLine();
153 Console.WriteLine("\tMessage Log Type: {0}\n\tMessage Log Origin: {1}\n\tMessage: {2}", TypeString, OriginString, Message);
154 Console.WriteLine();
155 }
156 else
157 {
158 OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
159 }
160 }
161
162 public granny_log_callback_builder() { _callback = new coreapi.granny_log_callback_method(this.LogCallback); }
163 //public granny_log_callback_builder(coreapi.granny_log_callback_method LogCallBack) { this._callback = LogCallBack; }
164 public granny_log_callback_builder(OnMessageLogged OnMessageLoggedEventHandler) { this.OnMessageLoggedEventHandler = OnMessageLoggedEventHandler; }
165
166 #region Start Logger
167 public void StartLogging()
168 {
169 try
170 {
171 _log_call_back = new granny_log_callback();
172 coreapi.granny_log_callback_method _granny_log_callback_method = new coreapi.granny_log_callback_method(this.LogCallback);
173 _log_call_back.Function = _granny_log_callback_method;
174 _log_call_back.UserData = IntPtr.Zero;
175 coreapi.GrannySetLogCallback(ref _log_call_back);
176 Console.WriteLine("Granny2 API Logger: {0}", "Activated");
177 }
178 catch
179 {
180 Console.WriteLine("Granny2 API Logger: {0}", "Not Activated");
181 }
182
183 }
184 #endregion
185
186 #region Stop Logger
187 public void StopLogging()
188 {
189 try
190 {
191 _log_call_back = new granny_log_callback();
192 _log_call_back = new granny_log_callback();
193 coreapi.granny_log_callback_method _granny_log_callback_method = null;
194 _log_call_back.Function = _granny_log_callback_method;
195 _log_call_back.UserData = IntPtr.Zero;
196 coreapi.GrannySetLogCallback(ref _log_call_back);
197 coreapi.GrannySetLogCallback(ref _log_call_back);
198 Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
199 }
200 catch
201 {
202 Console.WriteLine("Granny2 API Logger: {0}", "Not Deactivated");
203 }
204 }
205 #endregion
206
207
208 #region igranny_log_callback_builder Members
209 private List<grannylog_messageType> _Ingored = new List<grannylog_messageType>();
210 private List<grannylog_messageType> _Note = new List<grannylog_messageType>();
211 private List<grannylog_messageType> _Warning = new List<grannylog_messageType>();
212 private List<grannylog_messageType> _Error = new List<grannylog_messageType>();
213 private List<grannylog_messageType> _OnePastLastMessage = new List<grannylog_messageType>();
214
215 public List<grannylog_messageType> Ingored { get { return _Ingored; } }
216 public List<grannylog_messageType> Note { get { return _Note; } }
217 public List<grannylog_messageType> Warning { get { return _Warning; } }
218 public List<grannylog_messageType> Error { get { return _Error; } }
219 public List<grannylog_messageType> OnePastLastMessage { get { return _OnePastLastMessage; } }
220 #endregion
221 }
222 #endregion
223 }
224
225

  ViewVC Help
Powered by ViewVC 1.1.22