aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/debug/DebugLogger.cs
blob: 161d742c8a75226b6a9b9d37ee1ff369975039b9 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// CrowDebugger.cs
//
// Author:
//       Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Cairo;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

#if DEBUG_LOG
namespace Crow
{
	/*public class LayoutingEvent : DbgEvent {
		public List<LayoutingQueueItem> lqis = new List<LayoutingQueueItem>();

	}*/
	[Flags]
	public enum DbgEvtType {
		////9 nth bit set for iface event
		IFace							= 0x0100,
		IFaceStartLayouting				= 0x0101,
		IFaceEndLayouting				= 0x0102,
		IFaceStartClipping				= 0x0103,
		IFaceEndClipping				= 0x0104,
		IFaceStartDrawing				= 0x0105,
		IFaceEndDrawing					= 0x0106,
		IFaceUpdate						= 0x0107,
		//10 nth bit set for graphic obj
		GraphicObject					= 0x0200,
		GOClassCreation					= 0x0201,
		GOInitialization					= 0x0202,
		GOClippingRegistration			= 0x0203,
		GORegisterClip					= 0x0204,
		GORegisterForGraphicUpdate		= 0x0205,
		GOEnqueueForRepaint				= 0x0206,
		GOLayouting						= 0x0400,
		GORegisterLayouting				= 0x0607,
		GOProcessLayoutingWithNoParent	= 0x0608,
		GOProcessLayouting				= 0x0609,
		GODraw							= 0x020a,
		GORecreateCache					= 0x020b,
		GOUpdateCacheAndPaintOnCTX		= 0x020c,
		GOPaint							= 0x020d,
		GONewDataSource					= 0x020e,
	}
	public class DbgData {
		public int obj;
		public LayoutingType layout;
		public LayoutingQueueItem.Result result;

		public DbgData (int _obj) {
			obj = _obj;
		}
	}
	public class DbgEvent {
		public long begin, end;
		public DbgEvtType type;
		public object data = null;

		public DbgEvent() {}
			
		public DbgEvent(long timeStamp, DbgEvtType evt, object _data = null) {			
			data = _data;
			type = evt;
			begin = timeStamp;
		}

		public override string ToString ()
		{
			GraphicObject go = data as GraphicObject;
			if (go != null)
				return string.Format ("{0};{1};{2};{3}", begin, end, type, GraphicObject.GraphicObjects.IndexOf(go).ToString());
			if (!(data is LayoutingQueueItem))
				return string.Format ("{0};{1};{2}", begin, end, type);
			LayoutingQueueItem lqi = (LayoutingQueueItem)data;
			if (type == DbgEvtType.GOProcessLayouting)
				return string.Format ("{0};{1};{2};{3};{4};{5}", begin, end, type, GraphicObject.GraphicObjects.IndexOf(lqi.graphicObject).ToString(), lqi.LayoutType.ToString(), lqi.result.ToString());			
			return string.Format ("{0};{1};{2};{3};{4}", begin, end, type, GraphicObject.GraphicObjects.IndexOf(lqi.graphicObject).ToString(), lqi.LayoutType.ToString());
			
		}

		public static DbgEvent Parse (string str) {
			if (str == null)
				return null;
			string[] tmp = str.Trim().Split(';');


			DbgEvent evt = new DbgEvent ();
			evt.begin = long.Parse (tmp [0]);
			evt.end = long.Parse (tmp [1]);
			evt.type = (DbgEvtType)Enum.Parse (typeof(DbgEvtType), tmp [2]);

			if (evt.type.HasFlag (DbgEvtType.GraphicObject)) {
				DbgData data = new DbgData (int.Parse (tmp [3]));
				if (evt.type.HasFlag (DbgEvtType.GOLayouting)) {
					data.layout = (LayoutingType)Enum.Parse (typeof(LayoutingType), tmp [4]);
					if (evt.type == DbgEvtType.GOProcessLayouting)
						data.result = (LayoutingQueueItem.Result)Enum.Parse (typeof(LayoutingQueueItem.Result), tmp [5]);										
				}
				evt.data = data;
			}
			return evt;
		}
	}
	public class DbgGo {
		public int index;
		public string name;
		public int yIndex;
		public int xLevel;

