Source file src/cmd/vendor/github.com/google/pprof/internal/driver/webhtml.go

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package driver
    16  
    17  import (
    18  	"embed"
    19  	"fmt"
    20  	"html/template"
    21  	"os"
    22  
    23  	"github.com/google/pprof/third_party/d3flamegraph"
    24  )
    25  
    26  //go:embed html
    27  var embeddedFiles embed.FS
    28  
    29  // addTemplates adds a set of template definitions to templates.
    30  func addTemplates(templates *template.Template) {
    31  	// Load specified file.
    32  	loadFile := func(fname string) string {
    33  		data, err := embeddedFiles.ReadFile(fname)
    34  		if err != nil {
    35  			fmt.Fprintf(os.Stderr, "internal/driver: embedded file %q not found\n",
    36  				fname)
    37  			os.Exit(1)
    38  		}
    39  		return string(data)
    40  	}
    41  	loadCSS := func(fname string) string {
    42  		return `<style type="text/css">` + "\n" + loadFile(fname) + `</style>` + "\n"
    43  	}
    44  	loadJS := func(fname string) string {
    45  		return `<script>` + "\n" + loadFile(fname) + `</script>` + "\n"
    46  	}
    47  
    48  	// Define a named template with specified contents.
    49  	def := func(name, contents string) {
    50  		sub := template.New(name)
    51  		template.Must(sub.Parse(contents))
    52  		template.Must(templates.AddParseTree(name, sub.Tree))
    53  	}
    54  
    55  	// Pre-packaged third-party files.
    56  	def("d3flamegraphscript", d3flamegraph.JSSource)
    57  	def("d3flamegraphcss", d3flamegraph.CSSSource)
    58  
    59  	// Embeded files.
    60  	def("css", loadCSS("html/common.css"))
    61  	def("header", loadFile("html/header.html"))
    62  	def("graph", loadFile("html/graph.html"))
    63  	def("script", loadJS("html/common.js"))
    64  	def("top", loadFile("html/top.html"))
    65  	def("sourcelisting", loadFile("html/source.html"))
    66  	def("plaintext", loadFile("html/plaintext.html"))
    67  	def("flamegraph", loadFile("html/flamegraph.html"))
    68  	def("stacks", loadFile("html/stacks.html"))
    69  	def("stacks_css", loadCSS("html/stacks.css"))
    70  	def("stacks_js", loadJS("html/stacks.js"))
    71  }
    72  

View as plain text