forked from johanhelsing/wayland-log-reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwayland-debug-tools.js
179 lines (153 loc) · 5.43 KB
/
wayland-debug-tools.js
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
.pragma library
.import "./lodash.js" as Lodash
const re = {
// logLine: /^\[\d{7}\.\d{3}\] (.*)$/,
logLine: /^\[ {0,7}\d{0,7}[\.,]\d{3}\] (( -> )?(\w+@\d+)\.(\w+)\((.*)\))$/,
sentMessage: /^ -> (.*)$/,
message: /^(\w+@\d+)\.(\w+)\((.*)\)$/,
arguments: /^$/,
newId: /^new id ((\w+|\[\unknown\])@\d+)$/,
object: /^(\w+|\[unknown\])@(\d+)$/
};
function parseNewId(arg) {
const parts = arg.match(re.newId);
if (!parts)
return;
const object = parseObject(parts[1])
return { type: "new", object: object, rawText: arg };
}
function parseInteger(arg) {
const value = parseInt(arg);
if (isNaN(value))
return;
return { type: "integer", value: value, rawText: arg };
}
function parseString(arg) {
try {
const value = JSON.parse(arg);
if (typeof value === "string")
return { type: "string", value: value, rawText: arg };
} catch (e) {}
return;
}
function splitArgs(text) {
var startPos = 0;
var inString = false;
const args = [];
for (var i = 0;; ++i) {
const ch = text[i];
if (!ch) {
if (i > startPos)
args.push(text.slice(startPos, i));
break;
}
if (!inString && ch === ",") {
args.push(text.slice(startPos, i));
startPos = i + 2;
++i; //skip space inbetween
}
if (i === startPos)
inString = ch === "\"";
else if (inString && ch === "\"" && text[i-1] !== "\\")
inString = false;
}
return args;
}
function parseArgs(args) {
return splitArgs(args).map(function(arg) {
return parseNewId(arg) ||
parseObject(arg) ||
parseInteger(arg) ||
parseString(arg) ||
{ type: "unknown", rawText: arg };
});
}
function parseObject(object) {
const parts = object.match(re.object);
if (!parts)
return;
return { type: "object", interfaceName: parts[1], id: parseInt(parts[2]), rawText: object };
}
function parseMessage(message) {
const matches = message.match(re.message);
const object = parseObject(matches[1]);
const args = parseArgs(matches[3]);
return {object: object, fn: matches[2], args: args, rawText: message};
}
function parseLine(line) {
const lineParts = line.match(re.logLine);
if (!lineParts)
return {type: "comment", parts: {comment: line}, rawText: line};
const message = lineParts[1];
const sentMessage = message.match(re.sentMessage);
const parsedMessage = parseMessage(sentMessage ? sentMessage[1] : message);
const type = sentMessage ? "request" : "event"; // TODO: support server logs
return { type: type, parts: parsedMessage, rawText: line };
}
var nextId = 1;
function getUniqueId() {
return nextId++;
}
function updateUniqueId(object, liveObjects) {
const objectInfo = liveObjects[object.id];
if (!objectInfo) {
// console.warn("couldn't find live id", object.id);
return;
}
object.uniqueId = objectInfo.uniqueId;
}
function updateLiveObjects(line, state) {
if (line.type !== "event" && line.type !== "request")
return;
line.parts.args.forEach(function(arg) {
if (arg.type === "new") {
const id = arg.object.id;
if (state.liveObjectsById[id])
console.warn(id, "is already in db, something is wrong, continuing anyway");
const uniqueId = getUniqueId();
const object = { interfaceName: arg.object.interfaceName, id: arg.object.id, uniqueId: uniqueId };
state.objects[uniqueId] = object;
state.liveObjectsById[id] = object;
}
});
}
//TODO: currently state will be modified... could perhaps copy it first to have a cleaner API
function processLogLine(state, line) {
const parsedLine = parseLine(line);
state.parsedLines.push(parsedLine);
//easier access
const type = parsedLine.type;
const parts = parsedLine.parts;
const object = parts && parts.object;
const fn = parts && parts.fn;
const args = parts && parts.args;
if (type === "event" && object.interfaceName === "wl_registry" && fn === "global")
state.globals[args[0].value] = { number: args[0].value, interfaceName: args[1].value, version: args[2].value};
if (type === "request" && object.interfaceName === "wl_registry" && fn === "bind") {
const global = state.globals[args[0].value];
parsedLine.global = global;
args[3].object.interfaceName = global.interfaceName;
}
if (type === "event" || type === "request") {
updateLiveObjects(parsedLine, state);
updateUniqueId(object, state.liveObjectsById);
args.forEach(function(arg) {
if (arg.type === "object")
updateUniqueId(arg, state.liveObjectsById);
else if (arg.type === "new")
updateUniqueId(arg.object, state.liveObjectsById);
});
}
return state;
}
function createInitialState() {
const state = { parsedLines: [], objects: {}, liveObjectsById: {}, globals: {} };
const displayObject = { interfaceName: "wl_display", id: 1, uniqueId: getUniqueId() } ;
state.objects[displayObject.uniqueId] = displayObject;
state.liveObjectsById[displayObject.id] = displayObject;
return state;
}
function parseLog(log) {
const lines = log.split('\n');
return Lodash._.reduce(lines, processLogLine, createInitialState());
}