Skip to content

Commit 6bccea9

Browse files
authored
Set default log to src-cli.debug.log if build tag debug is used (#533)
* Set default log to src-cli.debug.log if build tag debug is used I don't care about the specifics - filename, build tag, etc. - but I *need* this. And the reason why I need it is that it's hard to log something when our TUI is active. Not because of how the TUI is implemented, but because the whole concept of an updating TUI doesn't gel with printf-debugging. And this is perfect for me: I can `tail -f` the log file and then use `log.Printf` whereever I want. * Add info to DEVELOPMENT.md
1 parent f7cf20f commit 6bccea9

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

DEVELOPMENT.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,17 @@ To remove it and then force the upstream image on Docker Hub to be used again:
7878
```sh
7979
docker rmi sourcegraph/src-batch-change-volume-workspace
8080
```
81+
82+
## Use `debug` build tag to debug batch changes functionality
83+
84+
Since `src batch apply` and `src batch preview` start up a TUI that gets updated repeatedly it's nearly impossible to do printf-debugging by printing debug information - the TUI would hide those or overwrite them.
85+
86+
To help with that you can compile your src binary (or run the tests) with the `debug` build flag:
87+
88+
```
89+
go build -tags debug -o ~/src ./cmd/src
90+
```
91+
92+
This will cause the `./internal/batches/debug.go` file to be included in the build. In that file the `log` default package logger is setup to log to `~/.sourcegraph/src-cli.debug.log`.
93+
94+
That allows you to `tail -f ~/.sourcegraph/src-cli.debug.log` and use `log.Println()` in your code to debug.

cmd/src/batch_common.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ type executeBatchSpecOpts struct {
201201
// Sourcegraph, including execution as needed and applying the resulting batch
202202
// spec if specified.
203203
func executeBatchSpec(ctx context.Context, opts executeBatchSpecOpts) error {
204-
batches.DebugOut = opts.out
205204
svc := service.New(&service.Opts{
206205
AllowUnsupported: opts.flags.allowUnsupported,
207206
AllowIgnored: opts.flags.allowIgnored,

internal/batches/debug.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1+
// +build debug
2+
13
package batches
24

35
import (
4-
"github.com/sourcegraph/sourcegraph/lib/output"
6+
"log"
7+
"os"
8+
"path/filepath"
59
)
610

7-
// DebugOut can be used to print debug messages in development to the TUI.
8-
// For that it needs to be set to an actual *output.Output.
9-
var DebugOut output.Writer = output.NoopWriter{}
11+
// In builds with the debug flag (i.e. `go build -tags debug -o src ./cmd/src`)
12+
// init() sets up the default logger to log to a file in ~/.sourcegraph.
13+
func init() {
14+
homedir, err := os.UserHomeDir()
15+
if err != nil {
16+
log.Fatalf("getting user home directory: %s", err)
17+
}
18+
19+
fullPath := filepath.Join(homedir, ".sourcegraph", "src-cli.debug.log")
20+
21+
f, err := os.OpenFile(fullPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
22+
if err != nil {
23+
log.Fatalf("setting debug log file failed: %s", err)
24+
}
25+
26+
log.SetOutput(f)
27+
}

0 commit comments

Comments
 (0)