summaryrefslogtreecommitdiff
path: root/src/trace_viewer/index.ts
blob: f9b5041b96cb2cae83f51a47123de9a574f17e13 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import {remote} from "electron"
import fs = require("fs")
const dialog = remote.dialog
const app = remote.app

let topCallDiv = document.createElement("div")

const max_arg_length = 5000

abstract class Event {
    caller: Call

    protected div: HTMLDivElement | null = null

    public hide(): void {
        if (this.div != null) {
            this.div.remove()
            this.div = null
        }        
    }

    protected abstract showText(text: HTMLParagraphElement): void

    public show(): HTMLDivElement {
        let callerDiv: HTMLDivElement = (this.caller != null) ? this.caller.show() : topCallDiv
        
        if (this.div != null) {
            return this.div
        } else {
            this.div = document.createElement("div")
            this.div.className = "tree"
            callerDiv.appendChild(this.div)
            let text = document.createElement("p")
            this.showText(text)
            this.div.appendChild(text)
            return this.div
        }          
    }
}

class Load extends Event {
    loc: string
    val: string

    constructor(loc: string, val: string) {
        super()
        this.loc = loc
        this.val = val
    }

    protected showText(text: HTMLParagraphElement): void {
        text.className = "load"
        text.insertAdjacentText('beforeend', this.loc + " " + this.val)        
    }
}

class Read extends Event {
    reg: string
    value: string

    constructor(reg: string, value: string) {
        super()
        this.reg = reg
        this.value = value
    }

    public showText(text: HTMLParagraphElement): void {
        text.className = "read"
        text.insertAdjacentText('beforeend', this.reg + " " + this.value)
    }
}

class Write extends Event {
    reg: string
    value: string

    constructor(reg: string, value: string) {
        super()
        this.reg = reg
        this.value = value
    }

    public showText(text: HTMLParagraphElement): void {
        text.className = "write"
        text.insertAdjacentText('beforeend', this.reg + " " + this.value)
    }
}

class Call {
    fn: string
    arg: string
    ret: string
    callees: (Call | Event)[] = []
    caller: Call

    private div: HTMLDivElement | null = null        

    private toggle: boolean = false
    private toggleImg: HTMLImageElement | null = null

    constructor(fn: string, arg: string, ret: string) {
        this.fn = fn
        this.arg = arg
        this.ret = ret
    }

    public expand() {
        if (this.caller != undefined) {
            this.caller.expand()
        }
        this.showChildren()
    }

    public iter(f: (call: Call) => void): void {
        f(this)
        this.callees.forEach((callee) => {
            if (callee instanceof Call) { callee.iter(f) }
        })

    }

    public show(): HTMLDivElement {
        let callerDiv: HTMLDivElement = (this.caller != null) ? this.caller.show() : topCallDiv

        if (this.div != null) {
            return this.div
        } else {
            this.div = document.createElement("div")
            this.div.className = "tree"
            callerDiv.appendChild(this.div)
            let text = document.createElement("p")
            text.className = "call"
            if (this.callees.length > 0) {
                this.toggleImg = document.createElement("img")
                this.toggleImg.src = "List-add.svg"
                this.toggleImg.addEventListener('click', () => {
                    if (this.toggle) {
                    this.hideChildren()
                    } else {
                        this.showChildren()
                    }
                })
                text.appendChild(this.toggleImg)
            }
            this.toggle = false
            let display_arg = this.arg
            if (this.arg.length > max_arg_length) {
                display_arg = this.arg.slice(0, max_arg_length)
            }
            let display_ret = this.ret
            if (this.ret.length > max_arg_length) {
                display_ret = this.ret.slice(0, max_arg_length)
            }

            text.insertAdjacentText('beforeend', this.fn + " " + display_arg + " -> " + display_ret)
            this.div.appendChild(text)
            return this.div
        }
    }

    public hide(): void {
        if (this.toggle == true) {
            this.hideChildren()
        }

        if (this.div != null) {
            this.div.remove()
            this.div = null
        }
        if (this.toggleImg != null) {
            this.toggleImg.remove()
            this.toggleImg = null
        }
    }

    public hideChildren(): void {
        this.callees.forEach(call => {
            call.hide()
        })

        if (this.toggleImg != null) {
            this.toggleImg.src = "List-add.svg"
            this.toggle = false
        } else {
            alert("this.toggleImg was null!")
        }
    }

    public showChildren(): void {
        this.callees.forEach(call => {
            call.show()
        });

        if (this.toggleImg != null) {
            this.toggleImg.src = "List-remove.svg"
            this.toggle = true
        } else {
            alert("this.toggleImg was null!")
        }
    }

    public appendChild(child: Call | Write | Read | Load): void {
        child.caller = this

        this.callees.push(child)
    }
}

document.addEventListener('DOMContentLoaded', () => {
    let rootCall = new Call("ROOT", "", "")
    topCallDiv.id = "root"
    document.getElementById("container")!.appendChild(topCallDiv)

    let commandInput = document.getElementById("command") as HTMLInputElement

    commandInput.addEventListener("keydown", (event) => {
        if(event.keyCode == 13) {
            let cmd = commandInput.value.split(" ")
            commandInput.value = ""

            if (cmd[0] == "function") {
                rootCall.iter((call) => {
                    if (call.fn == cmd[1]) { call.caller.expand() }
                })
            }
        }
    })

    let files = dialog.showOpenDialog(remote.getCurrentWindow(), {title: "Select log file", defaultPath: app.getAppPath()})

    if (files == [] || files == undefined) {
        dialog.showErrorBox("Error", "No file selected")
        app.exit(1)
    }

    fs.readFile(files[0], 'utf-8', (err, data) => {
        if (err) {
            dialog.showErrorBox("Error", "An error occurred when reading the log: " + err.message)
            app.exit(1)
        }

        let lines = data.split("\n")
        // let indents = lines.map(line => line.search(/[^\s]/) / 2)
        lines = lines.map(line => line.trim())

        let stack : Call[] = [rootCall]

        lines.forEach(line => {
            if (line.match(/^Call:/)) {
                let words = line.slice(6).split(" ")
                let call = new Call(words[0], words.slice(1).join(" "), "")
                if (stack.length > 0) {
                    stack[stack.length - 1].appendChild(call)
                }
                stack.push(call)
            } else if (line.match(/^Return:/)) {
                let call = stack.pop()
                if (call == undefined) {
                    alert("Unbalanced return")
                    app.exit(1)
                } else {
                    call.ret = line.slice(8)
                }
            } else if (line.match(/^Write:/)) {
                let words = line.slice(7).split(" ")
                let write = new Write(words[0], words.slice(1).join(" "))
                if (stack.length > 0) {
                    stack[stack.length - 1].appendChild(write)
                }
            } else if (line.match(/^Read:/)) {
                let words = line.slice(6).split(" ")
                let read = new Read(words[0], words.slice(1).join(" "))
                if (stack.length > 0) {
                    stack[stack.length - 1].appendChild(read)
                }
            } else if (line.match(/^Load:/)) {
                let words = line.slice(6).split(" ")
                let load = new Load(words[0], words[1])
                if (stack.length > 0) {
                    stack[stack.length - 1].appendChild(load)
                }
            }
        })

        rootCall.show()
    })
})