1
0
mirror of https://github.com/golang/go synced 2024-11-19 01:14:39 -07:00
go/internal/lsp/debug/info.go
Ian Cottrell 59bd84bb0c internal/lsp: change debug instance to a struct
We now build an instance and invoke methods on it.
This is a step towards removing some global state,
allowing multiple instances in one process and cleaning
up some telemetry handling.

Change-Id: I067469fed39c96653fffe96945c79193e86458d8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/218157
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2020-02-07 11:26:02 +00:00

77 lines
1.9 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package debug exports debug information for gopls.
package debug
import (
"fmt"
"io"
"os/exec"
"strings"
)
type PrintMode int
const (
PlainText = PrintMode(iota)
Markdown
HTML
)
// Version is a manually-updated mechanism for tracking versions.
var Version = "master"
// PrintServerInfo writes HTML debug info to w for the Instance.
func (i *Instance) PrintServerInfo(w io.Writer) {
section(w, HTML, "Server Instance", func() {
fmt.Fprintf(w, "Start time: %v\n", i.StartTime)
fmt.Fprintf(w, "LogFile: %s\n", i.Logfile)
fmt.Fprintf(w, "Working directory: %s\n", i.Workdir)
fmt.Fprintf(w, "Address: %s\n", i.ServerAddress)
fmt.Fprintf(w, "Debug address: %s\n", i.DebugAddress)
})
PrintVersionInfo(w, true, HTML)
}
// PrintVersionInfo writes version and environment information to w, using the
// output format specified by mode. verbose controls whether additional
// information is written, including section headers.
func PrintVersionInfo(w io.Writer, verbose bool, mode PrintMode) {
if !verbose {
printBuildInfo(w, false, mode)
return
}
section(w, mode, "Build info", func() {
printBuildInfo(w, true, mode)
})
fmt.Fprint(w, "\n")
section(w, mode, "Go info", func() {
cmd := exec.Command("go", "version")
cmd.Stdout = w
cmd.Run()
fmt.Fprint(w, "\n")
cmd = exec.Command("go", "env")
cmd.Stdout = w
cmd.Run()
})
}
func section(w io.Writer, mode PrintMode, title string, body func()) {
switch mode {
case PlainText:
fmt.Fprintln(w, title)
fmt.Fprintln(w, strings.Repeat("-", len(title)))
body()
case Markdown:
fmt.Fprintf(w, "#### %s\n\n```\n", title)
body()
fmt.Fprintf(w, "```\n")
case HTML:
fmt.Fprintf(w, "<h3>%s</h3>\n<pre>\n", title)
body()
fmt.Fprint(w, "</pre>\n")
}
}