		public static DbgGo Parse (string str) {
			DbgGo g = new DbgGo ();
			if (str == null)
				return null;
			string[] tmp = str.Trim().Split(';');
			g.index = int.Parse (tmp [0]);
			g.name = tmp [1];
			g.yIndex = int.Parse (tmp [2]);
			g.xLevel = int.Parse (tmp [3]);
			return g;
		}

	}
	public static class DebugLog
	{
		static Surface surf;
		static Context ctx;

		static Crow.Rectangle bounds = new Crow.Rectangle(0,0,8182,4096);
		static double penX = 1.0;
		static double ySpacing = 10.0;
		static double xPenStart = 250.0;
		static double xResolution = 0.001; //per tick
		public static Stopwatch chrono = Stopwatch.StartNew();

		public static List<DbgEvent> events = new List<DbgEvent>();
		public static DbgEvent currentEvent = null;

		public static DbgEvent AddEvent (DbgEvtType evtType, object data = null) {
			DbgEvent evt = new DbgEvent(chrono.ElapsedTicks, evtType, data);
			events.Add (evt);
			return evt;
		}

		static int y, level;

		static void parseTree (GraphicObject go) {
			if (go == null)
				return;
				
			go.yIndex = y++;
			go.xLevel = level++;

			Group gr = go as Group;
			if (gr != null) {
				foreach (GraphicObject g in gr.Children) {
					parseTree (g);
				}
			} else {
				PrivateContainer pc = go as PrivateContainer;
				if (pc != null)
					parseTree (pc.getTemplateRoot);				
			}
			level--;		
		}

		public static void save(Interface iface) {
			y = 1;
			level = 0;

			foreach (GraphicObject go in iface.GraphicTree) 
				parseTree (go);			

			using (StreamWriter s = new StreamWriter("debug.log")){
				s.WriteLine ("[GraphicObjects]");
				for (int i=0; i<GraphicObject.GraphicObjects.Count; i++) {
					GraphicObject g = GraphicObject.GraphicObjects [i];
					s.WriteLine ("{0};{1};{2};{3}", i, g.GetType().Name, g.yIndex, g.xLevel);	
				}
				s.WriteLine ("[Events]");

				foreach (DbgEvent e in events)
					s.WriteLine (e.ToString ());
			}
			/*
			List<GraphicObject> drawn = new List<GraphicObject> ();


			ctx.MoveTo (0.5 + xPenStart, 0.0);
			ctx.LineTo (0.5 + xPenStart, bounds.Height);
			ctx.LineWidth = 1.0;
			ctx.SetSourceColor (Crow.Color.Black);
			ctx.Stroke ();


			List<LayoutingQueueItem> lqis = new List<LayoutingQueueItem>();

			foreach (LayoutingQueueItem lqi in lqis) {
				GraphicObject go = lqi.graphicObject;
				if (go.yIndex == 0)
					continue;
				double penX = 0.0, penY = go.yIndex * ySpacing;

				if (!drawn.Contains (go)) {
					penX = go.xLevel * 5.0;

					ctx.MoveTo (penX, penY);
					ctx.SetSourceColor (Crow.Color.Black);

					ctx.ShowText (go.GetType ().ToString ());
					drawn.Add (go);
				}

				//penX = xPenStart + xResolution * lqi.begin;
				//ctx.Rectangle (penX, penY, Math.Max(1.0, (lqi.end - lqi.begin) * xResolution), ySpacing);


				ctx.Fill ();
			}
			surf.WriteToPng ("debug.png");
			*/
		}
	}
}
#endif