File: EventLogger.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (173 lines) | stat: -rw-r--r-- 4,659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// Copyright (c) 2007 Novell, Inc.
//
// Authors:
//      Rolf Bjarne Kvinge  (RKvinge@novell.com)
//

using System;
using System.Text;
using System.IO;
using NUnit.Framework;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Collections;
using System.Reflection;
using System.Reflection.Emit;

namespace MonoTests.System.Windows.Forms
{
	public class EventLogger
	{
		public class EventLog : ArrayList
		{
			public bool PrintAdds = false;
			
			new public int Add (object obj)
			{
				if (PrintAdds)
					Console.WriteLine ("{1} EventLog: {0}", obj, DateTime.Now.ToLongTimeString ());
				return base.Add (obj);
			}
		}
	
		private EventLog log;
		private object instance;

		public bool PrintAdds {
			get { return log.PrintAdds; }
			set { log.PrintAdds = value; }
		}

		// Tests if all the names in Names are in log with the order given in Names.
		public bool ContainsEventsOrdered (params string [] Names) 
		{
			if (Names.Length == 0)
				return true;
		
			int n = 0;
			for (int i = 0; i < log.Count; i++) {
				if ((string) log [i] == Names [n]) {
					n++;
					if (n == Names.Length)
						return true;
				}
			}
			
			if (n == Names.Length) {
				return true;
			} else {
				Console.WriteLine ("ContainsEventsOrdered: logged events '" + EventsJoined () + "' didn't match correct events '" + string.Join (";", Names) + "'");
				return false;
			}
		}
		
		public int CountEvents (string Name)
		{
			int count = 0;
			foreach (string str in log) {
				if (Name.Equals (str)) {
					count++;	
				}
			}
			return count;
		}
		
		public bool EventRaised (string Name) 
		{
			return log.Contains (Name);
		}
		
		public int EventsRaised {
			get {
				return log.Count;
			}
		}

		public string EventsJoined ()
		{
			return EventsJoined (";");
		}
		
		public string EventsJoined (string separator)
		{
			return string.Join (";", ToArray ());
		}
		
		public void Clear ()
		{
			log.Clear ();
		}
		
		public string [] ToArray ()
		{
			string [] result = new string [log.Count];
			log.CopyTo (result);
			return result;
		}
		
		public EventLogger (object item)
		{
			if (item == null) {
				throw new ArgumentNullException ("item");
			}

			log = new EventLog ();
			
			Type itemType = item.GetType ();
			AssemblyName name = new AssemblyName ();
			name.Name = "EventLoggerAssembly";
			AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (name, AssemblyBuilderAccess.RunAndSave);
			ModuleBuilder module = assembly.DefineDynamicModule ("EventLoggerAssembly", "EventLoggerAssembly.dll");
			
			Type ListType = log.GetType ();
			
			TypeBuilder logType = module.DefineType ("Logger");
			FieldBuilder logField = logType.DefineField ("log", ListType, FieldAttributes.Public);
			ConstructorBuilder logCtor = logType.DefineConstructor (MethodAttributes.Public, CallingConventions.HasThis, new Type [] {ListType, itemType});
			logCtor.DefineParameter (1, ParameterAttributes.None, "test");
			logCtor.DefineParameter (2, ParameterAttributes.None, "obj");
			ILGenerator logIL = logCtor.GetILGenerator ();

			logIL.Emit (OpCodes.Ldarg_0);
			logIL.Emit (OpCodes.Call, typeof (object).GetConstructor (Type.EmptyTypes));

			logIL.Emit (OpCodes.Ldarg_0);
			logIL.Emit (OpCodes.Ldarg_1);
			logIL.Emit (OpCodes.Stfld, logField);

			
			foreach (EventInfo Event in itemType.GetEvents ()) {
				ILGenerator il;

				MethodInfo invoke = Event.EventHandlerType.GetMethod ("Invoke");
				MethodBuilder method = logType.DefineMethod (Event.Name, MethodAttributes.Public, null, new Type [] { invoke.GetParameters () [0].ParameterType, invoke.GetParameters () [1].ParameterType });
				method.DefineParameter (1, ParameterAttributes.None, "test");
				method.DefineParameter (2, ParameterAttributes.None, "test2");
				il = method.GetILGenerator ();
				il.Emit (OpCodes.Ldarg_0);
				il.Emit (OpCodes.Ldfld, logField);
				il.Emit (OpCodes.Ldstr, Event.Name);
				il.Emit (OpCodes.Callvirt, ListType.GetMethod ("Add"));
				il.Emit (OpCodes.Pop);
				il.Emit (OpCodes.Ret);
				
				logIL.Emit (OpCodes.Ldarg_2);
				logIL.Emit (OpCodes.Ldarg_0);
				logIL.Emit (OpCodes.Dup);
				logIL.Emit (OpCodes.Ldvirtftn, method);
				logIL.Emit (OpCodes.Newobj, Event.EventHandlerType.GetConstructor (new Type [] {typeof(object), typeof(IntPtr)}));
				logIL.Emit (OpCodes.Call, Event.GetAddMethod ());
			}

			logIL.Emit (OpCodes.Ret);		
			Type builtLogType = logType.CreateType ();
			
			instance = builtLogType.GetConstructors () [0].Invoke (new object [] { log, item });
			TestHelper.RemoveWarning (instance);
			
			//assembly.Save ("EventLoggerAssembly.dll");
		}
	}
}