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

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    
97     public delegate void OnMessageLogged(object sender, granny_log_eventargs e);
98     public event OnMessageLogged OnMessageLoggedEventHandler;
99    
100 william 156 internal granny_log_callback _log_call_back;
101     private coreapi.granny_log_callback_method _callback = null;
102    
103 william 158 private void LogCallback(granny_log_message_type Type, granny_log_message_origin Origin, [MarshalAs(UnmanagedType.LPStr)] string Message, IntPtr UserData)
104 william 153 {
105     string TypeString = coreapi.GrannyGetLogMessageTypeString(Type);
106     string OriginString = coreapi.GrannyGetLogMessageOriginString(Origin);
107    
108 william 158 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 william 153
148 william 158 if (OnMessageLoggedEventHandler == null)
149 william 156 {
150 william 158 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 william 156 OnMessageLoggedEventHandler(this, new granny_log_eventargs(Type, Origin, Message));
159     }
160 william 153 }
161    
162 william 156 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 william 153 public granny_log_callback_builder(OnMessageLogged OnMessageLoggedEventHandler) { this.OnMessageLoggedEventHandler = OnMessageLoggedEventHandler; }
165 william 158
166     #region Start Logger
167 william 153 public void StartLogging()
168     {
169     try
170     {
171 william 156 _log_call_back = new granny_log_callback();
172 william 158 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 william 153 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 william 158 #endregion
185    
186     #region Stop Logger
187 william 153 public void StopLogging()
188     {
189 william 156 try
190 william 153 {
191 william 156 _log_call_back = new granny_log_callback();
192 william 158 _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 william 156 coreapi.GrannySetLogCallback(ref _log_call_back);
197 william 158 coreapi.GrannySetLogCallback(ref _log_call_back);
198 william 156 Console.WriteLine("Granny2 API Logger: {0}", "Deactivated");
199 william 153 }
200 william 156 catch
201     {
202     Console.WriteLine("Granny2 API Logger: {0}", "Not Deactivated");
203     }
204 william 153 }
205 william 158 #endregion
206 william 153
207 william 158
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 william 153 }
222 william 158 #endregion
223 william 153 }
224    
225 william 158

  ViewVC Help
Powered by ViewVC 1.1.22