html.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2017 Marc-Antoine Ruel. All rights reserved.
  2. // Use of this source code is governed under the Apache License, Version 2.0
  3. // that can be found in the LICENSE file.
  4. package internal
  5. import (
  6. "html/template"
  7. "os"
  8. "time"
  9. "notabug.org/themusicgod1/panicparse/stack"
  10. )
  11. func writeToHTML(html string, buckets []*stack.Bucket, needsEnv bool) error {
  12. m := template.FuncMap{
  13. "funcClass": funcClass,
  14. "notoColorEmoji1F4A3": notoColorEmoji1F4A3,
  15. }
  16. if len(buckets) > 1 {
  17. m["routineClass"] = routineClass
  18. } else {
  19. m["routineClass"] = func(bucket *stack.Bucket) template.HTML { return "Routine" }
  20. }
  21. t, err := template.New("htmlTpl").Funcs(m).Parse(htmlTpl)
  22. if err != nil {
  23. return err
  24. }
  25. data := struct {
  26. Buckets []*stack.Bucket
  27. Now time.Time
  28. NeedsEnv bool
  29. }{buckets, time.Now().Truncate(time.Second), needsEnv}
  30. f, err := os.Create(html)
  31. if err != nil {
  32. return err
  33. }
  34. err1 := t.Execute(f, data)
  35. err2 := f.Close()
  36. if err1 != nil {
  37. return err1
  38. }
  39. return err2
  40. }
  41. func funcClass(line *stack.Call) template.HTML {
  42. if line.IsStdlib {
  43. if line.Func.IsExported() {
  44. return "FuncStdLibExported"
  45. }
  46. return "FuncStdLib"
  47. } else if line.IsPkgMain() {
  48. return "FuncMain"
  49. } else if line.Func.IsExported() {
  50. return "FuncOtherExported"
  51. }
  52. return "FuncOther"
  53. }
  54. func routineClass(bucket *stack.Bucket) template.HTML {
  55. if bucket.First {
  56. return "RoutineFirst"
  57. }
  58. return "Routine"
  59. }
  60. const htmlTpl = `<!DOCTYPE html>
  61. {{- define "RenderCall" -}}
  62. {{.SrcLine}} <span class="{{funcClass .}}">{{.Func.Name}}</span>({{.Args}})
  63. {{- end -}}
  64. <meta charset="UTF-8">
  65. <meta name="viewport" content="width=device-width, initial-scale=1">
  66. <title>PanicParse</title>
  67. <link rel="shortcut icon" type="image/png" href="data:image/png;base64,{{notoColorEmoji1F4A3}}"/>
  68. <style>
  69. body {
  70. background: black;
  71. color: lightgray;
  72. }
  73. body, pre {
  74. font-family: Menlo, monospace;
  75. font-weight: bold;
  76. }
  77. .FuncStdLibExported {
  78. color: #7CFC00;
  79. }
  80. .FuncStdLib {
  81. color: #008000;
  82. }
  83. .FuncMain {
  84. color: #C0C000;
  85. }
  86. .FuncOtherExported {
  87. color: #FF0000;
  88. }
  89. .FuncOther {
  90. color: #A00000;
  91. }
  92. .RoutineFirst {
  93. }
  94. .Routine {
  95. }
  96. </style>
  97. <div id="legend">Generated on {{.Now.String}}.
  98. {{if .NeedsEnv}}
  99. <br>To see all goroutines, visit <a
  100. href=https://notabug.org/themusicgod1/panicparse#gotraceback>notabug.org/themusicgod1/panicparse</a>.<br>
  101. {{end}}
  102. </div>
  103. <div id="content">
  104. {{range .Buckets}}
  105. <h1>{{if .First}}Panicking {{end}}Routine</h1>
  106. <span class="{{routineClass .}}">{{len .IDs}}: <span class="state">{{.State}}</span>
  107. {{if .SleepMax -}}
  108. {{- if ne .SleepMin .SleepMax}} <span class="sleep">[{{.SleepMin}}~{{.SleepMax}} minutes]</span>
  109. {{- else}} <span class="sleep">[{{.SleepMax}} minutes]</span>
  110. {{- end -}}
  111. {{- end}}
  112. {{if .Locked}} <span class="locked">[locked]</span>
  113. {{- end -}}
  114. {{- if .CreatedBy.SrcPath}} <span class="created">[Created by {{template "RenderCall" .CreatedBy}}]</span>
  115. {{- end -}}
  116. <h2>Stack</h2>
  117. {{range .Signature.Stack.Calls}}
  118. - {{template "RenderCall" .}}<br>
  119. {{- end}}
  120. {{if .Stack.Elided}}(...)<br>{{end}}
  121. {{end}}
  122. </div>
  123. `
  124. // notoColorEmoji1F4A3 is the bomb emoji U+1F4A3 in Noto Emoji as a PNG.
  125. //
  126. // Source: https://www.google.com/get/noto/help/emoji/smileys-people.html
  127. // License: http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL
  128. //
  129. // Created with:
  130. // python -c "import base64;a=base64.b64encode(open('emoji_u1f4a3.png','rb').read()); print '\n'.join(a[i:i+70] for i in range(0,len(a),70))"
  131. func notoColorEmoji1F4A3() template.HTML {
  132. return "" +
  133. "iVBORw0KGgoAAAANSUhEUgAAAIgAAACACAMAAADnN9ENAAAA5FBMVEVMaXFvTjQhISEhIS" +
  134. "EhISEhISEhISEhISEhISEhISEhISEhISHRbBTRbBTRbBQhISHRbBTRbBTRbBTRbBQ6OjrR" +
  135. "bBTZcBTsehbzfhf1fxfidRVOTk75oiL7uCr90zL3kB3/6zv2hhlHMR5OTk5KSkpKSkpRUV" +
  136. "FKSkpKSkpMTExBQUE2NjYpKSlOTk5EREQ8PDw4ODgtLS1RUVFQUFAmJiYkJCQ0NDRLS0tT" +
  137. "U1MoKCgyMjJHR0dTU1NdXV0rKytXV1dVVVUvLy9eXl5aWlpcXFxcXFxeXl5fX186OjphYW" +
  138. "E/Pz9KSkrdB5CTAAAATHRSTlMAEUZggJjf/6/vcL86e1nP2//vmSC7//////9E////////" +
  139. "/2WPpYHp////////////////////z/////8w6P////+p/8f///////+/QBb3BQAACWJJRE" +
  140. "FUeAHslgWC6yAURUOk1N3d3d29+9/SzzxCStvQEfp9zgZycu8DnvTNN78YJCuqqsry77WQ" +
  141. "NRum2BX0u7JQiYWJw/l7NBz4AderQkFut8frdn+kFBu2wodeYeHxBwjBkFd6StiFOYiboF" +
  142. "CAxf9MRXHc9KGpqurDBpqghzcYuCOCeMp21oKelTBVCQt5kDiisXhCJ5aMQkFu6+lg4tDY" +
  143. "r2rikRCPKFgQYlGeiYpN7OHbpMj8OgQ8PAGdWIIlnnwbFKYdtgDAJj+MDgZSX/Zwsx4mby" +
  144. "YR6RbntRZVegQDX7/tI1YexMTNObQ+y992EUWRQJIJQjqTzWRyRjtRiMTqKtWQJCTCD4TM" +
  145. "aS6bBzLGxLKRKNer1MELX0wEmYHk8pSMGUmIlMI+LC7uTeEQmhEvnZBC/kqGTolfkmSn72" +
  146. "NPbFhoWOHswmczeYYC7YaV4MfBHl+BEYmCSJYVSUM3ukgRM5C7g4edqAqLMBq0m1sRh8oe" +
  147. "Fl4zzp8swtFApXKlmkIkECD8+mpYEZ8iWZGq1YFKKazg582IDiu8bk7Ob5YaOnVCs9UWu+" +
  148. "C99D7LWR5fVeY/YpVOp9Po9rp1g25/AIGIXmhp0yMLgSTIhcYDVYaj0ag1Hk/a0yZ1qVVK" +
  149. "6KsmfnMVSbMepBkv32M2nw87rfZioatMxsveirqUBLoxHr1CJpvNZmBQSSB+icd6M9dFpt" +
  150. "tt21CZ4EHfKKny9Ug4awA/kNRmt993loPBAFSICcbtFpRUeuViFIPFiENpp9NYHg6HexW8" +
  151. "1Suaiaysscc8gsg6jdTxpFNf6oALUaEmBz2SsMBKEkjeL88Bt8VFWj1fCKvWdDolKmYoYD" +
  152. "L5ejcSApNoMsZqBH+0byfbiStNEICbFZt/uIN3WtpASWIUwgOiZWgMyPL7v8+tCuIkBdlw" +
  153. "W4WWHS/AdyKzRCEfXy7Iw+PHntlti+k0TZ1FKCJJgteV0wHGhr/1Lvp4/HGQvGdJVVVTZy" +
  154. "HFl6TGDEIM3FiUIvnrv53zkXyH4PNzm5mknrhUNo7CUjAeSIbGmNW3Vih/gHGKY1jE53uc" +
  155. "1BJYSOF4KCmM6YcqaPmvy/86l88MKPbzcXIKLKSwFJFUPMDtpvPDKT63cZKMJSCRglJE4k" +
  156. "7x0hjTaZHAOpzjYBIKCpsThpQLSc4D3GaOdWQ0+CEFEo7HSkpIxjzAraXz4RxbURhJQQtK" +
  157. "gYSdYE2mPMBtZYWxjMggIRYLKSiFks0Gw9nExjy16XBnxYBxNHghRSTcE65JEXM4rTl2qI" +
  158. "OKkRdnIcWXcDgzXktam8s76zBQzOcZ4q6IsIBCSVVhYTmckpJ2HWBA8XoMMEri1oQnJ89L" +
  159. "x++3cF6U46hYI8CQ4ks4HBzhYdHK0+TDc5ABxDsCCygi4ZpIJYvF0EIGqzsdffvtskschA" +
  160. "4wLGHrcrRoCYfDSnBVe+nc5Yis4zAWRwYHFQwpFxKBuEq6Uyt5untBCs8hjB1DCiVnlfDg" +
  161. "5PkCdzUT3TmYDINxezqH46jY7+3FxF0Vd75EVcLZdPPCmEH4cFb202RBfIdT2K4ONo5CyQ" +
  162. "iSE+T5NJvu8q4z/LE/fBYWwsHY/Xgn45NxGEr8SvyDc4R06zt+W0S7wyGrk1MhdJAhkr2T" +
  163. "rEXy09ng/toLL2SfcENkMHRoiYVkrERBnKQKriSynzmqORlAlMObjgyHs+G5kSXp5sGV/N" +
  164. "jZQmQyKMQOxnNIpBI1m40sCSoJOjgPux0LKW4XQgkOjpqNBwm9wPYtJFGToeNaJaPjbPS2" +
  165. "utRh98bvu/1rLZAMEFWISAjxl0RBNkE//Fb2U4sTBI5bkJ2/JBqCFCHfOH27mBMHKXzI4R" +
  166. "pk/1PI8zmkCpnNx3aXaYh1NICkF5BZwKOkYz+1aBUS+OYmsp9aa8i+wWjUjuDc9BqvyPa9" +
  167. "AkSW9b5Tg0yNeWm8ItusmkwuniP6wUrHBSTREFmSpk+R7dZMq4l6sh6uP1kdBLc09WQVyL" +
  168. "D5Rc1eCGtAkiPk5pLIZDKuiH7EM40hD4R426oqufmlNwHEOs4hSdN7WmQhqYOoLxslEYd8" +
  169. "1ejTK5C6MWTtIN6SuHPD4fgSOvxCrnznBZxfQtbPrOTi7kyJdkghNyCpMV+NIP31+4gQVs" +
  170. "Itubg9yzVerqxyePWKhEHWDiLnhpVQAgoCBh08u7qQuyCv69FSKrmQgLLDm3jHEIdXiJ5M" +
  171. "IOTxdZ0B4h0cSvzfnFswzh1SiJ5MACSyn7h89iuhJIMEFCoc49KhJxN2aghxlSiJvJdg1n" +
  172. "DMnUMV4k0m9Dmysp/3zErOJKDAwrxahnKcCuFkAp6sjP20qauEwzmTgIJAAYbvuCjEhzT/" +
  173. "0htkr/VmyX0VCSnOwoABR0EHBnOlkLw55Ct7HTvImUQoFsN4L1rVS0VVSMh9pD/P4pmTYD" +
  174. "giUW98Y4/BPvRgJGnzG1pk698oiX4HblwK5ZC3rHSE31kf7FJOZxtfQgotEmGIQw1GEvLr" +
  175. "dzCaJ6WthJKKElAQGs4Y1x0Bv2uYp9E8Ls8kpIhFEI5Bx5SO44nxIcG/9CL7GF2WM5FgPK" +
  176. "DAwlBBBs4LHbqQgN++yCAeJcMzCSiwiCahAgyM5YZjFvZn4Kd4FJdOgo0VCizEwAAG69AO" +
  177. "P5Ow9yOrOB6lw2FZniSWIhbJBAphYE+VI+yNEfMVxyZ/o8SjwMKIAgzUoR1MFfpH4EcTx8" +
  178. "9HiVBgAeaUqTDoGMKhCgl/0TowcZFbCRcFFFokQJwx4FCM0PesrMTE6QISjwKLRBSWIWOh" +
  179. "o4VCmBdjTL7IheIsxEioEMYVB9/FByYyxtQLSkiBBaFAFML4mWN235+OesaYzcKnwEIMEV" +
  180. "CAcd2x4N9rQtMZGFPkXVJoIQYBAgrFUIOJghkcTtJ1ElBgAcZLSYVmSFK1qUHDqbqk0AIM" +
  181. "AwQUYNChF4SDCU/HnZxNlxRY3qBhiPAUOsOC33Z3ZTWgxLOAg8AAhWJI2vhLONeEElqoEY" +
  182. "JSKAdP7r15FIlgJBqhHVzUtiTLblBKOlqUVIsAx9LQ0aYkGTZlLCYtO3h2iobjKceG56XF" +
  183. "PLwYm7pBKYupsRlE39rOk3GZLn51Owpj89VpmyH/lVCkv0LZYCoDjqXtdPoGqf5lQHlaGN" +
  184. "Tx0L6BeegZJFnmV1djg6OitqPtRKSYZDrTMyrTOuC/BIJbGRhmXKfLktmkk8QwphfQRkA6" +
  185. "jz1zIy+PAbsRbInQi8qgF6C4H9PvfQ2E8NXrR7z9tJvf+Z1/ANt+S+GBXoDpAAAAAElFTk" +
  186. "SuQmCC"
  187. }