...

Source file src/github.com/google/pprof/pprof.go

Documentation: github.com/google/pprof

     1  // Copyright 2014 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  // pprof is a tool for collection, manipulation and visualization
    16  // of performance profiles.
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"strings"
    23  	"syscall"
    24  
    25  	"github.com/chzyer/readline"
    26  	"github.com/google/pprof/driver"
    27  )
    28  
    29  func main() {
    30  	if err := driver.PProf(&driver.Options{UI: newUI()}); err != nil {
    31  		fmt.Fprintf(os.Stderr, "pprof: %v\n", err)
    32  		os.Exit(2)
    33  	}
    34  }
    35  
    36  // readlineUI implements the driver.UI interface using the
    37  // github.com/chzyer/readline library.
    38  // This is contained in pprof.go to avoid adding the readline
    39  // dependency in the vendored copy of pprof in the Go distribution,
    40  // which does not use this file.
    41  type readlineUI struct {
    42  	rl *readline.Instance
    43  }
    44  
    45  func newUI() driver.UI {
    46  	rl, err := readline.New("")
    47  	if err != nil {
    48  		fmt.Fprintf(os.Stderr, "readline: %v", err)
    49  		return nil
    50  	}
    51  	return &readlineUI{
    52  		rl: rl,
    53  	}
    54  }
    55  
    56  // ReadLine returns a line of text (a command) read from the user.
    57  // prompt is printed before reading the command.
    58  func (r *readlineUI) ReadLine(prompt string) (string, error) {
    59  	r.rl.SetPrompt(prompt)
    60  	return r.rl.Readline()
    61  }
    62  
    63  // Print shows a message to the user.
    64  // It is printed over stderr as stdout is reserved for regular output.
    65  func (r *readlineUI) Print(args ...interface{}) {
    66  	text := fmt.Sprint(args...)
    67  	if !strings.HasSuffix(text, "\n") {
    68  		text += "\n"
    69  	}
    70  	fmt.Fprint(r.rl.Stderr(), text)
    71  }
    72  
    73  // PrintErr shows a message to the user, colored in red for emphasis.
    74  // It is printed over stderr as stdout is reserved for regular output.
    75  func (r *readlineUI) PrintErr(args ...interface{}) {
    76  	text := fmt.Sprint(args...)
    77  	if !strings.HasSuffix(text, "\n") {
    78  		text += "\n"
    79  	}
    80  	if readline.IsTerminal(int(syscall.Stderr)) {
    81  		text = colorize(text)
    82  	}
    83  	fmt.Fprint(r.rl.Stderr(), text)
    84  }
    85  
    86  // colorize the msg using ANSI color escapes.
    87  func colorize(msg string) string {
    88  	var red = 31
    89  	var colorEscape = fmt.Sprintf("\033[0;%dm", red)
    90  	var colorResetEscape = "\033[0m"
    91  	return colorEscape + msg + colorResetEscape
    92  }
    93  
    94  // IsTerminal returns whether the UI is known to be tied to an
    95  // interactive terminal (as opposed to being redirected to a file).
    96  func (r *readlineUI) IsTerminal() bool {
    97  	return readline.IsTerminal(int(syscall.Stdout))
    98  }
    99  
   100  // WantBrowser starts a browser on interactive mode.
   101  func (r *readlineUI) WantBrowser() bool {
   102  	return r.IsTerminal()
   103  }
   104  
   105  // SetAutoComplete instructs the UI to call complete(cmd) to obtain
   106  // the auto-completion of cmd, if the UI supports auto-completion at all.
   107  func (r *readlineUI) SetAutoComplete(complete func(string) string) {
   108  	// TODO: Implement auto-completion support.
   109  }
   110  

View as plain text