summaryrefslogtreecommitdiff
path: root/internal/task/debug.go
blob: 061dac2f0615fe8b77ce29426d7ab40b594f1d8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package task

import (
	"io"
	"os"
)

// debugConfig groups the optional debug-logging state for the task package.
// Collecting related vars into a struct makes the mutable state explicit and
// allows the logger to be swapped or reset cleanly without touching unrelated
// package globals.
type debugConfig struct {
	writer io.Writer
	file   *os.File // tracked separately so it can be closed on reconfiguration
}

// dbg holds the active debug-logging configuration for this package.
// It is written only via SetDebugLog and read only in run().
var dbg debugConfig

// SetDebugLog enables logging of executed commands to the given file.
// Passing an empty path disables logging and closes any previously opened file.
func SetDebugLog(path string) error {
	// Close existing debug file if open before re-configuring.
	if dbg.file != nil {
		_ = dbg.file.Close()
		dbg.file = nil
		dbg.writer = nil
	}

	if path == "" {
		return nil
	}

	f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
	if err != nil {
		return err
	}
	dbg.file = f
	dbg.writer = f
	return nil
}