/[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 166 - (show annotations) (download)
Thu Jul 22 14:24:07 2010 UTC (10 years, 3 months ago) by william
File size: 11047 byte(s)
bulk commit

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 [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
99 public delegate void OnMessageLogged(object sender, granny_log_eventargs e);
100 public event OnMessageLogged OnMessageLoggedEventHandler;
101
102 internal granny_log_callback _log_call_back;
103 internal granny_log_callback_method _callback = null;
104
105 [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 private void LogCallback(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
115 {
116 string TypeString = coreapi.GrannyGetLogMessageTypeString(Type);
117 string OriginString = coreapi.GrannyGetLogMessageOriginString(Origin);
118
119 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
159 if (OnMessageLoggedEventHandler == null)
160 {
161 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 OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
170 }
171 GC.KeepAlive(this);
172
173 }
174 #endregion
175
176 public granny_log_callback_builder() { _callback = new granny_log_callback_method(this.LogCallback); }
177 //public granny_log_callback_builder(coreapi.granny_log_callback_method LogCallBack) { this._callback = LogCallBack; }
178 public granny_log_callback_builder(OnMessageLogged OnMessageLoggedEvntHandler) { OnMessageLoggedEventHandler = OnMessageLoggedEvntHandler; _callback = new granny_log_callback_method(this.LogCallback); }
179
180 #region Start Logger
181 public void StartLogging()
182 {
183 try
184 {
185 _log_call_back = new granny_log_callback();
186 _log_call_back.Function = _callback;
187 _log_call_back.UserData = IntPtr.Zero;
188 GC.KeepAlive(this);
189 coreapi.GrannySetLogCallback(ref _log_call_back);
190
191 Console.WriteLine("Granny2 API Logger: {0}", "Activated");
192 }
193 catch
194 {
195 Console.WriteLine("Granny2 API Logger: {0}", "Not Activated");
196 }
197
198 }
199 #endregion
200
201 #region Stop Logger
202 public void StopLogging()
203 {
204 try
205 {
206 _log_call_back = new granny_log_callback();
207 _log_call_back = new granny_log_callback();
208 granny_log_callback_method _granny_log_callback_method = null;
209 _log_call_back.Function = _granny_log_callback_method;
210 _log_call_back.UserData = IntPtr.Zero;
211 coreapi.GrannySetLogCallback(ref _log_call_back);
212 Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
213 }
214 catch
215 {
216 Console.WriteLine("Granny2 API Logger: {0}", "Not Deactivated");
217 }
218 }
219 #endregion
220
221
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 }
236 #endregion
237 }
238
239

  ViewVC Help
Powered by ViewVC 1.1.22